"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var utils_exports = {};
__export(utils_exports, {
  applicationScriptingObject: () => applicationScriptingObject,
  authScriptingObject: () => authScriptingObject,
  postMessage: () => postMessage,
  setupHost: () => setupHost
});
module.exports = __toCommonJS(utils_exports);
var import_microfe_common = require("@elliemae/microfe-common");
const applicationScriptingObject = {
  object: {
    events: [],
    functions: [
      "closeModal",
      "extend",
      "getApplicationContext",
      "getCapabilities",
      "getCompanySettings",
      "getDescriptor",
      "getPersonaAccess",
      "getPoliciesDetails",
      "getUserAccessRights",
      "hideSpinner",
      "keepSessionAlive",
      "log",
      "navigate",
      "open",
      "openModal",
      "performAction",
      "print",
      "showError",
      "showSpinner",
      "supportsAction",
      "supportsNavigateTo"
    ],
    objectId: "application",
    objectType: "Object"
  },
  type: "object"
};
const authScriptingObject = {
  object: {
    events: [],
    functions: ["getAccessToken", "createAuthCode", "getUser"],
    objectId: "auth",
    objectType: "Object"
  },
  type: "object"
};
const hostObjects = {
  application: applicationScriptingObject,
  auth: authScriptingObject
};
const getOrigin = (url) => {
  if (!url || url === "about:blank") return "*";
  try {
    const { origin } = new URL(url);
    return origin === "null" || !origin ? url : origin;
  } catch (e) {
    const { origin } = new URL(url, document.baseURI);
    return origin;
  }
};
const postMessage = ({
  srcWindow,
  targetWindow,
  message
}) => {
  targetWindow.dispatchEvent(
    new MessageEvent("message", {
      source: srcWindow,
      origin: getOrigin(srcWindow.location.href),
      // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
      data: {
        source: "elli:remoting",
        ...message
      }
    })
  );
};
const setupHost = (hostWindow) => {
  const onMessage = ({ data }) => {
    if (data?.type !== import_microfe_common.MessageType.ObjectGet) return;
    const scriptingObject = hostObjects[data?.body?.objectId ?? ""] ?? null;
    if (scriptingObject)
      postMessage({
        srcWindow: hostWindow,
        targetWindow: window,
        message: {
          body: scriptingObject,
          type: "elli:remoting:response",
          requestId: data.requestId
        }
      });
  };
  hostWindow?.addEventListener?.("message", onMessage);
  return () => hostWindow?.removeEventListener?.("message", onMessage);
};
