import { ConstantLocalStorage } from '@/utils/constant'
import { ConstantStore } from '@/utils/constant'
import { defineStore } from 'pinia'
import { reactive } from 'vue'
import { useLocalStorageState } from 'vue-hooks-plus'

export const useOpraRecordStore = defineStore(ConstantStore.OPRARECORDS, () => {

  const state = reactive<{value: API.LearnPlanRecords[]}>({value: []
  })

  // const [ state, setState ] = useLocalStorageState<API.LearnPlanRecords[]>(ConstantLocalStorage.OPRARECORDS, {
  //   defaultValue: []
  // })

  const _push = (records: any) => state.value.push(records)
  
  const _clear = () => state.value = []

  const _get = () => state.value

  const _correctQuantityTotal = () => {
    return state.value?.length && state.value?.map( item => {
      return 6 - item.wrongCount <=  0 ? 0 : 6 - item.wrongCount
    }).reduce( (pre, next) => pre + next )
  }

  const _totalQuantity = () => state.value?.length && state.value?.map( item => item.totalQuantity).reduce( (pre, next) => pre + next )

  return {
    push: _push,
    get: _get,
    clear: _clear,
    totalCorrectQuantity: _correctQuantityTotal,
    totalQuantity: _totalQuantity,
    length: state.value?.length
  }

})


