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/solution/BuyTickets/index.ts
var BuyTickets_exports = {};
__export(BuyTickets_exports, {
  BuyTicketsImpl: () => BuyTicketsImpl
});
module.exports = __toCommonJS(BuyTickets_exports);
var import_modules = require("../../modules/");
var import_BaseModule = require("../../modules/BaseModule");
__reExport(BuyTickets_exports, require("./types"), module.exports);
var BuyTicketsImpl = class extends import_BaseModule.BaseModule {
  constructor(name, version) {
    super(name, version);
    // 提供给 baseModule 用的默认名称 和 默认版本
    this.defaultName = "buyTickets";
    this.defaultVersion = "1.0.0";
    this.isSolution = true;
    this.otherParams = {};
  }
  async initialize(core, options) {
    this.core = core;
    this.store = options.store || {};
    this.otherParams = options.otherParams || {};
    console.log("[BuyTickets] 初始化完成");
    this.request = core.getPlugin("request");
    this.window = core.getPlugin("window");
    if (!this.request) {
      throw new Error("购物车模块需要 request 插件支持");
    }
    if (this.otherParams.prodctIds) {
      await this.loadProductsByIds(this.otherParams.prodctIds);
    }
    await this.core.effects.emit(`${this.name}:onInited`, {});
  }
  // 通过 ids 获取产品，在当前解决方案下，获取到的就是主商品
  async loadProductsByIds(ids) {
    const productsData = await this.request.post("/product/query", {
      ids,
      open_quotation: 1,
      open_bundle: 1,
      with: ["bundleGroup.bundleItem"],
      status: "published",
      num: 10,
      skip: 1
    });
    const mainProducts = new import_modules.ProductList(`product-kiosk-main-list`);
    this.core.registerModule(mainProducts, {
      initialState: {
        list: productsData.data.list
      }
    });
    this.store.mainProducts = mainProducts;
    await this.core.effects.emit(`${this.name}:onMainProductsLoaded`, {});
  }
  // 通过 ids 获取产品，在当前解决方案下，获取到的就是其他商品
  async loadProductsByCategory(categoryId) {
  }
  async destroy() {
    await this.core.effects.emit(`${this.name}:onDestroy`, {});
    console.log("[BuyTickets] 已销毁");
  }
  // 获取当前绑定的所有商品
  async getProducts() {
    var _a;
    const mainData = await this.store.mainProducts.getProducts() || [];
    const otherData = await ((_a = this.store.otherProducts) == null ? void 0 : _a.getProducts()) || [];
    await this.core.effects.emit(`${this.name}:onGetProducts`, [
      ...mainData,
      ...otherData
    ]);
    return [...mainData, ...otherData];
  }
  // 根据 id 获取商品详情，他会尝试先去主列表里找，找不到再去子列表，还找不到就报错
  async getProduct(id) {
    var _a, _b;
    const mainProduct = await ((_a = this.store.mainProducts) == null ? void 0 : _a.getProduct(id));
    const otherProduct = await ((_b = this.store.otherProducts) == null ? void 0 : _b.getProduct(id));
    if (mainProduct)
      return mainProduct;
    if (otherProduct)
      return otherProduct;
    throw new Error(`Product not found: ${id}`);
  }
  // 商品列表页提交
  async listSubmit(data) {
    const res = await this.core.effects.emit(`${this.name}:onListSubmit`, {
      ...data,
      products: this.getProducts()
    });
    if (res.status === false)
      return res;
    this.window.localStorage.setItem(
      "buyTickets:listSubmit",
      JSON.stringify({ ...data, products: this.getProducts() })
    );
    console.log("listSubmit 应该走逻辑层的 API 发请求了");
  }
  // 购物车提交
  async cartSubmit(data) {
    await this.core.effects.emit(`${this.name}:onCartSubmit`, {
      ...data,
      products: this.getProducts()
    });
  }
  // 结算提交
  async checkoutSubmit(data) {
    await this.core.effects.emit(`${this.name}:onCheckoutSubmit`, {
      ...data,
      products: this.getProducts()
    });
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BuyTicketsImpl,
  ...require("./types")
});
