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/components/booking/info/service2/Group/utils.ts
var utils_exports = {};
__export(utils_exports, {
  concatBookings: () => concatBookings,
  createBookingItemsByGroup: () => createBookingItemsByGroup,
  filterItemByGroupId: () => filterItemByGroupId,
  getGroupBooking: () => getGroupBooking,
  getGroupTotalPrice: () => getGroupTotalPrice,
  getProductByGroupId: () => getProductByGroupId,
  isGroupBooking: () => isGroupBooking
});
module.exports = __toCommonJS(utils_exports);
var import_utils = require("@pisell/utils");
var import_utils2 = require("../../../utils");
var import_distributeDiscount = require("../../service/serviceManager/distributeDiscount");
var import_utils3 = require("../../../info2/service/editService/utils");
var import_utils4 = require("../../../../ticketBooking/utils");
var import_decimal = __toESM(require("decimal.js"));
var getGroupTotalPrice = (items) => {
  return items.reduce((prev, next) => {
    prev.duration += next._extend.duration;
    prev.total += next._extend.total;
    prev.origin_total += next._extend.origin_total;
    return prev;
  }, { total: 0, origin_total: 0 });
};
var concatBookings = (items) => {
  let firstItem = items[0];
  let lastItem = items[items.length - 1];
  let { total, duration, startDate, endDate, origin_total } = items.reduce((prev, next) => {
    prev.duration += next._extend.duration;
    prev.total += next._extend.total;
    prev.origin_total += next._extend.origin_total;
    return prev;
  }, { price: 0, total: 0, origin_total: 0, duration: 0, startDate: firstItem._extend.startDate, endDate: lastItem._extend.endDate });
  return {
    ...firstItem,
    // 虚拟组, id使用组id
    _id: firstItem._extend.groupId,
    _extend: {
      ...firstItem._extend,
      items,
      duration,
      // price: total,
      total,
      origin_total,
      startDate,
      endDate,
      start_date: startDate,
      end_date: endDate
    }
  };
};
var createBookingItemsByGroup = (item, isBatchEdit) => {
  var _a, _b, _c, _d, _e, _f;
  const groupId = item._extend.groupId || (0, import_utils.getUniqueId)("bookingGroup_");
  let bookings = [];
  for (let i = 0; i < item._extend.duration; i++) {
    const _extend = { ...item._extend };
    const startDate = _extend.start_date.add(i, "day");
    const endDate = _extend.start_date.add(i + 1, "day");
    const price = ((_b = (_a = _extend.multiDayPrices) == null ? void 0 : _a[startDate.format("YYYY-MM-DD")]) == null ? void 0 : _b.total) || item._extend.price;
    const cacheItem = {
      ...item,
      // 编辑时组商品需要清空booking_id  否则会重复
      booking_id: isBatchEdit ? 0 : item.booking_id,
      _id: `${item._id}-${i + 1}`,
      // price: price,
      _extend: {
        ..._extend,
        order_detail_id: isBatchEdit ? 0 : item.order_detail_id,
        groupId,
        duration: 1,
        price,
        startDate,
        endDate,
        start_date: startDate,
        end_date: endDate,
        dateKey: `${startDate.format("YYYY-MM-DD")}~${endDate.format("YYYY-MM-DD")}}`
      }
    };
    const totalPrice = (0, import_utils2.getProductTotalPrice)(cacheItem);
    const origin_total = (_f = (_e = (_d = (_c = item._extend) == null ? void 0 : _c.items) == null ? void 0 : _d[0]) == null ? void 0 : _e._extend) == null ? void 0 : _f.origin_total;
    cacheItem._extend.total = totalPrice;
    cacheItem._extend.origin_total = origin_total ?? totalPrice;
    cacheItem._extend.items = void 0;
    cacheItem._serviceKey = (0, import_utils4.genServiceKey)(cacheItem);
    bookings.push(cacheItem);
  }
  if (isBatchEdit) {
    const groupTotalPrice = item._extend.total;
    const groupOriginTotalPrice = item._extend.origin_total;
    const discountAmount = (groupOriginTotalPrice - groupTotalPrice) * -1;
    if (discountAmount !== 0) {
      if (groupOriginTotalPrice !== 0) {
        bookings = (0, import_distributeDiscount.distributeDiscountOrIncrease)(bookings, discountAmount, groupOriginTotalPrice);
        bookings = bookings.map((booking) => {
          var _a2, _b2;
          return (0, import_utils3.setProductPrice)(booking, {
            newTotal: (_a2 = booking == null ? void 0 : booking._extend) == null ? void 0 : _a2.total,
            origin_total: (_b2 = booking == null ? void 0 : booking._extend) == null ? void 0 : _b2.origin_total
          });
        });
      } else {
        const count = bookings.length;
        const avgTotal = new import_decimal.default(groupTotalPrice).div(count).toDecimalPlaces(2, import_decimal.default.ROUND_HALF_UP);
        let assignedSum = new import_decimal.default(0);
        bookings = bookings.map((booking, i) => {
          const newTotal = i === count - 1 ? new import_decimal.default(groupTotalPrice).sub(assignedSum).toNumber() : avgTotal.toNumber();
          if (i !== count - 1) {
            assignedSum = assignedSum.add(avgTotal);
          }
          return {
            ...booking,
            _extend: {
              ...booking._extend,
              total: newTotal,
              price: newTotal
            }
          };
        });
      }
    }
  }
  console.log("bookings", bookings);
  return bookings;
};
var isGroupBooking = (_id) => {
  var _a;
  return ((_a = _id == null ? void 0 : _id.indexOf) == null ? void 0 : _a.call(_id, "bookingGroup_")) === 0;
};
var getProductByGroupId = (groupId, items) => {
  return items.filter((item) => {
    var _a;
    return ((_a = item._extend) == null ? void 0 : _a.groupId) === groupId;
  }).map((item) => item._id);
};
var getGroupBooking = (groupId, items) => {
  let products = getProductByGroupId(groupId, items);
  return concatBookings(products);
};
var filterItemByGroupId = (groupId, items) => {
  return items.filter((item) => {
    var _a;
    return ((_a = item._extend) == null ? void 0 : _a.groupId) !== groupId;
  });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  concatBookings,
  createBookingItemsByGroup,
  filterItemByGroupId,
  getGroupBooking,
  getGroupTotalPrice,
  getProductByGroupId,
  isGroupBooking
});
