import { defineStore } from 'pinia';
import { listPmsProductCategory } from '@/api/pms/productCategory';
import { allBrand } from '@/api/pms/brand';
import { areaSelect } from '@/api/system/common.js';

export const useMallStore = defineStore('mall', () => {
  const state = ref({
    productCategories: [],
    brandList: [],
    areaSelect: []
  });
  const loadProductCategories = async (force: any = false) => {
    if (!force && state.value.productCategories.length > 0) {
      return Promise.resolve();
    }
    // 无全量枚举接口，暂用分页接口代替
    return listPmsProductCategory({ current: 1, size: 99999 }).then((res: any) => {
      const { records } = res.data || {};
      state.value.productCategories = records || [];
    });
  };
  const loadAreaSelect = async (force: any = false) => {
    if (!force && state.value.areaSelect.length > 0) {
      return Promise.resolve();
    }

    return areaSelect({}).then((res) => {
      state.value.areaSelect = res.data;
    });
  };
  const loadBrandList = async (force: any = false) => {
    if (!force && state.value.brandList.length > 0) {
      return Promise.resolve();
    }
    // 全量品牌接口未迁移，暂时用分页接口代替
    return allBrand({ current: 1, size: 99999 }).then((res: any) => {
      const { records } = res.data;
      state.value.brandList = records || [];
    });
  };
  return {
    state,
    loadProductCategories,
    loadAreaSelect,
    loadBrandList
  };
});
