import { LocalStorageStateEnum } from "@/enum/localStore"
import { ConstantStoreEnum } from "@/enum/store"
import { defineStore } from "pinia"
import { useLocalStorageState } from 'vue-hooks-plus'
import { reactive  } from 'vue'


export const usePracticeStore = defineStore(ConstantStoreEnum.PRACTICE, () => {

  let appPracticeStore = reactive<Record<string, string[]>>({})

  const [ practice, setPractice ] = useLocalStorageState<Record<string, string[]>>(LocalStorageStateEnum.PRACTICE)

  appPracticeStore = practice.value!

  /**
   * 获取全部题卡id与已经练过的题卡id的差集
   * @param collectionId 题集id
   * @param cardIds 题集下的题卡id数组
   * @returns 全部题卡id与已经练过的题卡id的差集
   */
  const get = (collectionId: string, cardIds: string[]) => {
    
    const olds = practice.value![collectionId] || []

    const diffIds = cardIds.filter( cardId => !olds.includes(cardId))

    if (diffIds.length === 0 || diffIds.length == cardIds.length) {
      setPractice({
        ...practice.value,
        [collectionId]: []
      })
      return cardIds
    } else {
      return diffIds
    }
  }

  const set = (collectionId: string, cardId: string) => {
    practice.value![collectionId].push(cardId)
  }

  const nextId = (cardIds: string[]) => {
    const randomCount = Math.floor(Math.random() * cardIds.length)
    return cardIds[randomCount]
  }
  
  // get (stage) 获取collectionIds 的请求也在这个里边来请求

  // submit stage cardId

  // current stage

  // getNextCardId 

  // if all sumit 清空题卡

  return {
    get,
    set,
    nextId
  }

})


