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

export const musicStatusStore = defineStore(
  'musicStatus',
  () => {
    // let musicList: any[] = []
    const musicList = ref<any[]>([])

    //设置全部音乐变化
    const saveMusic = (val: any) => {
      // musicList = val
      musicList.value = val
    }
    //根据id设置音乐音量
    const saveVolume = (id: string, volume: number) => {
      for (let i = 0; i < musicList.value.length; i++) {
        if (musicList.value[i].id == id) {
          musicList.value[i].volume = volume
        }
      }
    }
    //根据id设置音乐是否点击状态
    const saveIsclick = (id: string, isClick: boolean) => {
      for (let i = 0; i < musicList.value.length; i++) {
        if (musicList.value[i].id == id) {
          musicList.value[i].isClick = isClick
        }
      }
    }
    //根据id设置音乐对象
    const saveAudio = (id: string, audio: any) => {
      for (let i = 0; i < musicList.value.length; i++) {
        if (musicList.value[i].id == id) {
          musicList.value[i].innerAudioContext = audio
        }
      }
    }

    return { musicList, saveMusic, saveVolume, saveIsclick, saveAudio }
  },
  {
    //网页版是这样设置
    // persist: true,
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
      },
    },
  },
)
