var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/modules/Summary/index.ts
var Summary_exports = {};
__export(Summary_exports, {
  SummaryModule: () => SummaryModule
});
module.exports = __toCommonJS(Summary_exports);
var import_decimal = __toESM(require("decimal.js"));
var import_BaseModule = require("../BaseModule");
var import_Schedule = require("../Schedule");
var import_utils = require("./utils");
var SummaryModule = class extends import_BaseModule.BaseModule {
  constructor(name, version) {
    super(name, version);
    this.defaultName = "summary";
    this.defaultVersion = "1.0.0";
    this.openCache = false;
  }
  async initialize(core, options) {
    var _a, _b;
    this.core = core;
    this.store = options.store;
    this.shopStore = this.core.getPlugin("shopStore");
    this.request = this.core.getPlugin("request");
    if (options.initialState) {
      this.store.summary = options.initialState.summary;
    }
    if ((_a = options.otherParams) == null ? void 0 : _a.cacheId) {
      this.openCache = options.otherParams.openCache;
      this.cacheId = options.otherParams.cacheId;
    }
    if ((_b = options.otherParams) == null ? void 0 : _b.fatherModule) {
      this.fatherModule = options.otherParams.fatherModule;
    }
    if (!this.shopStore) {
      throw new Error("SummaryModule 需要 shopStore 插件支持");
    }
    this.getSurchargeList();
  }
  async getSurchargeList() {
    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) || [];
  }
  async getSummary(cartItems) {
    var _a, _b, _c;
    const shopInfo = ((_b = (_a = this.shopStore.get("core")) == null ? void 0 : _a.core) == null ? void 0 : _b.shop) || {};
    const scheduleModule = this.core.getModule("appointmentBooking_schedule");
    const needScheduleIds = this.store.surchargeList.map((item) => item.available_schedule_ids).flat();
    const scheduleList = scheduleModule == null ? void 0 : scheduleModule.getScheduleListByIds(needScheduleIds);
    const scheduleById = {};
    if (Array.isArray(scheduleList)) {
      for (let item of scheduleList) {
        scheduleById[item.id] = item;
      }
    }
    const summary = (0, import_utils.calculatePriceDetails)(shopInfo, cartItems, (_c = import_Schedule.ScheduleModule) == null ? void 0 : _c.isInScheduleByDate, this.store.surchargeList, scheduleById);
    this.store.summary = summary;
    return this.store.summary;
  }
  /**
   * 获取协议
   * @param protocolId 协议ID
   * @returns 协议
   */
  async getProtocol(protocolId) {
    return this.request.get(`/shop-policy/${protocolId}`);
  }
  storeChange() {
    if (this.openCache) {
      this.checkSaveCache({
        cacheId: this.cacheId,
        fatherModule: this.fatherModule,
        store: this.store,
        cacheKey: ["summary"]
      });
    }
  }
  getTaxforUtils(items, shopInfo, subtotal) {
    return (0, import_utils.getTax)({ service: items, bookingDetail: null, bookingId: void 0 }, {
      computed: {
        productExpectAmount: subtotal,
        shopDiscount: 0
      },
      isEdit: false,
      tax_rate: shopInfo == null ? void 0 : shopInfo.tax_rate,
      is_price_include_tax: shopInfo == null ? void 0 : shopInfo.is_price_include_tax
    });
  }
  getSurchargeforUtils(items) {
    const scheduleModule = this.core.getModule("appointmentBooking_schedule");
    const needScheduleIds = this.store.surchargeList.map((item) => item.available_schedule_ids).flat();
    const scheduleList = scheduleModule == null ? void 0 : scheduleModule.getScheduleListByIds(needScheduleIds);
    const scheduleById = {};
    if (Array.isArray(scheduleList)) {
      for (let item of scheduleList) {
        scheduleById[item.id] = item;
      }
    }
    const surchargeList = (0, import_utils.getSurcharge)({ service: items, addons: [], bookingDetail: null, bookingId: void 0 }, { isEdit: false, isInScheduleByDate: import_Schedule.ScheduleModule.isInScheduleByDate, surcharge_list: this.store.surchargeList, scheduleById });
    const surchargeAmount = new import_decimal.default((0, import_utils.getSurchargeAmount)({ bookingDetail: null, bookingId: void 0 }, surchargeList, { isEdit: false }));
    return {
      surchargeList,
      surchargeAmount: surchargeAmount.toFixed(2)
    };
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  SummaryModule
});
