"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 use_frame_loaded_exports = {};
__export(use_frame_loaded_exports, {
  useFrameLoaded: () => useFrameLoaded
});
module.exports = __toCommonJS(use_frame_loaded_exports);
var import_react = require("react");
var import_session = require("../../utils/session.js");
var import_host = require("../../utils/micro-frontend/host.js");
var import_micro_frontend = require("../../utils/micro-frontend/index.js");
var import_const = require("../micro-app/const.js");
const addBaseTag = (id, documentEle) => {
  const { hostUrl, manifestPath } = (0, import_micro_frontend.getMicroFrontEndAppConfig)({
    id
  });
  if (!hostUrl || !manifestPath)
    throw new Error("hostUrl or manifestPath is not defined");
  const base = documentEle.createElement("base");
  base.href = new URL(manifestPath, hostUrl).href;
  documentEle.getElementsByTagName("head")[0].appendChild(base);
};
const updateContainerId = (id, documentEle) => {
  const ele = documentEle.getElementById(import_const.APP_CONTAINER_ID_PREFIX);
  if (ele) {
    ele.id = `${ele.id}${id}`;
    ele.dataset.testid = ele.id;
  }
};
const useFrameLoaded = ({ id, documentEle }) => {
  (0, import_react.useEffect)(() => {
    if (!documentEle) return () => {
    };
    const host = import_host.CMicroAppHost.getInstance();
    addBaseTag(id, documentEle);
    updateContainerId(id, documentEle);
    const removeUserActivityListener = (0, import_session.trackActivity)(documentEle, async () => {
      await host.renewSessionTimer();
    });
    return () => {
      if (removeUserActivityListener) {
        removeUserActivityListener();
      }
    };
  }, [id, documentEle]);
};
