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/plus/salesSdk/bookingEditService/buildEditedCacheItem.ts
var buildEditedCacheItem_exports = {};
__export(buildEditedCacheItem_exports, {
  buildEditedCacheItem: () => buildEditedCacheItem
});
module.exports = __toCommonJS(buildEditedCacheItem_exports);
var import_utils = require("@pisell/utils");
var import_decimal = __toESM(require("decimal.js"));
var import_utils2 = require("../../../components/ticketBooking/utils");
var import_utils3 = require("../../../components/booking/utils");
var import_utils4 = require("../../../components/booking/info2/service/editService/utils");
var import_mergeFormIntoExtend = require("./mergeFormIntoExtend");
var import_resolveCacheItemForResourceCheck = require("./resolveCacheItemForResourceCheck");
function stripManualProductDiscountFromList(discountList) {
  return (discountList || []).filter((item) => (item == null ? void 0 : item.type) !== "product");
}
function buildEditedCacheItem(options) {
  var _a, _b;
  const { values, cacheItem: sourceItem, isEditGroup, isChangeTotal, isPriceIncludeTax } = options;
  let item = {
    is_price_include_tax: isPriceIncludeTax,
    ...sourceItem,
    autoClose: false
  };
  let extend = (0, import_mergeFormIntoExtend.mergeFormIntoExtend)(item._extend, values);
  if (values.quantity !== void 0) {
    extend.other = {
      ...extend.other || {},
      quantity: values.quantity
    };
  }
  if (isEditGroup) {
    extend.quantity = 1;
    extend.startDate = extend == null ? void 0 : extend.start_date;
    extend.endDate = extend == null ? void 0 : extend.end_date;
  } else {
    const { startDate, endDate } = (0, import_utils3.getServiceTimes)({ _extend: extend });
    extend.startDate = startDate;
    extend.endDate = endDate;
  }
  if (isChangeTotal) {
    const originTotal = extend.origin_total ?? ((_a = item._extend) == null ? void 0 : _a.origin_total) ?? values.origin_total;
    applyManualDiscountToExtend(extend, values.total, originTotal);
    extend.priceOverride = values.total;
    if (extend.origin_total == null || extend.origin_total === "") {
      extend.origin_total = originTotal;
    }
  }
  if (extend.bundle_edit !== void 0) {
    extend.bundle_edit = 1;
  }
  item._extend = extend;
  item._time = (/* @__PURE__ */ new Date()).getTime();
  item._serviceKey = (0, import_utils2.genServiceKey)(item);
  item._holderKey = (0, import_utils2.genHolderKey)(item);
  item = applyHolderSplit(item);
  item = (0, import_utils4.setProductPrice)(item, {
    newTotal: values.total,
    origin_total: (_b = item._extend) == null ? void 0 : _b.origin_total
  });
  if (isChangeTotal) {
    item.discount_list = stripManualProductDiscountFromList(item.discount_list);
  }
  return (0, import_resolveCacheItemForResourceCheck.resolveCacheItemForResourceCheck)({ ...item, new: 0 });
}
function applyManualDiscountToExtend(extend, total, originTotal) {
  var _a, _b;
  const discountRatio = new import_decimal.default(total).div(originTotal);
  const bundles = ((_a = extend == null ? void 0 : extend.other) == null ? void 0 : _a.bundle) || [];
  let childProductsTotal = new import_decimal.default(0);
  if (bundles.length > 0) {
    bundles.forEach((bundle) => {
      const discountedPrice = new import_decimal.default(bundle.original_price).mul(discountRatio).toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP);
      bundle.bundle_selling_price = discountedPrice.toNumber();
      childProductsTotal = childProductsTotal.add(discountedPrice.mul(bundle.quantity));
    });
  }
  const mainProductTotal = new import_decimal.default(total).sub(childProductsTotal);
  const mainProductQuantity = ((_b = extend.other) == null ? void 0 : _b.quantity) || extend.quantity || 1;
  extend.main_product_selling_price = mainProductTotal.div(mainProductQuantity).toNumber();
}
function applyHolderSplit(item) {
  var _a, _b;
  const holderIds = (_a = item._extend) == null ? void 0 : _a.holder_id;
  if ((holderIds == null ? void 0 : holderIds.length) > 1 || ((_b = item._extend) == null ? void 0 : _b.quantity) > 1 && (holderIds == null ? void 0 : holderIds.length) >= 1) {
    const ids = [...holderIds];
    item._extend.holder_id = ids[0];
    if (item._extend.quantity > 1) {
      item._extend.quantity = 1;
    }
    item._holderKey = (0, import_utils2.genHolderKey)(item);
    item._splitHolderRemainder = ids.slice(1).map((holder_id) => ({
      _id: (0, import_utils.getUniqueId)(),
      holder_id: [holder_id],
      quantity: 1
    }));
  }
  return item;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildEditedCacheItem
});
