/**
 * useCharacterNav - 人物跳转复用方法（Vue 3 Composable）
 *
 * 使用方式：
 *   import { useCharacterNav } from "@/hooks/useCharacterNav";
 *   const { handleClickToCharacter } = useCharacterNav();
 *   handleClickToCharacter(personId);
 */
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import {  getPersonSummaryInfo } from "@/api/technologyFigures/technologyFigures";

// 人物类型名称 -> type 映射
const PERSON_TYPE_MAP = {
  "科技企业领袖": 1,
  "政府官员": 1,
  "国会议员": 2,
  "智库研究人员": 3,
};

export function useCharacterNav() {
  const router = useRouter();

  /**
   * 根据人物 ID 查询类型并跳转到人物详情页
   * @param {string|number} id - 人物 ID
   */
  const handleClickToCharacter = async (id) => {
    const personTypeList = JSON.parse(
      window.sessionStorage.getItem("personTypeList") || "[]"
    );

    const params = { personId: id };
    console.log('dsdsdwdwf',id)
    try {
      const res = await getPersonSummaryInfo(params);

      if (res.code !== 200 || !res.data) {
        ElMessage.warning("找不到当前人员的类型值！");
        return;
      }

      const matched = personTypeList.find(
        (item) => item.typeId === res.data.personType
      );

      if (!matched) {
        ElMessage.warning("找不到当前人员的类型值！");
        return;
      }

      const personTypeName = matched.typeName;
      const type = PERSON_TYPE_MAP[personTypeName];

      if (!type) {
        ElMessage.warning("找不到当前人员的类型值！");
        return;
      }

      const route = router.resolve({
        path: "/characterPage",
        query: { type, personId: id },
      });
      window.open(route.href, "_blank");
    } catch (error) {
      console.error("查询人物信息失败:", error);
    }
  };

  return { handleClickToCharacter };
}
