import "../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                       */
/* empty css                                                           */
/* empty css                                                               */
/* empty css                                                            */
/* empty css                                                            */
/* empty css                                                          */
/* empty css                                      */
/* empty css                                       */
import { getCurrentInstance, ref, provide, onMounted, createBlock, createCommentVNode, unref, openBlock, withCtx } from "vue";
import MobilePage from "./page.vue.mjs";
import api from "./api/index.mjs";
import emitter from "./utils/eventBus.mjs";
import { pageHistory } from "./utils/pageHistory.mjs";
import { findPageById } from "./utils/common.mjs";
/* empty css                                                           */
/* empty css                                                             */
/* empty css            */
import _export_sfc from "../../../_virtual/_plugin-vue_export-helper.mjs";
import { ElContainer } from "../../../node_modules/element-plus/es/components/container/index.mjs";
import { ElMessage } from "../../../node_modules/element-plus/es/components/message/index.mjs";
import { Empty } from "../node_modules/vant/es/empty/index.mjs";
const _sfc_main = {
  __name: "index",
  props: {
    appId: {
      type: String,
      default: ""
    },
    pageKey: {
      type: String,
      default: ""
    }
  },
  setup(__props) {
    const props = __props;
    const currentInstance = getCurrentInstance();
    const ctx = currentInstance.proxy;
    const pageList = ref({});
    const configLoading = ref(true);
    const noTargetPage = ref(false);
    const loadConfig = async () => {
      var _a, _b;
      if (!props.appId) {
        return;
      }
      configLoading.value = true;
      try {
        let option = await api.getAppById(ctx.$http, props.appId);
        pageList.value = ((_a = JSON.parse(option.pageConfig)) == null ? void 0 : _a.pageConfig) || [];
        pageHistory.clear();
        let homePage;
        if (props.pageKey) {
          homePage = pageList.value.find((page) => page.isHome);
          if (!homePage) {
            noTargetPage.value = true;
          }
        } else {
          homePage = pageList.value.find((page) => page.isHome);
          if (!homePage) {
            homePage = (_b = pageList.value) == null ? void 0 : _b[0];
          }
        }
        let pageId = homePage.id;
        if (homePage.footerType == "tabbar") {
          pageId = homePage.tabbarConfig.tabbarOptions[0].pageId;
        }
        loadPage(pageId);
      } catch (error) {
        console.error("加载配置失败:", error);
        ElMessage.error("加载配置失败");
      } finally {
        configLoading.value = false;
      }
    };
    const currentPage = ref(null);
    const routePage = ref(null);
    function loadPage(pageId) {
      var _a;
      if (((_a = currentPage.value) == null ? void 0 : _a.id) == pageId) {
        return;
      }
      const targetPage = findPageById(pageList.value, pageId);
      if (targetPage) {
        if (targetPage.parentPage) {
          currentPage.value = targetPage.parentPage;
          routePage.value = targetPage;
        } else {
          currentPage.value = targetPage;
          routePage.value = null;
        }
        pageHistory.push(targetPage);
        noTargetPage.value = false;
      } else {
        noTargetPage.value = true;
        currentPage.value = null;
        routePage.value = null;
      }
    }
    const pageParams = ref({});
    emitter.on("loadPage", (pageId, params) => {
      loadPage(pageId);
      pageParams.value = params || { applyId: "7a16522c29e047d1be80e884773a87ac" };
    });
    provide("pageList", pageList);
    onMounted(() => {
      loadConfig();
    });
    return (_ctx, _cache) => {
      const _component_van_empty = Empty;
      const _component_el_container = ElContainer;
      return !unref(configLoading) ? (openBlock(), createBlock(_component_el_container, {
        key: 0,
        direction: "vertical",
        class: "fec-mobile-app",
        ref: "fecMobileAppRef"
      }, {
        default: withCtx(() => [
          !unref(noTargetPage) ? (openBlock(), createBlock(unref(MobilePage), {
            key: 0,
            parentPage: unref(currentPage),
            routePage: unref(routePage),
            params: unref(pageParams)
          }, null, 8, ["parentPage", "routePage", "params"])) : (openBlock(), createBlock(_component_van_empty, {
            key: 1,
            image: "search",
            description: "页面找不到了"
          }))
        ]),
        _: 1
      }, 512)) : createCommentVNode("", true);
    };
  }
};
const MobileApp = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-4e4cf5f1"]]);
export {
  MobileApp as default
};
