import clsx from 'clsx';
import {
	renderToString,
	useRef,
	useState,
	useEffect,
	useMemo,
} from '@wordpress/element';
import { useFocusableIframe, useMergeRefs } from '@wordpress/compose';
import type { SandBoxProps } from './types';

type SandBoxContentProps = Omit< SandBoxProps, 'allowSameOrigin' >;

/**
 * Matches CSS viewport-relative length values such as `100vh`, `50.5vw`,
 * and `.5dvh`. Used to strip viewport units from user-supplied HTML inside
 * the sandbox iframe, because those units are relative to the iframe's
 * own size and would create a measurement feedback loop with the
 * resize observer.
 *
 * Exported for tests. NOTE: an identical regex literal is duplicated
 * inside `observeAndResizeJS` below because that function is serialized
 * via `.toString()` and embedded into the iframe's `srcdoc` — it has no
 * access to this module's scope at runtime. If you change one, change
 * the other; the "is embedded in the sandbox iframe srcdoc" test
 * guards against drift.
 */
export const VIEWPORT_UNIT_VALUE_REGEX =
	/^\d*\.?\d+(?:vw|vh|svw|lvw|dvw|svh|lvh|dvh|vi|svi|lvi|dvi|vb|svb|lvb|dvb|vmin|svmin|lvmin|dvmin|vmax|svmax|lvmax|dvmax)$/;

const observeAndResizeJS = function () {
	const { MutationObserver } = window;

	if ( ! MutationObserver || ! window.parent ) {
		return;
	}

	function sendResize() {
		// This runs in the document <head>, and as a `load`/`resize` listener,
		// so the body may not exist yet. Bail rather than throw on a null body.
		if ( ! document.body ) {
			return;
		}

		const clientBoundingRect = document.body.getBoundingClientRect();

		window.parent.postMessage(
			{
				action: 'resize',
				width: clientBoundingRect.width,
				height: clientBoundingRect.height,
			},
			'*'
		);
	}

	// Runs the body-dependent setup once the document body is available. Reads
	// `document.body` into a local so the null check narrows it for the rest of
	// the function: this runs in <head> and via DOMContentLoaded, so the body
	// isn't guaranteed to exist yet.
	function connect() {
		const { body } = document;
		if ( ! body ) {
			return;
		}

		const observer = new MutationObserver( sendResize );
		observer.observe( body, {
			attributes: true,
			attributeOldValue: false,
			characterData: true,
			characterDataOldValue: false,
			childList: true,
			subtree: true,
		} );

		// Hack: Remove viewport unit styles, as these are relative
		// the iframe root and interfere with our mechanism for
		// determining the unconstrained page bounds.
		function removeViewportStyles( ruleOrNode: ElementCSSInlineStyle ) {
			if ( ruleOrNode.style ) {
				(
					[ 'width', 'height', 'minHeight', 'maxHeight' ] as const
				 ).forEach( function ( style ) {
					if (
						/^\d*\.?\d+(?:vw|vh|svw|lvw|dvw|svh|lvh|dvh|vi|svi|lvi|dvi|vb|svb|lvb|dvb|vmin|svmin|lvmin|dvmin|vmax|svmax|lvmax|dvmax)$/.test(
							ruleOrNode.style[ style ]
						)
					) {
						ruleOrNode.style[ style ] = '';
					}
				} );
			}
		}

		Array.prototype.forEach.call(
			document.querySelectorAll( '[style]' ),
			removeViewportStyles
		);
		Array.prototype.forEach.call(
			document.styleSheets,
			function ( stylesheet ) {
				let rules;
				try {
					rules = stylesheet.cssRules || stylesheet.rules;
				} catch {
					// Reading `cssRules` throws a SecurityError for a
					// cross-origin stylesheet (e.g. a remote <link> in the
					// user's HTML). Skip those rather than aborting the setup.
					return;
				}
				Array.prototype.forEach.call( rules, removeViewportStyles );
			}
		);

		body.style.position = 'absolute';
		body.style.width = '100%';
		body.setAttribute( 'data-resizable-iframe-connected', '' );

		sendResize();
	}

	window.addEventListener( 'load', sendResize, true );

	// Resize events can change the width of elements with 100% width, but we don't
	// get an DOM mutations for that, so do the resize when the window is resized, too.
	window.addEventListener( 'resize', sendResize, true );

	// This script is injected into the document <head> so it parses before the
	// (possibly malformed) user content in <body>: an unclosed attribute quote
	// in that content can't then swallow this <script> tag and dump its source
	// into the preview as visible text. Because <head> runs before <body>
	// exists, defer the body-dependent setup until the DOM is ready.
	if ( document.body ) {
		connect();
	} else {
		document.addEventListener( 'DOMContentLoaded', connect );
	}
};

// TODO: These styles shouldn't be coupled with WordPress.
const style = `
	body {
		margin: 0;
	}
	html,
	body,
	body > div {
		width: 100%;
	}
	html.wp-has-aspect-ratio,
	body.wp-has-aspect-ratio,
	body.wp-has-aspect-ratio > div,
	body.wp-has-aspect-ratio > div iframe {
		width: 100%;
		height: 100%;
		overflow: hidden; /* If it has an aspect ratio, it shouldn't scroll. */
	}
	body > div > * {
		margin-top: 0 !important; /* Has to have !important to override inline styles. */
		margin-bottom: 0 !important;
	}
`;

/**
 * Builds the full HTML document string for the sandbox iframe content. Shared
 * by both the isolated (`srcdoc`) and same-origin (`contentDocument.write`)
 * sandboxes so the two paths always emit the same markup — notably keeping the
 * resize script in `<head>`, ahead of the user content, on both. A single
 * builder means the write path cannot drift back to putting the script in
 * `<body>` while the `srcdoc` path keeps it in `<head>`.
 *
 * `lang` is passed in rather than read here because the two callers source it
 * from different documents: the isolated sandbox uses the module's `document`,
 * while the same-origin sandbox uses the iframe's `ownerDocument`. They are
 * normally the same document, but keeping the argument preserves each path's
 * exact prior behavior.
 *
 * Exported for tests.
 *
 * @param props
 * @param props.html    User-supplied HTML for the iframe body.
 * @param props.title   Document title.
 * @param props.type    Optional class name for the `<html>`/`<body>` elements.
 * @param props.styles  CSS rule strings, injected as `<style>` tags in the head.
 * @param props.scripts External script URLs, injected as `<script src>` in the
 *                      body (embeds expect their scripts there).
 * @param props.lang    Language for the `<html lang>` attribute.
 * @return The full `<!DOCTYPE html>…` document string.
 */
export function buildSandBoxDocument( {
	html,
	title,
	type,
	styles,
	scripts,
	lang,
}: {
	html: string;
	title: string;
	type?: string;
	styles: string[];
	scripts: string[];
	lang: string;
} ): string {
	const htmlDoc = (
		<html lang={ lang } className={ type }>
			<head>
				<title>{ title }</title>
				<style dangerouslySetInnerHTML={ { __html: style } } />
				{ styles.map( ( rules, i ) => (
					<style
						key={ i }
						dangerouslySetInnerHTML={ { __html: rules } }
					/>
				) ) }
				<script
					type="text/javascript"
					dangerouslySetInnerHTML={ {
						__html: `(${ observeAndResizeJS.toString() })();`,
					} }
				/>
			</head>
			<body
				data-resizable-iframe-connected="data-resizable-iframe-connected"
				className={ type }
			>
				<div dangerouslySetInnerHTML={ { __html: html } } />
				{ scripts.map( ( src ) => (
					<script key={ src } src={ src } />
				) ) }
			</body>
		</html>
	);

	return '<!DOCTYPE html>' + renderToString( htmlDoc );
}

/**
 * Isolated sandbox that uses the `srcdoc` attribute to render content
 * without `allow-same-origin`. This is the default for user-controlled
 * content (e.g., the HTML block) where same-origin access would be a
 * security risk.
 *
 * Because `srcdoc` is a declarative attribute, the browser automatically
 * re-renders the content when the iframe is moved in the DOM (e.g.,
 * block reordering), so no `load` event listener is needed.
 * The `message` listener is re-synced on every `load` so
 * it follows the iframe if it's reparented into a different document.
 */
function IsolatedSandBox( {
	html = '',
	title = '',
	type,
	styles = [],
	scripts = [],
	onFocus,
	tabIndex,
	allowPopups = false,
	allowForms = false,
}: SandBoxContentProps ) {
	const ref = useRef< HTMLIFrameElement >( null );
	const [ width, setWidth ] = useState( 0 );
	const [ height, setHeight ] = useState( 0 );

	const sandbox = clsx( 'allow-scripts', 'allow-presentation', {
		'allow-popups': allowPopups,
		'allow-forms': allowForms,
	} );

	const srcDoc = useMemo(
		() =>
			buildSandBoxDocument( {
				html,
				title,
				type,
				styles,
				scripts,
				// Read inside the memo: the `no-dom-globals-in-react-fc` lint
				// rule forbids DOM globals in the render body. `lang` rarely
				// changes after load, so leaving it out of the deps is fine.
				lang: document.documentElement.lang,
			} ),
		[ html, title, type, styles, scripts ]
	);

	useEffect( () => {
		const iframe = ref.current;
		if ( ! iframe ) {
			return;
		}

		function checkMessageForResize( event: MessageEvent ) {
			// Verify that the mounted element is the source of the message.
			// iframe.contentWindow is accessible cross-origin as a
			// WindowProxy reference, so this check still works without
			// allow-same-origin.
			if ( ! iframe || iframe.contentWindow !== event.source ) {
				return;
			}

			// Attempt to parse the message data as JSON if passed as string.
			let data = event.data || {};

			if ( 'string' === typeof data ) {
				try {
					data = JSON.parse( data );
				} catch {}
			}

			// Update the state only if the message is formatted as we expect,
			// i.e. as an object with a 'resize' action.
			if ( 'resize' !== data.action ) {
				return;
			}

			setWidth( data.width );
			setHeight( data.height );
		}

		let currentView: Window | null = null;
		function syncListener() {
			const view = iframe?.ownerDocument?.defaultView ?? null;
			if ( view === currentView ) {
				return;
			}

			currentView?.removeEventListener(
				'message',
				checkMessageForResize
			);

			currentView = view;
			currentView?.addEventListener( 'message', checkMessageForResize );
		}

		syncListener();
		iframe.addEventListener( 'load', syncListener );

		return () => {
			iframe.removeEventListener( 'load', syncListener );
			currentView?.removeEventListener(
				'message',
				checkMessageForResize
			);
		};
	}, [] );

	return (
		<iframe
			ref={ useMergeRefs( [ ref, useFocusableIframe() ] ) }
			title={ title }
			tabIndex={ tabIndex }
			className="components-sandbox"
			sandbox={ sandbox }
			srcDoc={ srcDoc }
			onFocus={ onFocus }
			width={ Math.ceil( width ) }
			height={ Math.ceil( height ) }
		/>
	);
}

/**
 * Same-origin sandbox that writes to `contentDocument` directly. This
 * preserves the parent page's URL as the iframe's document URL, which
 * provides a valid Referer header for nested iframes (required by
 * providers like YouTube).
 *
 * Only used when `allowSameOrigin` is true — i.e., for server-fetched
 * oEmbed previews that are not directly user-controlled.
 *
 * This implementation is intentionally kept close to the original
 * pre-refactor code to preserve past bugfixes:
 * - load listener for iframe re-initialization after DOM moves (#21916)
 * - forceRerender guard to avoid unnecessary full rewrites (#20176)
 */
function SameOriginSandBox( {
	html = '',
	title = '',
	type,
	styles = [],
	scripts = [],
	onFocus,
	tabIndex,
	allowPopups = false,
	allowForms = false,
}: SandBoxContentProps ) {
	const ref = useRef< HTMLIFrameElement >( null );
	const [ width, setWidth ] = useState( 0 );
	const [ height, setHeight ] = useState( 0 );

	const sandbox = clsx(
		'allow-scripts',
		'allow-same-origin',
		'allow-presentation',
		{
			'allow-popups': allowPopups,
			'allow-forms': allowForms,
		}
	);

	function isFrameAccessible() {
		try {
			return !! ref.current?.contentDocument?.body;
		} catch {
			return false;
		}
	}

	function trySandBox( forceRerender = false ) {
		if ( ! isFrameAccessible() ) {
			return;
		}

		const { contentDocument, ownerDocument } =
			ref.current as HTMLIFrameElement & {
				contentDocument: Document;
			};

		if (
			! forceRerender &&
			null !==
				contentDocument?.body.getAttribute(
					'data-resizable-iframe-connected'
				)
		) {
			return;
		}

		// Put the html snippet into a html document, and then write it to the
		// iframe's document. The external `scripts` still go into the body to
		// support embedded content such as Instagram that expects them there;
		// only the resize helper lives in the head.
		//
		// Writing the document like this makes it act in the same way as if it
		// was loaded over the network, so DOM creation and mutation, script
		// execution, etc. all work as expected.
		contentDocument.open();
		// Nested iframes should send this page's URL as their `Referer`, which
		// YouTube requires. Safari sends none after `open()` until the URL is
		// set again with `replaceState()`. Firefox throws here but already
		// sends the referrer. See https://bugs.webkit.org/show_bug.cgi?id=169846.
		try {
			contentDocument.defaultView?.history.replaceState(
				null,
				'',
				window.location.href
			);
		} catch {}
		contentDocument.write(
			buildSandBoxDocument( {
				html,
				title,
				type,
				styles,
				scripts,
				lang: ownerDocument.documentElement.lang,
			} )
		);
		contentDocument.close();
	}

	useEffect( () => {
		trySandBox();

		function tryNoForceSandBox() {
			trySandBox( false );
		}

		function checkMessageForResize( event: MessageEvent ) {
			const iframe = ref.current;

			// Verify that the mounted element is the source of the message.
			if ( ! iframe || iframe.contentWindow !== event.source ) {
				return;
			}

			// Attempt to parse the message data as JSON if passed as string.
			let data = event.data || {};

			if ( 'string' === typeof data ) {
				try {
					data = JSON.parse( data );
				} catch {}
			}

			// Update the state only if the message is formatted as we expect,
			// i.e. as an object with a 'resize' action.
			if ( 'resize' !== data.action ) {
				return;
			}

			setWidth( data.width );
			setHeight( data.height );
		}

		const iframe = ref.current;
		const defaultView = iframe?.ownerDocument?.defaultView;

		// This used to be registered using <iframe onLoad={} />, but it made the iframe blank
		// after reordering the containing block. See these two issues for more details:
		// https://github.com/WordPress/gutenberg/issues/6146
		// https://github.com/facebook/react/issues/18752
		iframe?.addEventListener( 'load', tryNoForceSandBox, false );
		defaultView?.addEventListener( 'message', checkMessageForResize );

		return () => {
			iframe?.removeEventListener( 'load', tryNoForceSandBox, false );
			defaultView?.removeEventListener(
				'message',
				checkMessageForResize
			);
		};
		// Passing `exhaustive-deps` will likely involve a more detailed refactor.
		// See https://github.com/WordPress/gutenberg/pull/44378
	}, [] );

	useEffect( () => {
		trySandBox();
		// Passing `exhaustive-deps` will likely involve a more detailed refactor.
		// See https://github.com/WordPress/gutenberg/pull/44378
	}, [ title, styles, scripts ] );

	useEffect( () => {
		trySandBox( true );
		// Passing `exhaustive-deps` will likely involve a more detailed refactor.
		// See https://github.com/WordPress/gutenberg/pull/44378
	}, [ html, type ] );

	return (
		<iframe
			ref={ useMergeRefs( [ ref, useFocusableIframe() ] ) }
			title={ title }
			tabIndex={ tabIndex }
			className="components-sandbox"
			sandbox={ sandbox }
			onFocus={ onFocus }
			width={ Math.ceil( width ) }
			height={ Math.ceil( height ) }
		/>
	);
}

/**
 * This component provides an isolated environment for arbitrary HTML via iframes.
 *
 * ```jsx
 * import { SandBox } from '@wordpress/components';
 *
 * const MySandBox = () => (
 * 	<SandBox html="<p>Content</p>" title="SandBox" type="embed" />
 * );
 * ```
 */
function SandBox( { allowSameOrigin = false, ...contentProps }: SandBoxProps ) {
	if ( allowSameOrigin ) {
		return <SameOriginSandBox { ...contentProps } />;
	}
	return <IsolatedSandBox { ...contentProps } />;
}

export default SandBox;
