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/SalesSummary/index.ts
var SalesSummary_exports = {};
__export(SalesSummary_exports, {
  SalesSummaryModule: () => SalesSummaryModule
});
module.exports = __toCommonJS(SalesSummary_exports);
var import_BaseModule = require("../BaseModule");
var import_Schedule = require("../Schedule");
var import_utils = require("./utils");
__reExport(SalesSummary_exports, require("./types"), module.exports);
var salesSummaryDataKeys = {
  isPriceIncludeTax: "is_price_include_tax",
  taxRate: "tax_rate",
  taxTitle: "tax_title",
  taxCountryCode: "tax_country_code"
};
var SalesSummaryModule = class extends import_BaseModule.BaseModule {
  constructor(name, version) {
    super(name, version);
    this.defaultName = "salesSummary";
    this.defaultVersion = "1.0.0";
  }
  async initialize(core, options) {
    var _a, _b;
    this.core = core;
    this.store = options.store;
    this.appPlugin = this.core.getPlugin("app");
    this.request = this.core.getPlugin("request");
    this.store.surchargeList = ((_a = options.initialState) == null ? void 0 : _a.surchargeList) || [];
    this.store.summary = ((_b = options.initialState) == null ? void 0 : _b.summary) || (0, import_utils.createEmptySalesSummary)();
    if (!this.appPlugin) {
      throw new Error("SalesSummaryModule 需要 app 插件支持");
    }
    if (!this.request) {
      throw new Error("SalesSummaryModule 需要 request 插件支持");
    }
    await this.getSurchargeList();
  }
  async getSurchargeList() {
    try {
      const surchargeList = await this.request.get(
        "/order/custom-surcharge/available/v2",
        {
          channel: "online-store",
          is_assemble_product_data: 1,
          is_assemble_schedule_data: 1,
          with: ["relationSchedule"]
        }
      );
      this.store.surchargeList = (surchargeList == null ? void 0 : surchargeList.data) || [];
    } catch (error) {
      console.warn("[SalesSummaryModule] 加载附加费配置失败", error);
      this.store.surchargeList = [];
    }
  }
  getAppData(key) {
    var _a, _b, _c, _d, _e, _f;
    const getData = (_b = (_a = this.appPlugin).getData) == null ? void 0 : _b.call(_a);
    if (typeof getData === "function")
      return getData(key);
    const app = this.appPlugin.getApp();
    const coreData = (_f = (_d = (_c = app == null ? void 0 : app.models) == null ? void 0 : _c.getStore) == null ? void 0 : (_e = _d.call(_c)).getDataByModel) == null ? void 0 : _f.call(_e, "core", "core");
    return coreData == null ? void 0 : coreData[key];
  }
  getTaxConfig() {
    return {
      isPriceIncludeTax: this.getAppData(salesSummaryDataKeys.isPriceIncludeTax),
      taxRate: this.getAppData(salesSummaryDataKeys.taxRate),
      taxTitle: this.getAppData(salesSummaryDataKeys.taxTitle),
      taxCountryCode: this.getAppData(salesSummaryDataKeys.taxCountryCode)
    };
  }
  async getSummary(params) {
    const { products = [], isPriceIncludeTax, taxRate, shopDiscount } = params;
    const taxConfig = this.getTaxConfig();
    const summarySchedule = this.core.getModule(
      `${this.name.split("_")[0]}_schedule`
    );
    const needScheduleIds = this.store.surchargeList.map((item) => item.available_schedule_ids).flat();
    const scheduleList = summarySchedule == null ? void 0 : summarySchedule.getScheduleListByIds(needScheduleIds);
    const scheduleById = {};
    if (Array.isArray(scheduleList)) {
      for (const item of scheduleList) {
        scheduleById[item.id] = item;
      }
    }
    const summary = (0, import_utils.calculateSalesSummary)({
      products,
      isPriceIncludeTax: isPriceIncludeTax ?? taxConfig.isPriceIncludeTax ?? 1,
      taxRate: taxRate ?? taxConfig.taxRate,
      surchargeList: this.store.surchargeList,
      scheduleById,
      isInScheduleByDate: import_Schedule.ScheduleModule.isInScheduleByDate,
      shopDiscount
    });
    const summaryWithTaxMeta = {
      ...summary,
      tax_title: taxConfig.taxTitle || "",
      tax_rate: taxRate ?? taxConfig.taxRate
    };
    this.store.summary = summaryWithTaxMeta;
    return summaryWithTaxMeta;
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  SalesSummaryModule,
  ...require("./types")
});
