import { Local } from '../utils/storage.mjs';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '../stores/themeConfig.mjs';
import img from '../theme-chalk/assets/logo.png.mjs';
import { updateIdleTimeout } from './idleTimeout.mjs';
import '../api/index.mjs';
import { feature } from '../api/sys/index.mjs';
import { useBaseApi } from '../api/base/index.mjs';

async function loadSysInfo(tenantid) {
  var _a;
  const storesThemeConfig = useThemeConfig();
  const { themeConfig } = storeToRefs(storesThemeConfig);
  const [err, res] = await feature(useBaseApi("sysTenant").gcomm(null, "sysInfo/" + Number(tenantid)));
  if (err) {
    themeConfig.value.logoUrl = img;
    Local.remove("themeConfig");
    Local.set("themeConfig", storesThemeConfig.themeConfig);
    return;
  } else {
    if (res.data.type != "success" || res.data.result == null) return;
    const data = res.data.result;
    themeConfig.value.logoUrl = data.logo;
    themeConfig.value.globalTitle = data.title;
    themeConfig.value.globalViceTitle = data.viceTitle;
    themeConfig.value.globalViceTitleMsg = data.viceDesc;
    themeConfig.value.icp = data.icp;
    themeConfig.value.icpUrl = data.icpUrl;
    themeConfig.value.isWatermark = data.watermark != null;
    themeConfig.value.watermarkText = data.watermark;
    themeConfig.value.copyright = data.copyright;
    themeConfig.value.version = data.version;
    themeConfig.value.carouselFiles = data.carouselFiles;
    themeConfig.value.primary = data.themeColor;
    themeConfig.value.layout = data.layout;
    themeConfig.value.animation = data.animation;
    themeConfig.value.secondVer = data.secondVer;
    themeConfig.value.captcha = data.captcha;
    themeConfig.value.adminTel = data.adminTel;
    themeConfig.value.forceChangePassword = data.forceChangePassword;
    themeConfig.value.passwordExpirationTime = data.passwordExpirationTime;
    themeConfig.value.i18NSwitch = data.i18NSwitch;
    themeConfig.value.idleTimeout = data.idleTimeout;
    themeConfig.value.onlineNotice = data.onlineNotice;
    themeConfig.value.mobileLogin = data.mobileLogin;
    themeConfig.value.isExpireTip = data.isExpireTip;
    themeConfig.value.expireDay = data.expireDay;
    themeConfig.value.scanLogin = data.scanLogin;
    themeConfig.value.signUp = data.signUp;
    window.__env__.VITE_SM_PUBLIC_KEY = data.publicKey;
    updateFavicon(data.logo);
    updateIdleTimeout((_a = themeConfig.value.idleTimeout) != null ? _a : 0);
    Local.remove("themeConfig");
    Local.set("themeConfig", storesThemeConfig.themeConfig);
  }
}
const updateFavicon = (url) => {
  const favicon = document.getElementById("favicon");
  favicon.href = url ? url : "data:;base64,=";
};

export { loadSysInfo, updateFavicon };
