import type { addressItem } from '@/types/address'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAddressStore = defineStore(
  'address',
  () => {
    //地址信息
    const selectedAddress = ref<addressItem>()
    //获取地址场景
    const scene = ref('list')

    //修改地址
    const changeSelectedAddress = (address: addressItem) => {
      selectedAddress.value = address
    }

    //修改地址场景
    const changeScene = (sceneTag: string) => {
      scene.value = sceneTag
    }

    //清除选择地址
    const clearSelectedAddress = () => {
      selectedAddress.value = undefined
    }

    return {
      selectedAddress,
      scene,
      changeSelectedAddress,
      changeScene,
      clearSelectedAddress,
    }
  },
  {
    persist: {
      storage: {
        setItem(key: string, value: any) {
          uni.setStorageSync(key, value)
        },
        getItem(key: string) {
          return uni.getStorageSync(key)
        },
      },
    },
  },
)
