import { defineStore } from 'pinia'
import { getProductDetailApi, getProductListApi } from '@/service/product'
import { until } from '@vueuse/core'

export const useProductStore = defineStore(
  'product',
  () => {
    // 获取产品列表
    const getProductList = async (productsmerchantid: string) => {
      const { data: productList, loading } = useRequest(
        () => getProductListApi({ productsmerchantid }, { pageindex: 1, rows: 999 }),
        {
          immediate: true,
        },
      )

      await until(loading).toBe(false)
      return productList.value.Data
    }

    // 获取产品详情
    const getProductDetail = async ({ productsid, merchantid }) => {
      const { data: productDetail, loading } = useRequest(
        () => getProductDetailApi({ productsid, merchantid }),
        {
          immediate: true,
        },
      )

      await until(loading).toBe(false)
      return productDetail.value
    }

    return {
      getProductList,
      getProductDetail,
    }
  },
  {
    persist: true,
  },
)
