import { ref, type Ref } from "vue";
import { defineStore } from "pinia";
import * as kgService from "@/services/knowledgeGraph.service";
import * as searchService from "@/services/search.service";
import type { Property } from "@/types/knowledgeGraph.types";
import type { SearchResult } from "@/types/search.types";
import { message } from "ant-design-vue";

export const useKnowledgeGraphStore = defineStore('knowledgeGraph', () => {
  const property: Ref<Property> = ref({} as Property);

  const searchResult: Ref<SearchResult | null> = ref(null);

  function fetchGraph(query?: string) {
    // TODO
    message.info('根据查询条件' + query + '查询图数据');
    return new Promise((resolve) => {
      resolve('TOTO');
    });
  }

  /**
   * 根据学科ID查询学科属性
   * @param id 学科ID
   */
  function fetchProperty(id: number) {
    kgService.fetchProperty(id).then((resp) => {
      property.value = resp.data.data;
    })
  }

  /**
   * 根据学科名称搜索文献
   * @param name 学科名称
   */
  function search(name: string) {
    searchService.search({query: `TP=("${name}")`}).then((resp) => {
      searchResult.value = resp;
    });
  }

  return {
    property,
    searchResult,
    fetchGraph,
    fetchProperty,
    search,
  }
});
