import { __, _n, sprintf } from '@wordpress/i18n';
import type { Fix } from '../../shared/api/types/fixes';
import { formatNumber } from '../../shared/lib/numbers';
import { withUnchangedPlaces } from '../../shared/fixes/unchangedPlacesText';
import { linksPhrase } from '../../shared/text/countedPhrases';

export type FixAllMode = 'broken' | 'moved';

export function chooseTitle( mode: FixAllMode, count: number ): string {
	const formattedCount = formatNumber( count );

	if ( mode === 'moved' ) {
		return sprintf(
			/* translators: %s: number of moved links */
			_n(
				'Update %s moved link',
				'Update all %s moved links',
				count,
				'aiplaz-link-checkup'
			),
			formattedCount
		);
	}

	return sprintf(
		/* translators: %s: number of broken links */
		_n(
			'Fix %s broken link',
			'Fix all %s broken links',
			count,
			'aiplaz-link-checkup'
		),
		formattedCount
	);
}

export function startLabel( mode: FixAllMode, count: number ): string {
	if ( mode === 'moved' ) {
		return sprintf(
			/* translators: %s: number of links, for example "8 links" */
			__( 'Update %s', 'aiplaz-link-checkup' ),
			linksPhrase( count )
		);
	}

	return sprintf(
		/* translators: %s: number of links, for example "12 links" */
		__( 'Fix %s', 'aiplaz-link-checkup' ),
		linksPhrase( count )
	);
}

export function doneTitle( fix: Fix ): string {
	if ( fix.places_done === 0 && fix.places_failed > 0 ) {
		return __( 'Nothing was changed', 'aiplaz-link-checkup' );
	}

	const count = linksPhrase( fix.links_done );

	switch ( fix.action ) {
		case 'remove':
			/* translators: %s: number of links, for example "12 links" */
			return sprintf( __( '%s removed', 'aiplaz-link-checkup' ), count );
		case 'ignore':
			/* translators: %s: number of links, for example "12 links" */
			return sprintf( __( '%s ignored', 'aiplaz-link-checkup' ), count );
		case 'update':
			/* translators: %s: number of links, for example "8 links" */
			return sprintf( __( '%s updated', 'aiplaz-link-checkup' ), count );
		default:
			/* translators: %s: number of links, for example "12 links" */
			return sprintf( __( '%s replaced', 'aiplaz-link-checkup' ), count );
	}
}

export function doneText( fix: Fix ): string {
	if ( fix.action === 'ignore' ) {
		return __( 'Your posts were not changed.', 'aiplaz-link-checkup' );
	}

	const sentences: string[] = [
		sprintf(
			/* translators: %s: number of places */
			_n(
				'%s place was changed.',
				'%s places were changed.',
				fix.places_done,
				'aiplaz-link-checkup'
			),
			formatNumber( fix.places_done )
		),
	];

	if ( fix.action === 'remove' ) {
		sentences.push(
			__( 'The text is still there.', 'aiplaz-link-checkup' )
		);
	}

	sentences.push(
		__( 'A revision of each post was saved.', 'aiplaz-link-checkup' )
	);

	return withUnchangedPlaces( sentences.join( ' ' ), fix );
}

export function stoppedText( fix: Fix ): string {
	return sprintf(
		/* translators: %s: number of links, for example "4 links" */
		__(
			'You stopped it. %s were not touched. You can continue from History.',
			'aiplaz-link-checkup'
		),
		linksPhrase( fix.links_total - fix.links_done )
	);
}
