import { ErrorPageNameMap, PageEnum } from "../enums/pageEnum.mjs";
import { docPath, giteeSourceCodePath } from "../settings/pathConst.mjs";
import "../../node_modules/.pnpm/crypto-js@4.1.1/node_modules/crypto-js/index.mjs";
import { StorageEnum } from "../enums/storageEnum.mjs";
import { clearLocalStorage } from "./storage.mjs";
import router from "../router/index.mjs";
const routerTurnByName = (pageName, isReplace, windowOpen) => {
  if (windowOpen) {
    const path = fetchPathByName(pageName, "href");
    openNewWindow(path);
    return;
  }
  if (isReplace) {
    router.replace({
      name: pageName
    });
    return;
  }
  router.push({
    name: pageName
  });
};
const fetchPathByName = (pageName, p) => {
  try {
    const pathData = router.resolve({
      name: pageName
    });
    return p ? pathData[p] : pathData;
  } catch (error) {
    window["$message"].warning("查询路由信息失败，请联系管理员！");
  }
};
const routerTurnByPath = (path, query, isReplace, windowOpen) => {
  let fullPath = "";
  if (query == null ? void 0 : query.length) {
    fullPath = `${path}/${query.join("/")}`;
  }
  if (windowOpen) {
    return openNewWindow(fullPath);
  }
  if (isReplace) {
    router.replace({
      path: fullPath
    });
    return;
  }
  router.push({
    path: fullPath
  });
};
const redirectErrorPage = (code) => {
  if (!code)
    return false;
  const pageName = ErrorPageNameMap.get(code);
  if (!pageName)
    return false;
  routerTurnByName(pageName);
};
const reloadRoutePage = () => {
  routerTurnByName(PageEnum.RELOAD_NAME);
};
const logout = () => {
  clearLocalStorage(StorageEnum.GO_LOGIN_INFO_STORE);
  routerTurnByName(PageEnum.BASE_LOGIN_NAME);
};
const openNewWindow = (url) => {
  return window.open(url, "_blank");
};
const openDoc = () => {
  openNewWindow(docPath);
};
const openGiteeSourceCode = () => {
  openNewWindow(giteeSourceCodePath);
};
const isPreview = () => {
  return document.location.hash.includes("preview");
};
const fetchRouteParamsLocation = () => {
  try {
    return document.location.hash.split("?")[0].split("/").pop() || "";
  } catch (error) {
    window["$message"].warning("查询路由信息失败，请联系管理员！");
    return "";
  }
};
export {
  fetchPathByName,
  fetchRouteParamsLocation,
  isPreview,
  logout,
  openDoc,
  openGiteeSourceCode,
  openNewWindow,
  redirectErrorPage,
  reloadRoutePage,
  routerTurnByName,
  routerTurnByPath
};
