export const writeOpenWindowStuff = async (theWindow, title) => {
  let url = `${addonhost}?fileName=popup.js`;

  if (useLocal == true) url = "https://localhost:3002/popup.js";

  theWindow.document.write(
    `<html><head><title>${title}</title><script> async function loadIt() { let m = await import("${url}"); } loadIt(); </script></head><body><div id="app-child"></div></body></html>`,
  );
  theWindow.document.title = title;
};

export const showPopupMessage = async (data) => {
  const left = window.screenLeft ?? 0;
  const top = window.screenTop ?? 0;
  const windowTitle = `${appName} Title`;
  const openWindow = window.open(
    "",
    "",
    `left=${left},top=${top},width=700,height=500,location=no,menubar=no,status=no,toolbar=no`,
  );

  const onClose = (closeData) => {
    if (openWindow) {
      openWindow.close();
    }
  };

  if (openWindow) {
    openWindow.bridge = {
      data,
      onClose,
    };
    openWindow.signalLoaded = () => {
      if (openWindow && openWindow.setupelli) {
        openWindow.setupelli(elli);
      }
    };
    await writeOpenWindowStuff(openWindow, windowTitle);
  }
};
