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/plus/salesSdk/utils/holderSubmitValidation.ts
var holderSubmitValidation_exports = {};
__export(holderSubmitValidation_exports, {
  buildHolderConfirmDisplayLines: () => buildHolderConfirmDisplayLines,
  buildHolderConfirmSnapshotKeys: () => buildHolderConfirmSnapshotKeys,
  calculateUnassignedHolderCount: () => calculateUnassignedHolderCount,
  collectUnassignedHolderLines: () => collectUnassignedHolderLines,
  dedupeUnassignedHolderLines: () => dedupeUnassignedHolderLines,
  resolveEnrichedBookingForLine: () => resolveEnrichedBookingForLine,
  resolveLineByProductUid: () => resolveLineByProductUid,
  shouldPromptHolderBeforeSubmit: () => shouldPromptHolderBeforeSubmit
});
module.exports = __toCommonJS(holderSubmitValidation_exports);
var import_utils = require("../../../components/booking/utils");
var import_utils2 = require("../../../components/booking/info/service2/utils");
function hasHolderAssigned(cacheItem) {
  var _a;
  const holderId = (_a = cacheItem == null ? void 0 : cacheItem._extend) == null ? void 0 : _a.holder_id;
  if (Array.isArray(holderId)) return holderId.length > 0;
  return holderId !== void 0 && holderId !== null && holderId !== "";
}
function dedupeUnassignedHolderLines(lines) {
  const result = [];
  lines.forEach((line) => {
    var _a, _b;
    const groupId = (_b = (_a = line.cacheItem) == null ? void 0 : _a._extend) == null ? void 0 : _b.groupId;
    if (groupId) {
      if (!result.some(
        (item) => {
          var _a2, _b2;
          return ((_b2 = (_a2 = item.cacheItem) == null ? void 0 : _a2._extend) == null ? void 0 : _b2.groupId) === groupId;
        }
      )) {
        result.push(line);
      }
      return;
    }
    result.push(line);
  });
  return result;
}
function calculateUnassignedHolderCount(lines) {
  return dedupeUnassignedHolderLines(lines).reduce((sum, line) => {
    var _a, _b;
    return sum + (((_b = (_a = line.cacheItem) == null ? void 0 : _a._extend) == null ? void 0 : _b.quantity) || 1);
  }, 0);
}
function collectUnassignedHolderLines(bookingTicket) {
  var _a, _b;
  if (!bookingTicket || typeof bookingTicket.getCart !== "function") {
    return [];
  }
  if (typeof bookingTicket.buildCacheItemFromOrderLine !== "function") {
    return [];
  }
  const buildCacheItemFromOrderLine = bookingTicket.buildCacheItemFromOrderLine;
  const cart = bookingTicket.getCart();
  const catalog = ((_b = (_a = bookingTicket.getProductCatalog) == null ? void 0 : _a.call(bookingTicket)) == null ? void 0 : _b.products) ?? [];
  const rawLines = [];
  (cart.bookings || []).forEach((bookingView) => {
    var _a2;
    const orderLine = (_a2 = bookingView._extend) == null ? void 0 : _a2.product;
    if (!orderLine) return;
    const sourceProduct = catalog.find((p) => String(p.id) === String(orderLine.product_id)) ?? null;
    const cacheItem = buildCacheItemFromOrderLine({
      product: orderLine,
      booking: bookingView,
      sourceProduct
    });
    if ((0, import_utils2.isNormalProductByDurationSchedule)(cacheItem)) return;
    if (hasHolderAssigned(cacheItem)) return;
    rawLines.push({
      cacheItem,
      booking: bookingView,
      orderLine
    });
  });
  return dedupeUnassignedHolderLines(rawLines);
}
function resolveEnrichedBookingForLine(line, cartBookings) {
  var _a, _b;
  const uid = (_a = line.orderLine.metadata) == null ? void 0 : _a.unique_identification_number;
  if (uid != null && uid !== "") {
    const matched = cartBookings.find(
      (booking) => String(booking.product_uid) === String(uid)
    );
    if (matched) return matched;
  }
  const bookingId = (_b = line.booking) == null ? void 0 : _b.booking_id;
  if (bookingId) {
    const matched = cartBookings.find(
      (booking) => booking.booking_id === bookingId
    );
    if (matched) return matched;
  }
  return line.booking;
}
function buildHolderConfirmSnapshotKeys(lines) {
  const keys = [];
  dedupeUnassignedHolderLines(lines).forEach((line) => {
    var _a, _b, _c;
    const productUid = (_a = line.orderLine.metadata) == null ? void 0 : _a.unique_identification_number;
    if (productUid === void 0 || productUid === null || productUid === "") {
      return;
    }
    const key = {
      productUid: String(productUid)
    };
    if ((_c = (_b = line.cacheItem) == null ? void 0 : _b._extend) == null ? void 0 : _c.groupId) {
      key.groupId = line.cacheItem._extend.groupId;
    }
    keys.push(key);
  });
  return keys;
}
function resolveLineByProductUid(bookingTicket, productUid) {
  var _a, _b, _c;
  if (typeof bookingTicket.getCart !== "function") return null;
  if (typeof bookingTicket.buildCacheItemFromOrderLine !== "function") return null;
  const buildCacheItemFromOrderLine = bookingTicket.buildCacheItemFromOrderLine;
  const cart = bookingTicket.getCart();
  const catalog = ((_b = (_a = bookingTicket.getProductCatalog) == null ? void 0 : _a.call(bookingTicket)) == null ? void 0 : _b.products) ?? [];
  const bookingView = (cart.bookings || []).find(
    (booking) => String(booking.product_uid) === String(productUid)
  );
  const orderLine = (_c = bookingView == null ? void 0 : bookingView._extend) == null ? void 0 : _c.product;
  if (!bookingView || !orderLine) return null;
  const sourceProduct = catalog.find((p) => String(p.id) === String(orderLine.product_id)) ?? null;
  return {
    cacheItem: buildCacheItemFromOrderLine({
      product: orderLine,
      booking: bookingView,
      sourceProduct
    }),
    booking: bookingView,
    orderLine
  };
}
function buildHolderConfirmDisplayLines(input) {
  const { bookingTicket, snapshotLineKeys, cartBookings } = input;
  const unassignedUidSet = new Set(
    collectUnassignedHolderLines(bookingTicket).map(
      (line) => {
        var _a;
        return String(((_a = line.orderLine.metadata) == null ? void 0 : _a.unique_identification_number) ?? "");
      }
    )
  );
  return snapshotLineKeys.map((key) => {
    const baseLine = resolveLineByProductUid(bookingTicket, key.productUid);
    if (!baseLine) return null;
    return {
      ...baseLine,
      displayBooking: resolveEnrichedBookingForLine(baseLine, cartBookings),
      isAssigned: !unassignedUidSet.has(key.productUid)
    };
  }).filter((line) => line != null);
}
function shouldPromptHolderBeforeSubmit(input) {
  if (!input.bookingTicket) return false;
  if ((0, import_utils.isWalkIn)(input.customerId ?? void 0)) return false;
  if (!(0, import_utils2.getIsSubjectRequire)({ bookingConfig: input.bookingConfig })) return false;
  return collectUnassignedHolderLines(input.bookingTicket).length > 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildHolderConfirmDisplayLines,
  buildHolderConfirmSnapshotKeys,
  calculateUnassignedHolderCount,
  collectUnassignedHolderLines,
  dedupeUnassignedHolderLines,
  resolveEnrichedBookingForLine,
  resolveLineByProductUid,
  shouldPromptHolderBeforeSubmit
});
