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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/booking/info2/service/editService/utils.tsx
var utils_exports = {};
__export(utils_exports, {
  getAutoAllocationStorage: () => getAutoAllocationStorage,
  setAutoAllocationStorage: () => setAutoAllocationStorage,
  setProductPrice: () => setProductPrice
});
module.exports = __toCommonJS(utils_exports);
var import_utils = require("@pisell/utils");
var import_utils2 = require("../addService/utils");
var import_decimal = require("decimal.js");
var import_utils3 = require("@pisell/utils");
var getAutoAllocationStorage = (id) => {
  const value = localStorage.getItem("resource-auto-allocation");
  if (value) {
    const valueObj = JSON.parse(value);
    return (0, import_utils.isBoolean)(valueObj[id]) ? valueObj[id] : true;
  }
  return true;
};
var setAutoAllocationStorage = (data) => {
  const value = localStorage.getItem("resource-auto-allocation");
  if (value) {
    const valueObj = JSON.parse(value);
    localStorage.setItem("resource-auto-allocation", JSON.stringify({ ...valueObj, ...data }));
  } else {
    localStorage.setItem("resource-auto-allocation", JSON.stringify({ ...data }));
  }
};
var setProductPrice = (cacheItem, { newTotal, origin_total }) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
  if (((_a = cacheItem == null ? void 0 : cacheItem.discount_list) == null ? void 0 : _a.length) && ((cacheItem == null ? void 0 : cacheItem.discount_list) || []).some((item) => ["good_pass", "discount_card", "product_discount_card"].includes(
    item.type
  ))) {
    return cacheItem;
  }
  if (cacheItem.discount_amount) {
    return cacheItem;
  }
  const discountRatio = new import_decimal.Decimal(newTotal).div(new import_decimal.Decimal(origin_total));
  let subTotal = new import_decimal.Decimal(0);
  if (((_b = cacheItem._extend.other) == null ? void 0 : _b.bundle) && (0, import_utils3.isArr)((_c = cacheItem._extend.other) == null ? void 0 : _c.bundle)) {
    (_e = (_d = cacheItem._extend.other) == null ? void 0 : _d.bundle) == null ? void 0 : _e.forEach((bundle) => {
      const newPrice = new import_decimal.Decimal(bundle.original_price || bundle.price).mul(discountRatio).toDecimalPlaces(2, import_decimal.Decimal.ROUND_DOWN).toNumber();
      const quantity = bundle.num ?? bundle.quantity ?? 1;
      subTotal = subTotal.add(new import_decimal.Decimal(newPrice).mul(quantity));
      bundle.price = newPrice;
    });
  }
  if (((_f = cacheItem._extend.other) == null ? void 0 : _f.option) && (0, import_utils3.isArr)((_g = cacheItem._extend.other) == null ? void 0 : _g.option)) {
    (_i = (_h = cacheItem._extend.other) == null ? void 0 : _h.option) == null ? void 0 : _i.forEach((option) => {
      const newPrice = new import_decimal.Decimal(option.price).mul(discountRatio).toDecimalPlaces(2, import_decimal.Decimal.ROUND_DOWN).toNumber();
      const quantity = option.num ?? option.quantity ?? 1;
      subTotal = subTotal.add(new import_decimal.Decimal(newPrice).mul(quantity));
      option.price = newPrice;
    });
  }
  cacheItem._extend.price = new import_decimal.Decimal(newTotal).sub(subTotal).toNumber();
  return cacheItem;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getAutoAllocationStorage,
  setAutoAllocationStorage,
  setProductPrice
});
