import { defineStore } from "pinia";
import { SHOP_CART_LIST, SHOP_ORDER_STATUS, CONTENT_FLOAT, SHOP_ADDRESS_LIST, SELLER_CART_LIsts } from "@/api";
import { setStorage, getStorage } from '@/utils'

const state = () => ({
  cartList: [],
  cartNum: 0,
  sellerList: [],
  sellerNum: 0,

  orderStatus: [],
  hotLink: [],
  addressList: [],
  logistics: {},
  tabParams: null
})
export const useShopStore = defineStore('shop', {
  state,
  getters: {
    getCartList() {
      return this.cartList
    },
    getCartNum() {
      return this.cartNum
    },
    getSellerList() {
      return this.sellerList
    },
    getSellerNum() {
      return this.sellerNum
    },
    getOrderStatus() {
      return this.orderStatus
    },
    getHotLink() {
      // if (!this.hotLink.length) {
      //   this.setHotLink();
      // }
      return this.hotLink || [];
    },
    getAddressList() {
      return this.addressList || [];
    },
    getLogistics() {
      return this.logistics || {}
    },
    getTabParams() {
      return this.tabParams || null
    }
  },
  actions: {
    async setCartList(is_free) {
      try {
        const { data: { list = [], cartCount = 0 } } = await SHOP_CART_LIST(is_free);
        this.cartList = list || [];
        this.cartNum = cartCount;
      } catch (error) { }
    },
    setCartNum(_num_) {
      this.cartNum = _num_
    },
    async setSellerList() {
      try {
        const { data: { list = [], cartCount = 0 } } = await SELLER_CART_LIsts();
        this.sellerList = list || [];
        this.sellerNum = cartCount;
      } catch (error) { }
    },
    setSellerNum(_num_) {
      this.sellerNum = _num_
    },
    async setOrderStatus() {
      try {
        const res = await SHOP_ORDER_STATUS();
        let arr = Object.entries(res.data).map(([key, value]) => ({
          status: key,
          value: value
        }));
        this.orderStatus = arr
      } catch (error) { }
    },
    async setHotLink() {
      try {
        const res = await CONTENT_FLOAT({
          type: 'image-text',
          posititon: 'search'
        })
        this.hotLink = res.data
      } catch (error) { }
    },
    async setAddressList() {
      try {
        const res = await SHOP_ADDRESS_LIST()
        this.addressList = res.data
      } catch (error) { }
    },
    setLogistics(data) {
      this.logistics = data
    },
    setTabParams(params) {
      this.tabParams = params;
    }
  }
})