"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_app_will_render_exports = {};
__export(use_app_will_render_exports, {
  useAppWillRender: () => useAppWillRender
});
module.exports = __toCommonJS(use_app_will_render_exports);
var import_react = require("react");
var import_lodash = require("lodash");
var import_react_redux = require("react-redux");
var import_actions = require("../../data/wait-message/actions.js");
var import_app_factory = require("./app-factory/index.js");
var import_micro_frontend = require("../../utils/micro-frontend/index.js");
var import_log_records = require("../../utils/log-records.js");
const useAppWillRender = ({
  id,
  uuid,
  documentEle,
  history,
  theme,
  homeRoute,
  onUnloadComplete
}) => {
  const dispatch = import_react_redux.useDispatch ? (0, import_react_redux.useDispatch)() : null;
  const ref = (0, import_react.useRef)(Promise.resolve());
  const getConfig = (0, import_react.useCallback)(
    () => (0, import_micro_frontend.getMicroFrontEndAppConfig)({
      id
    }),
    [id]
  );
  const unload = (0, import_react.useCallback)(
    async (appParams, loadFailed = false) => {
      await (0, import_app_factory.unmountApp)(appParams);
      (0, import_app_factory.unloadApp)(appParams);
      if (!loadFailed && onUnloadComplete) onUnloadComplete();
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    []
  );
  const load = (0, import_react.useCallback)(
    async (appParams) => {
      if (dispatch) dispatch(import_actions.waitMessage.open());
      try {
        if (ref.current) await ref.current;
        await (0, import_app_factory.loadApp)(appParams);
        await (0, import_app_factory.mountApp)(appParams);
      } catch (ex) {
        (0, import_micro_frontend.getLogger)().error({
          ...import_log_records.logRecords.APP_CONFIG_LOAD_FAILED,
          exception: ex
        });
        await unload(appParams, true);
        throw ex;
      }
      if (dispatch) dispatch(import_actions.waitMessage.close());
    },
    [dispatch, unload]
  );
  (0, import_react.useLayoutEffect)(() => {
    if (!documentEle) return () => {
    };
    const appConfig = (0, import_lodash.merge)(getConfig(), {
      documentEle,
      history,
      theme,
      homeRoute
    });
    const appParams = { ...appConfig, uuid };
    load(appParams);
    return () => {
      ref.current = unload(appParams);
    };
  }, [documentEle, getConfig, history, theme, homeRoute, load, unload, uuid]);
};
