import { defineStore } from 'pinia'
import { ref } from 'vue'

export const wordSortStore = defineStore('wordSort', () => {
  //  语感单词排列部分仓库共享数据
  const isAudioEnd = ref(false) // 最后一个音频是否播放完  播放完之前不允许使用三个道具
  const initPostData = {
    costTime: 0, // 时长 单位秒
    faultCount: 0, // 错误次数
    sessionId: '',
    successCount: 0, // 	正确次数
    surrenderCount: 0, //	投降次数
  } as any
  const postData = ref(initPostData)
  const initShowData = {
    // 统计页面数据
    costTime: 0,
    costTimeStr: '',
    diamond: 0,
    exp: 0,
    rate: 0,
    wordCount: 0,
    wordTable: null,
  } as any
  const showData = ref(initShowData)

  const changeAudioFlag = (flag: boolean) => {
    isAudioEnd.value = flag
  }

  const setData = (data: anyObj) => {
    for (const key in postData.value) {
      postData.value[key] = data[key]
    }
  }

  const setShowData = (data: anyObj) => {
    for (const key in showData.value) {
      showData.value[key] = data[key]
    }
  }

  const $reset = () => {
    isAudioEnd.value = false
    postData.value = initPostData
    showData.value = initShowData
  }

  const onclear = $reset

  return {
    $reset,
    isAudioEnd,
    postData,
    showData,
    changeAudioFlag,
    setData,
    setShowData,
    onclear,
  }
})
