/**
 * WordPress dependencies
 */
import { createInterpolateElement } from '@safe-wordpress/element';
import { sprintf, _x } from '@safe-wordpress/i18n';

/**
 * External dependencies
 */
import { ConversionActionScopeView } from '@nab/components';
import type { CAViewProps, ConversionActionScope } from '@nab/types';

/**
 * Internal dependencies
 */
import type { Attributes } from './types';

export const View = ( props: CAViewProps< Attributes > ): JSX.Element => (
	<>
		<div>
			<ActualView { ...props } />
		</div>
		<ConversionActionScopeView scope={ props.scope } />
	</>
);

const ActualView = ( {
	attributes: { mode, value },
	scope,
}: CAViewProps< Attributes > ): JSX.Element => {
	if ( ! value ) {
		return <>{ getLinkLabel( scope ) }</>;
	}

	switch ( mode ) {
		case 'partial':
			return (
				<>
					{ createInterpolateElement(
						sprintf(
							getPartialLabel( scope ),
							`<code>${ value }</code>`
						),
						{ code: <code /> }
					) }
				</>
			);
		case 'start':
			return (
				<>
					{ createInterpolateElement(
						sprintf(
							getStartLabel( scope ),
							`<code>${ value }</code>`
						),
						{ code: <code /> }
					) }
				</>
			);
		case 'end':
			return (
				<>
					{ createInterpolateElement(
						sprintf(
							getEndLabel( scope ),
							`<code>${ value }</code>`
						),
						{ code: <code /> }
					) }
				</>
			);
		case 'regex':
			return (
				<>
					{ createInterpolateElement(
						sprintf(
							getRegexLabel( scope ),
							`<code>${ value }</code>`
						),
						{ code: <code /> }
					) }
				</>
			);
		case 'exact':
			return (
				<>
					{ createInterpolateElement(
						sprintf(
							getExactLabel( scope ),
							`<code>${ value }</code>`
						),
						{ code: <code /> }
					) }
				</>
			);
	}
};

// =======
// HELPERS
// =======

function getLinkLabel( scope: ConversionActionScope ): string {
	switch ( scope.type ) {
		case 'all-pages':
			return _x(
				'A visitor clicks on a link on any page.',
				'text',
				'nelio-ab-testing'
			);

		case 'test-scope':
			return _x(
				'A visitor clicks on a link on a tested page.',
				'text',
				'nelio-ab-testing'
			);

		case 'urls':
		case 'post-ids':
		case 'php-function':
			return _x(
				'A visitor clicks on a link on certain pages.',
				'text',
				'nelio-ab-testing'
			);
	}
}

function getPartialLabel( scope: ConversionActionScope ): string {
	switch ( scope.type ) {
		case 'all-pages':
			/* translators: %s: Some text that might appear in a URL, such as "wordpress", or "amazon", or "nelio". */
			return _x(
				'A visitor clicks on a link containing %s on any page.',
				'text',
				'nelio-ab-testing'
			);

		case 'test-scope':
			/* translators: %s: Some text that might appear in a URL, such as "wordpress", or "amazon", or "nelio". */
			return _x(
				'A visitor clicks on a link containing %s on a tested page.',
				'text',
				'nelio-ab-testing'
			);

		case 'urls':
		case 'post-ids':
		case 'php-function':
			/* translators: %s: Some text that might appear in a URL, such as "wordpress", or "amazon", or "nelio". */
			return _x(
				'A visitor clicks on a link containing %s on certain pages.',
				'text',
				'nelio-ab-testing'
			);
	}
}

function getStartLabel( scope: ConversionActionScope ): string {
	switch ( scope.type ) {
		case 'all-pages':
			/* translators: %s: URL fragment, such as "https://nelio". */
			return _x(
				'A visitor clicks on a link starting with %s on any page.',
				'text',
				'nelio-ab-testing'
			);

		case 'test-scope':
			/* translators: %s: URL fragment, such as "https://nelio". */
			return _x(
				'A visitor clicks on a link starting with %s on a tested page.',
				'text',
				'nelio-ab-testing'
			);

		case 'urls':
		case 'post-ids':
		case 'php-function':
			/* translators: %s: URL fragment, such as "https://nelio". */
			return _x(
				'A visitor clicks on a link starting with %s on certain pages.',
				'text',
				'nelio-ab-testing'
			);
	}
}

function getEndLabel( scope: ConversionActionScope ): string {
	switch ( scope.type ) {
		case 'all-pages':
			/* translators: %s: URL fragment, such as "/blog/". */
			return _x(
				'A visitor clicks on a link ending with %s on any page.',
				'text',
				'nelio-ab-testing'
			);

		case 'test-scope':
			/* translators: %s: URL fragment, such as "/blog/". */
			return _x(
				'A visitor clicks on a link ending with %s on a tested page.',
				'text',
				'nelio-ab-testing'
			);

		case 'urls':
		case 'post-ids':
		case 'php-function':
			/* translators: %s: URL fragment, such as "/blog/". */
			return _x(
				'A visitor clicks on a link ending with %s on certain pages.',
				'text',
				'nelio-ab-testing'
			);
	}
}

function getRegexLabel( scope: ConversionActionScope ): string {
	switch ( scope.type ) {
		case 'all-pages':
			/* translators: %s: JavaScript regular expression. */
			return _x(
				'A visitor clicks on a link matching regex %s on any page.',
				'text',
				'nelio-ab-testing'
			);

		case 'test-scope':
			/* translators: %s: JavaScript regular expression. */
			return _x(
				'A visitor clicks on a link matching regex %s on a tested page.',
				'text',
				'nelio-ab-testing'
			);

		case 'urls':
		case 'post-ids':
		case 'php-function':
			/* translators: %s: JavaScript regular expression. */
			return _x(
				'A visitor clicks on a link matching regex %s on certain pages.',
				'text',
				'nelio-ab-testing'
			);
	}
}

function getExactLabel( scope: ConversionActionScope ): string {
	switch ( scope.type ) {
		case 'all-pages':
			/* translators: %s: URL such as "https://wordpress.org/plugins/nelio-ab-testing/". */
			return _x(
				'A visitor clicks on a link to %s on any page.',
				'text',
				'nelio-ab-testing'
			);

		case 'test-scope':
			/* translators: %s: URL such as "https://wordpress.org/plugins/nelio-ab-testing/". */
			return _x(
				'A visitor clicks on a link to %s on a tested page.',
				'text',
				'nelio-ab-testing'
			);

		case 'urls':
		case 'post-ids':
		case 'php-function':
			/* translators: %s: URL such as "https://wordpress.org/plugins/nelio-ab-testing/". */
			return _x(
				'A visitor clicks on a link to %s on certain pages.',
				'text',
				'nelio-ab-testing'
			);
	}
}
