import { defineStore } from "pinia"
import { useRoute, useRouter } from 'vue-router'
import { getContentHelpApi } from '@/apis/article'



export interface ILinkInfo {
  links: string;
  title: string;
  id: string | number;
}

export interface IData {
  id: number;
  pid: number;
  name: string;
  sort: number;
  code: string | null;
  lists: Array<ILinkInfo>; 
}

// store state
export interface IFooterState {
  linkList: Array<IData>;
  activeLinkId: string | number;
  activeLinkInfo: ILinkInfo | null;
  activeLinkCateInfo: IData | null;
}



const useFooterStores = defineStore('footer', {
  state: (): IFooterState => ({
    linkList: [],
    activeLinkId: '',
    activeLinkInfo: null,
    activeLinkCateInfo: null,
  }),

  getters: {
    activeInfo(): ILinkInfo | null {
      if (this.activeLinkInfo) return this.activeLinkInfo
      if (this.activeLinkCateInfo && this.activeLinkId) {
        const result = this.activeLinkCateInfo.lists.find((info: ILinkInfo) => (info.id == this.activeLinkId))
        return result ? result : null
      }
      return null
    }
  },

  actions: {

    // 获取数据列表
    fetchHelpDatas(lang: string) {
      const route = useRoute()
      const cateId = route.query.cateId ? route.query.cateId : ''
      const id = route.query.id ? route.query.id : ''

      switch(lang) {
        case 'zh-CN':
          lang = 'zh'; break;
        default:
          break;
      }
  
      getContentHelpApi({ lang })
        .then((result) => {
          this.linkList = result.data
          console.log('this.linkList', this.linkList)
          if (this.linkList.length > 0) {
            if (cateId) {
              const cateInfo: IData | undefined = this.linkList.find((item: IData) => (`${item.id}` == cateId))
              const linkInfo: ILinkInfo | undefined = cateInfo ? cateInfo.lists.find((info: ILinkInfo) => (`${info.id}` == id)) : undefined
              this.updateActiveInfo(cateInfo ? cateInfo : null, linkInfo ? linkInfo : null)
            }
            else if (id) {
              let linkInfo: ILinkInfo | null = null
              for (let i = 0; i < this.linkList.length; i++) {
                for (let j = 0; j < this.linkList[i].lists.length; j++) {
                  if (`${this.linkList[i].lists[j].id}` == id) {
                    linkInfo = this.linkList[i].lists[j]
                    break
                  }
                }
                if (linkInfo) {
                  break
                }
              }
              this.updateActiveInfo(null, linkInfo)
            }
          }
        })
        .catch((err) => {
  
        })
    },

    // 更新 active 信息
    updateActiveInfo(cateInfo: IData | null, linkInfo: ILinkInfo | null, callback?: Function) {
      if (!linkInfo) {
        this.activeLinkId = ''
        this.activeLinkInfo = null
        this.activeLinkCateInfo = null
      }
      else if (linkInfo.links) {
        console.log(linkInfo.links)
        window.open(linkInfo.links, '__blank')
        return
      }
      else {
        this.activeLinkId = linkInfo.id
        this.activeLinkInfo = linkInfo
        this.activeLinkCateInfo = cateInfo
      }
      callback && callback()
    }
  }
})




export default useFooterStores