import { __ } from '@wordpress/i18n';
import type { CheckResult } from '../../shared/api/types/links';
import type { Problem } from '../../shared/links/problem';

export type AddressTestState =
	| { kind: 'none' }
	| { kind: 'checking' }
	| { kind: 'ok'; text: string }
	| { kind: 'bad'; text: string };

export const UNTESTED: AddressTestState = { kind: 'none' };

export function initialTestState( problem: Problem ): AddressTestState {
	if ( problem !== 'moved' ) {
		return UNTESTED;
	}

	return {
		kind: 'ok',
		text: __(
			'This is where the old address leads now.',
			'aiplaz-link-checkup'
		),
	};
}

export function testStateOf( checkResult: CheckResult ): AddressTestState {
	if (
		checkResult.verdict === 'working' ||
		checkResult.verdict === 'moved'
	) {
		return {
			kind: 'ok',
			text: __( 'This address works.', 'aiplaz-link-checkup' ),
		};
	}

	return {
		kind: 'bad',
		text: __(
			'This address did not answer. Check the spelling.',
			'aiplaz-link-checkup'
		),
	};
}

export function emptyAddressState(): AddressTestState {
	return {
		kind: 'bad',
		text: __( 'Type an address first.', 'aiplaz-link-checkup' ),
	};
}
