import { DOMAIN, VITE_CONFIG_FILE_SEVER_URL } from "@/constants/constants";
import { httpNoAuth } from "@/plugins/axiosNoAuth";
import { Store } from "@/types/student-client";

export async function getElectronConfig(): Promise<Store["QECSConfig"]> {
  try {
    const res = await httpNoAuth.get(
      VITE_CONFIG_FILE_SEVER_URL +
        "/org_properties/byOrgDomain/" +
        DOMAIN +
        "/studentClientConfig.json",
      { noErrorMessage: true, "axios-retry": { retries: 3 } }
    );
    // console.log(res.data);
    res.data.ROOT_ORG_ID = JSON.parse((res.data.ROOT_ORG_ID as string) || "-1");
    res.data.IS_CUSTOM_MENU_LOGO = JSON.parse(
      (res.data.IS_CUSTOM_MENU_LOGO as string) || "false"
    );
    res.data.LOGIN_TYPE = ((res.data.LOGIN_TYPE as string) || "[]").split(",");
    res.data.PREVENT_CHEATING_CONFIG = (
      (res.data.PREVENT_CHEATING_CONFIG as string) || ""
    ).split(",");
    // console.log(res.data);

    return res.data as Store["QECSConfig"];
  } catch (e) {
    logger({
      cnl: ["console", "local", "server"],
      pgu: "AUTO",
      ejn: JSON.stringify(e),
      stk: e instanceof Error ? e.stack : "not Error object." + e,
      dtl: "获取远程配置文件出错，请重新打开程序！",
      act: "fetch studentClientConfig.json",
    });
    $message.error("获取远程配置文件出错，请重新打开程序！", {
      duration: 24 * 60 * 60 * 1000,
    });
    throw e;
  }
}
