let effectSetup = false;
function KCDShopIFrameSync({
  React,
  navigate
}) {
  React.useEffect(() => {
    if (effectSetup)
      return;
    effectSetup = true;
    if (window.parent === window)
      return;
    window.__kcdshop__?.onHydrated?.();
    const methods = [
      "pushState",
      "replaceState",
      "go",
      "forward",
      "back"
    ];
    for (const method of methods) {
      window.history[method] = new Proxy(window.history[method], {
        // eslint-disable-next-line no-loop-func
        apply(target, thisArg, argArray) {
          window.parent.postMessage(
            { type: "kcdshop:history-call", method, args: argArray },
            "*"
          );
          return target.apply(thisArg, argArray);
        }
      });
    }
  }, []);
  React.useEffect(() => {
    function handleMessage(event) {
      const { type, params } = event.data;
      if (type === "kcdshop:navigate-call") {
        navigate(...params);
      }
    }
    window.addEventListener("message", handleMessage);
    return () => window.removeEventListener("message", handleMessage);
  }, [navigate]);
  return React.createElement("script", {
    type: "module",
    dangerouslySetInnerHTML: { __html: iframeSyncScript }
  });
}
const iframeSyncScript = (
  /* javascript */
  `
if (window.parent !== window) {
	window.__kcdshop__ = window.__kcdshop__ || {};
	window.parent.postMessage(
		{ type: 'kcdshop:loaded', url: window.location.href },
		'*'
	);
	function handleMessage(event) {
		const { type, params } = event.data
		if (type === 'kcdshop:navigate-call') {
			const [distanceOrUrl, options] = params
			if (typeof distanceOrUrl === 'number') {
				window.history.go(distanceOrUrl)
			} else {
				if (options?.replace) {
					window.location.replace(distanceOrUrl)
				} else {
					window.location.assign(distanceOrUrl)
				}
			}
		}
	}

	window.addEventListener('message', handleMessage)
	window.__kcdshop__.onHydrated = function() {
		window.removeEventListener('message', handleMessage)
	};
}
`
);
export {
  KCDShopIFrameSync
};
