import { __, sprintf } from '@wordpress/i18n';
import type { Fix, FixAction } from '../../shared/api/types/fixes';
import { withUnchangedPlaces } from '../../shared/fixes/unchangedPlacesText';
import { linkName } from '../../shared/links/linkName';
import { shortUrl } from '../../shared/lib/url';
import { linksPhrase, placesPhrase } from '../../shared/text/countedPhrases';
import type { IconName } from '../../shared/ui/Icon';

export function fixIcon( action: FixAction ): IconName {
	switch ( action ) {
		case 'replace':
			return 'pen';
		case 'update':
			return 'moved';
		case 'remove':
			return 'unlink';
		case 'redirect':
			return 'redirect';
		case 'ignore':
			return 'eyeoff';
		case 'works':
			return 'check';
	}
}

function fixSubject( fix: Fix ): string {
	if ( fix.links_total === 1 ) {
		return linkName( fix.first_url );
	}

	return linksPhrase( fix.links_total );
}

export function fixTitle( fix: Fix ): string {
	const subject = fixSubject( fix );

	switch ( fix.action ) {
		case 'replace':
			return sprintf(
				/* translators: %s: an address, or a number of links such as "12 links" */
				__( 'Replaced %s', 'aiplaz-link-checkup' ),
				subject
			);
		case 'update':
			return sprintf(
				/* translators: %s: an address, or a number of links such as "12 links" */
				__( 'Updated %s', 'aiplaz-link-checkup' ),
				subject
			);
		case 'remove':
			return sprintf(
				/* translators: %s: an address, or a number of links such as "12 links" */
				__( 'Removed %s', 'aiplaz-link-checkup' ),
				subject
			);
		case 'redirect':
			return sprintf(
				/* translators: %s: an address, or a number of links such as "12 links" */
				__( 'Redirected %s', 'aiplaz-link-checkup' ),
				subject
			);
		case 'ignore':
			return sprintf(
				/* translators: %s: an address, or a number of links such as "12 links" */
				__( 'Ignored %s', 'aiplaz-link-checkup' ),
				subject
			);
		case 'works':
			return sprintf(
				/* translators: %s: an address, or a number of links such as "12 links" */
				__( 'Marked as working: %s', 'aiplaz-link-checkup' ),
				subject
			);
	}
}

export function fixDetail( fix: Fix ): string {
	if ( fix.action === 'ignore' || fix.action === 'works' ) {
		return __( 'No posts were changed.', 'aiplaz-link-checkup' );
	}

	if ( fix.action === 'redirect' ) {
		return sprintf(
			/* translators: %s: the address visitors are sent to */
			__( 'Visitors now go to %s.', 'aiplaz-link-checkup' ),
			shortUrl( fix.new_url ?? '' )
		);
	}

	if ( fix.state === 'running' ) {
		return sprintf(
			/* translators: 1: links fixed so far, 2: links in total, for example "12 links" */
			__( 'Stopped after %1$s of %2$s.', 'aiplaz-link-checkup' ),
			String( fix.links_done ),
			linksPhrase( fix.links_total )
		);
	}

	return withUnchangedPlaces(
		sprintf(
			/* translators: %s: number of places, for example "3 places" */
			__( '%s changed.', 'aiplaz-link-checkup' ),
			placesPhrase( fix.places_done )
		),
		fix
	);
}

export function fixStateLabel( fix: Fix ): string {
	switch ( fix.state ) {
		case 'done':
			return __( 'Done', 'aiplaz-link-checkup' );
		case 'running':
			return __( 'Stopped', 'aiplaz-link-checkup' );
		case 'undoing':
			return __( 'Undoing', 'aiplaz-link-checkup' );
		case 'undone':
			return __( 'Undone', 'aiplaz-link-checkup' );
	}
}
