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

import { flushEvents as flushEventsCore } from '@aws-amplify/core/internals/providers/pinpoint';
import {
	AnalyticsAction,
	resolveCtxArgs,
} from '@aws-amplify/core/internals/utils';
import { AmplifyContext, ConsoleLogger } from '@aws-amplify/core';

import { resolveConfig, resolveCredentials } from '../utils';
import { getAnalyticsUserAgentString } from '../../../utils';

const logger = new ConsoleLogger('Analytics');
export function flushEvents(ctx: AmplifyContext): void;

/**
 * Flushes all buffered Pinpoint events to the service.
 *
 * @deprecated AWS will end support for Amazon Pinpoint on October 30, 2026.
 *
 * @note
 * This API will make a best-effort attempt to flush events from the buffer. Events recorded immediately after invoking
 * this API may not be included in the flush.
 */
export function flushEvents(): void;
export function flushEvents(...args: any[]): void {
	const [ctx] = resolveCtxArgs<[]>(args);
	const { appId, region, bufferSize, flushSize, flushInterval, resendLimit } =
		resolveConfig(ctx);
	resolveCredentials(ctx)
		.then(({ credentials, identityId }) => {
			flushEventsCore({
				appId,
				region,
				credentials,
				identityId,
				bufferSize,
				flushSize,
				flushInterval,
				resendLimit,
				userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
			});
		})
		.catch(e => {
			logger.warn('Failed to flush events', e);
		});
}
