"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WORKER_HOST_HARD_CODE = void 0;
exports.startInsideIframe = startInsideIframe;
const ide_core_common_1 = require("@opensumi/ide-core-common");
exports.WORKER_HOST_HARD_CODE = `
(function() {
  const workerSrc = document.getElementById('kaitian-worker-src').getAttribute('data-value');
	const worker = new Worker(workerSrc, { name: 'KaitianWorkerExtensionHost' });
	const kaitianWebWorkerExtHostId = document.getElementById('kaitian-web-worker-ext-host-id').getAttribute('data-value');

	worker.onmessage = (event) => {
    const { data } = event;
    if (data instanceof MessagePort) {
      window.parent.postMessage({
        kaitianWebWorkerExtHostId,
        data: data
      }, '*', [data]);
      return;
    }

    window.parent.postMessage({
      kaitianWebWorkerExtHostId,
      data: data
    }, '*');
	};

	worker.onerror = (event) => {
		console.error(event.message, event.error);
		window.parent.postMessage({
			kaitianWebWorkerExtHostId,
			error: {
				name: event.error ? event.error.name : '',
				message: event.error ? event.error.message : '',
				stack: event.error ? event.error.stack : []
			}
		}, '*');
	};

	window.addEventListener('message', function(event) {
		if (event.source !== window.parent) {
			return;
		}
		if (event.data.kaitianWebWorkerExtHostId !== kaitianWebWorkerExtHostId) {
			return;
    }

		worker.postMessage(event.data.data);
	}, false);
})();
`;
function startInsideIframe(workerSrc) {
    const iframe = document.createElement('iframe');
    iframe.setAttribute('class', 'web-worker-ext-host-iframe');
    iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin');
    iframe.style.display = 'none';
    const escapeAttribute = (value) => value.replace(/"/g, '&quot;');
    const extHostUuid = (0, ide_core_common_1.uuid)();
    const html = `<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' http:; worker-src data:; connect-src http:" />
		<meta id="kaitian-worker-src" data-value="${escapeAttribute(workerSrc)}" />
		<meta id="kaitian-web-worker-ext-host-id" data-value="${escapeAttribute(extHostUuid)}" />
	</head>
	<body>
	<script>${exports.WORKER_HOST_HARD_CODE}</script>
	</body>
</html>`;
    const iframeContent = `data:text/html;charset=utf-8,${encodeURIComponent(html)}`;
    iframe.setAttribute('src', iframeContent);
    document.body.appendChild(iframe);
    return { iframe, extHostUuid };
}
//# sourceMappingURL=workerHostIframe.js.map