import { defineStore } from 'pinia'

type StoreState = {
  marks: Record<any, any>
  fileInfo: Record<any, any>
  isSelectedRow: Boolean
  isSetSheetMarks: Boolean[]
  sheetBegin: Record<any, any>[]
}

export const useExcelStore = defineStore('excelStore', {
  state: (): StoreState => ({
    marks: {},
    fileInfo: {},
    isSelectedRow: false,
    isSetSheetMarks: [],
    sheetBegin: []
  }),
  getters: {
    getMarks(): Record<any, any> {
      return this.marks
    },
    getFileInfo(): Record<any, any> {
      return this.fileInfo
    },
    getIsSelectedRow(): Boolean {
      return this.isSelectedRow
    },
    getIsSetSheetMarks(): boolean[] {
      return this.isSetSheetMarks as []
    },
    getSheetBegin(): Record<any, any>[] {
      return this.sheetBegin
    }
  },
  actions: {
    setFileInfo(info: any) {
      Object.assign(this.fileInfo, info)
    },
    deleteMark(order: any, r: any, c: any) {
      const ckey = r + '_' + c
      Reflect.deleteProperty(this.marks[order], ckey)
    },
    setMark(order: any, r: any, c: any, data: any) {
      const ckey = r + '_' + c
      if (!Reflect.has(this.marks, order)) {
        Reflect.set(this.marks, order, {})
      }
      Reflect.set(this.marks[order], ckey, data)
    },
    hasMark(order: any, r: any, c: any) {
      const ckey = r + '_' + c
      if (!Reflect.has(this.marks, order) || !Reflect.has(this.marks[order], ckey)) {
        return false
      }
      return true
    },
    getMark(order: any, r: any, c: any) {
      const ckey = r + '_' + c
      if (!this.hasMark(order, r, c)) {
        return false
      }
      return Reflect.get(this.marks[order], ckey)
    },
    setIsSelectedRow(flag: boolean) {
      this.isSelectedRow = flag
    },
    initSheetBegin(length: number) {
      this.sheetBegin = Array.from({ length }).map((v) => ({})) as []
    },
    setSheetBegin(order: any, data: any) {
      this.sheetBegin[order] = data
    },
    initIsSetSheetMarks(length: number) {
      this.isSetSheetMarks = Array.from({ length }).fill(false) as boolean[]
    },
    setIsSetSheetMark(order: number) {
      this.isSetSheetMarks[order] = true
    },
    resetMarks() {
      this.marks = {}
    },
    resetFileInfo() {
      this.fileInfo = {}
    },
    resetState() {
      this.resetMarks()
    }
  }
})
