window.ss = {
  dom: {
    TYPE: {
      INPUT: 1,
      OBJP: 2,
      DATE: 3,
      TIME: 4,
      DATETIME: 5,
      ONOFFBTN: 6,
      PIC: 7,
      HTML: 9,
      CCPSINGLE: 51, //只对应一个对象属性的，带编码规则的级联菜单，如“出生地区码”（对应省、市、区）
      CCPMUTIPLE: 52, //对应多个对象属性的级联菜单
      SEARCHINPUT: 31, //查询页用到的文本框
      SEARCHDATE: 33, //查询页用到的日期选择
      SEARCHTIME: 34, //查询页用到的时间选择
      SEARCHDATETIME: 35, //查询页用到的日期时间选择
    },
  },
  config: {},
};

window.SS = window.ss; //最终都用小写ss，暂时先兼容大写SS

(function () {
  // ========== 移动端适配：设置根元素font-size ==========
  // 设计稿宽度430px，分成10份，1rem = 43px（在430px屏幕上）
  function setRootFontSize() {
    const designWidth = 430; // 设计稿宽度
    const rootValue = designWidth / 10; // 43px
    const clientWidth =
      document.documentElement.clientWidth || window.innerWidth;

    // 计算当前屏幕对应的根font-size
    const rootFontSize = (clientWidth / designWidth) * rootValue;

    // 设置根元素font-size
    document.documentElement.style.fontSize = rootFontSize + "px";

    // 调试信息（可选）
    // console.log(`屏幕宽度: ${clientWidth}px, 根font-size: ${rootFontSize}px, 1rem = ${rootFontSize}px`);
  }

  // 立即执行
  setRootFontSize();

  // 监听窗口大小变化
  window.addEventListener("resize", setRootFontSize);

  // 监听屏幕旋转
  window.addEventListener("orientationchange", setRootFontSize);
  // ========== 移动端适配结束 ==========

  const _deps = [
    ["script", "/js/mp_jq/jquery1.11.3.min.js", ""],
    ["script", "/js/mp_vue/vue.global.js", ""],
    ["script", "/js/mp_axios/axios.min.js", ""],
    // 通用富文本依赖（PC/H5共享目录） by xu 2026-03-01
    ["style", "/skin/easy/css/jodit.css"],
    ["script", "/js/jodit/jodit.js", ""],
    ["script", "/js/mp_jweixin/jweixin-1.6.0.js", ""],
    ["script", "/js/mp_common/common.js", ""],
    ["script", "/js/mp_toast/toast.js", ""],
    ["script", "/js/mp_request/request.js", ""],
    ["script", "/js/mp_user_api/user-api.js", ""],
    ["script", "/js/mp_validator/validator-rules.js", ""],
    ["script", "/js/mp_validation/validation-manager.js", ""],
    ["script", "/js/mp_ss_components/mp-ss-components.js", ""],
    ["script", "/js/mp_bridge/bridge.js", ""],
    ["script", "/js/mp_utils/field-formatter.js", ""],
    ["script", "/js/mp_utils/navigation.js", ""],
    // 引入 Vant UI 库
    ["script", "/js/mp_vant/vant.min.js"],
    ["style", "/skin/mp_easy/vant.min.css"],

    // 引入 Day.js 日期处理库
    ["script", "/js/mp_dayjs/dayjs.min.js"],

    // 引入 Cropper.js 图片裁剪库
    ["script", "/js/mp_cropper/cropper.min.js"],
    ["style", "/skin/mp_easy/cropper.min.css"],

    // ['style', '/skin/mp_easy/base.css'],
    ["style", "/skin/mp_easy/base.css"],
    ["style", "/skin/mp_easy/iconfont.css"],
  ];

  function loadResource(type, src, module) {
    return new Promise((resolve, reject) => {
      const elem = document.createElement(
        type === "script" ? "script" : "link"
      );

      if (type === "script") {
        elem.src = src;
        elem.type = module ? "module" : "";
      } else {
        elem.rel = "stylesheet";
        elem.href = src;
      }

      elem.onload = () => {
        if (type === "script") {
          // elem.remove();
        }
        resolve();
      };

      elem.onerror = reject;
      document.head.appendChild(elem);
    });
  }

  // 等待所有依赖加载完成后再执行回调
  window.SS.ready = function (callback) {
    if (window.SS && window.SS.dom && window.SS.dom.initializeFormApp) {
      callback();
    } else {
      document.addEventListener("SSReady", () => {
        // 确保组件初始化完成后再执行回调
        if (window.SS && window.SS.dom && window.SS.dom.initializeFormApp) {
          callback();
        }
      });
    }
  };

  async function loadAll() {
    // 然后加载资源
    for (const [type, src, module] of _deps) {
      await loadResource(type, src, module);
      if (src.includes("ace.js")) {
        window.ace.require("ace/config").set("workerPath", null);
      }
    }

    // 所有 H5 页面公共依赖加载完成后，先判断是否需要做消息直达登录分流。
    // 如果这里已经跳去了 login/autoLogin，就直接终止当前页面初始化，避免业务页先渲染再被重定向。
    if (typeof window.handleMessageEntryAuthRedirect === "function") {
      const redirected = window.handleMessageEntryAuthRedirect();
      if (redirected) {
        return;
      }
    }

    document.dispatchEvent(new Event("SSReady"));
  }

  // 当 DOM 加载完成后执行清理和加载
  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", loadAll);
    console.log("DOMContentLoaded");
  } else {
    loadAll();
  }
})();
