import { getUserAddressList } from "@/api/interfaceDocument"
import { getRequest } from "@/api/customFetch"
import { useUserStore } from "@/store/user"

const userStore = useUserStore();

export const address = ref<any[]>([])
export const defaultAddress = ref(1);

export const useMatchDefault = () => {
  const matchAddress = address.value.find(item => item.isDefault);
  if(matchAddress){
      defaultAddress.value = matchAddress.id
  }else{
      defaultAddress.value = 1
  }
}

export const useGetUserAddressList = () => {
    getRequest(getUserAddressList()).then(resp => {
        if(resp.code === 200){
            address.value = (resp.result as {list:any[]}).list
            useMatchDefault()
            userStore.setRecvAddrList(address.value)
        }
    })
}