var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/modules/ProductList/index.ts
var ProductList_exports = {};
__export(ProductList_exports, {
  ProductList: () => ProductList
});
module.exports = __toCommonJS(ProductList_exports);
var import_BaseModule = require("../BaseModule");
var import_lodash_es = require("lodash-es");
__reExport(ProductList_exports, require("./types"), module.exports);
var ProductList = class extends import_BaseModule.BaseModule {
  constructor(name, version) {
    super(name, version);
    this.defaultName = "productList";
    this.defaultVersion = "1.0.0";
    this.otherParams = {};
  }
  async initialize(core, options) {
    var _a;
    this.core = core;
    this.store = options.store;
    this.otherParams = options.otherParams || {};
    if (Array.isArray((_a = options.initialState) == null ? void 0 : _a.list)) {
      this.store.list = options.initialState.list.slice().sort((a, b) => Number(b.sort) - Number(a.sort));
      this.core.effects.emit(`${this.name}:changed`, this.store.list);
    } else {
      this.store.list = [];
      this.store.selectProducts = [];
    }
    this.request = core.getPlugin("request");
  }
  async storeChange(path, value) {
  }
  async loadProducts({
    category_ids = [],
    product_ids = [],
    collection = [],
    menu_list_ids = [],
    customer_id: paramsCustomerId,
    with_count = [],
    schedule_datetime,
    schedule_date,
    cacheId,
    with_schedule
  }, options) {
    var _a, _b;
    let userPlugin = this.core.getPlugin("user");
    let customer_id = void 0;
    try {
      customer_id = paramsCustomerId || ((_a = userPlugin == null ? void 0 : userPlugin.get()) == null ? void 0 : _a.id);
    } catch (error) {
      console.error(error);
    }
    const productsData = await this.request.post(
      `/product/query`,
      {
        open_quotation: 1,
        open_bundle: 0,
        exclude_extension_type: [
          "product_party",
          "product_event",
          "product_series_event",
          "product_package_ticket",
          "ticket",
          "event_item"
        ],
        with: ["category", "collection", "resourceRelation"],
        status: "published",
        num: 500,
        skip: 1,
        customer_id,
        category_ids,
        ids: product_ids,
        collection,
        front_end_cache_id: cacheId,
        menu_list_ids,
        with_count,
        // client_schedule_ids: schedule_ids,
        schedule_date,
        application_code: (_b = this.otherParams) == null ? void 0 : _b.channel,
        is_eject: 1,
        with_schedule,
        schedule_datetime
      },
      { osServer: true, callback: options == null ? void 0 : options.callback, subscriberId: options == null ? void 0 : options.subscriberId, customToast: () => {
      } }
    );
    const sortedList = (productsData.data.list || []).slice().sort((a, b) => Number(b.sort) - Number(a.sort));
    this.addProduct(sortedList);
    return sortedList;
  }
  async loadProductsPrice({
    ids = [],
    customer_id,
    schedule_date,
    channel
  }) {
    const productsData = await this.request.post(
      `/product/query/price`,
      {
        ids,
        customer_id,
        schedule_date,
        channel
      },
      { useCache: true }
    );
    return productsData.data;
  }
  async getProducts() {
    await this.core.effects.emit(
      `${this.name}:onGetProducts`,
      this.store.list
    );
    return (0, import_lodash_es.cloneDeep)(this.store.list);
  }
  async getProduct(id) {
    await this.core.effects.emit(
      `${this.name}:onGetProduct`,
      this.store.list
    );
    const product = this.store.list.find((product2) => product2.id === id);
    return product ? (0, import_lodash_es.cloneDeep)(product) : void 0;
  }
  async getProductByIds(ids) {
    await this.core.effects.emit(
      `${this.name}:onGetProductByIds`,
      this.store.list
    );
    const products = this.store.list.filter((product) => ids.includes(product.id));
    return products;
  }
  async addProduct(products) {
    if (!this.store.list) {
      this.store.list = [];
    }
    products.forEach((n) => {
      const index = this.store.list.findIndex((m) => m.id === n.id);
      if (index === -1) {
        this.store.list.push(n);
      } else {
        this.store.list[index] = n;
      }
    });
    this.store.list.sort((a, b) => Number(b.sort) - Number(a.sort));
    this.core.effects.emit(`${this.name}:changed`, this.store.list);
  }
  async selectProducts(products) {
    this.store.selectProducts = products;
  }
  /**
   * 根据商品编码或条码搜索商品
   * @param code 商品编码或条码
   * @returns 匹配的商品列表，如果没有匹配则返回空数组
   */
  findProductsByCodeOrBarcode(code) {
    if (!code || typeof code !== "string") {
      return [];
    }
    const trimmedCode = code.trim();
    if (!trimmedCode) {
      return [];
    }
    const matchingProducts = this.store.list.filter((product) => {
      return product.code && product.code.trim() === trimmedCode || product.barcode && product.barcode.trim() === trimmedCode;
    });
    return (0, import_lodash_es.cloneDeep)(matchingProducts);
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ProductList,
  ...require("./types")
});
