// 每一个存储的模块，命名规则use开头，store结尾
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { removeLocal, setLocal } from '@/utils/local'

export const useUserInfoStore = defineStore('userInfo', () => {
  const state = ref({
    userInfo: {},
    customerId: '',
    token: '',
    generateParams: {
      stylistId: '',
      sex: ''
    }
  })

  // getters
  const getUserInfo = computed(() => state.value.userInfo)

  // actions
  const setUserInfo = (data: any) => {
    state.value.userInfo = data
  }

  const setCustomerId = (data: string) => {
    state.value.customerId = data
  }

  const setToken = (data: string) => {
    state.value.token = data
    setLocal(data, 'token')
  }

  const getGenerateParams = () => {
    return state.value.generateParams
  }

  const setGenerateParams = (data: any) => {
    state.value.generateParams = data
  }

  const resetGenerateParams = () => {
    state.value.customerId = ''
    state.value.generateParams = {
      stylistId: '',
      sex: ''
    }
  }

  const logOut = () => {
    // 处理退出登录的一些逻辑
    return new Promise((resolve) => {
      state.value.token = ''
      state.value.userInfo = {}
      state.value.customerId = ''
      removeLocal('token')
      resetGenerateParams()
      resolve('')
    })
  }

  return {
    state,
    getUserInfo,
    setToken,
    setUserInfo,
    setCustomerId,
    setGenerateParams,
    getGenerateParams,
    resetGenerateParams,
    logOut
  }
})
