import {
  relicsInfoFetch,
  // relicsPolyginsFetch,
  relicsSelfCheckFetch,
  updateRelicsFetch,
} from "@/request";
import { errorHook } from "@/request/state";
import { ref } from "vue";
import { Relics } from "@/request/type";
import { refreshScenes } from "./scene";
import { router } from '../router'

export type { Relics } from "@/request/type";
export const relics = ref<Relics>();

errorHook.push((code) => {
  if (code === 4002) {
    // (window as any).router = router
    setTimeout(() => {
      router.replace({ name: "no-persession" });
    }, 500)
    return
    // debugger
  }
});

export const initRelics = async (relicsId: number) => {
  relics.value = await relicsInfoFetch(relicsId);
  if (relics.value) {
    await refreshScenes();
  }
};
export const initSelfRelics = async (relicsId: number, isEdit = false) => {
  console.log('isEditMode', isEdit)
  isEdit && await relicsSelfCheckFetch(relicsId);
  relics.value = await relicsInfoFetch(relicsId);
  if (relics.value) {
    await refreshScenes();
  }
};
export const updateRelicsName = async (name: string) => {
  await updateRelicsFetch({ ...relics.value!, name });
  relics.value!.name = name;
};

export const relicsLevelDesc = {
  0: "全国重点文物保护单位",
  1: "省级文物保护单位",
  2: "市级和县级文物保护单位",
  3: "尚未核定公布为文物保护单位的不可移动文物",
  4: "未认定",
};

export const relicsTypeDesc = {
  0: "古文化遗址",
  1: "古墓葬",
  2: "古建筑",
  3: "石窟寺及石刻",
  4: "近现代重要史迹及代表性建筑",
  5: "其它",
};

export const creationMethodDesc = {
  1: "手动",
  2: "自动",
};
