// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import {
	AmplifyContext,
	getGlobalContext,
	isAmplifyContext,
} from '@aws-amplify/core';

import { copy as copyInternal } from '../../providers/s3/apis/internal/copy';
import { CopyInput } from '../types/inputs';
import { CopyOutput } from '../types/outputs';

/**
 * @internal
 */
export function copy(
	ctx: AmplifyContext,
	input: CopyInput,
): Promise<CopyOutput>;
/**
 * @internal
 */
export function copy(input: CopyInput): Promise<CopyOutput>;
export function copy(
	ctxOrInput: AmplifyContext | CopyInput,
	maybeInput?: CopyInput,
): Promise<CopyOutput> {
	// Resolve the optional leading context. The global context is resolved at
	// CALL time (never cached) so single-arg callers follow live configuration.
	const [ctx, input]: [AmplifyContext, CopyInput] = isAmplifyContext(ctxOrInput)
		? [ctxOrInput, maybeInput as CopyInput]
		: [getGlobalContext(), ctxOrInput];

	return copyInternal(ctx, {
		source: {
			path: input.source.path,
			bucket: input.source.bucket,
			eTag: input.source.eTag,
			notModifiedSince: input.source.notModifiedSince,
			expectedBucketOwner: input.source.expectedBucketOwner,
		},
		destination: {
			path: input.destination.path,
			bucket: input.destination.bucket,
			expectedBucketOwner: input.destination.expectedBucketOwner,
		},
		options: {
			// Advanced options
			locationCredentialsProvider: input.options?.locationCredentialsProvider,
			customEndpoint: input?.options?.customEndpoint,
		},
		// Type casting is necessary because `copyInternal` supports both Gen1 and Gen2 signatures, but here
		// given in input can only be Gen2 signature, the return can only ben Gen2 signature.
	}) as Promise<CopyOutput>;
}
