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/solution/BookingTicket/utils/cartView.ts
var cartView_exports = {};
__export(cartView_exports, {
  buildCartView: () => buildCartView,
  buildCustomerView: () => buildCustomerView,
  indexBookingsByProductUid: () => indexBookingsByProductUid,
  isWalkInCustomer: () => isWalkInCustomer
});
module.exports = __toCommonJS(cartView_exports);
function indexBookingsByProductUid(bookings) {
  if (!Array.isArray(bookings) || bookings.length === 0)
    return {};
  return bookings.reduce((acc, booking) => {
    const uid = booking == null ? void 0 : booking.product_uid;
    if (uid === void 0 || uid === null || uid === "")
      return acc;
    const key = String(uid);
    if (!acc[key])
      acc[key] = [];
    acc[key].push(booking);
    return acc;
  }, {});
}
function indexProductsByUid(lines) {
  return lines.reduce((acc, line) => {
    var _a;
    const uid = (_a = line.metadata) == null ? void 0 : _a.unique_identification_number;
    if (uid === void 0 || uid === null || uid === "")
      return acc;
    const key = String(uid);
    if (!acc[key])
      acc[key] = line;
    return acc;
  }, {});
}
function buildCartView(lines, bookings) {
  const productLines = Array.isArray(lines) ? lines : [];
  const bookingList = Array.isArray(bookings) ? bookings : [];
  const linesByUid = indexProductsByUid(productLines);
  const claimedUids = /* @__PURE__ */ new Set();
  const cartBookings = bookingList.filter((booking) => booking.parent_id !== 0).map((booking) => {
    const uid = booking == null ? void 0 : booking.product_uid;
    let product = null;
    if (uid !== void 0 && uid !== null && uid !== "") {
      const key = String(uid);
      if (!claimedUids.has(key)) {
        const line = linesByUid[key];
        if (line) {
          claimedUids.add(key);
          product = line;
        }
      }
    }
    return {
      ...booking,
      _extend: { product }
    };
  });
  const items = productLines.filter((line) => {
    var _a;
    const uid = (_a = line.metadata) == null ? void 0 : _a.unique_identification_number;
    if (uid === void 0 || uid === null || uid === "")
      return true;
    return !claimedUids.has(String(uid));
  });
  return { bookings: cartBookings, items };
}
function isWalkInCustomer(customer) {
  if (!customer)
    return true;
  const id = customer.id;
  if (id === void 0 || id === null)
    return true;
  if (typeof id === "string") {
    return ["", "0", "1"].includes(id);
  }
  return [0, 1].includes(id);
}
function buildCustomerView(selected, tempOrder) {
  if (selected && selected.id !== void 0 && selected.id !== null && selected.id !== "" && selected.id !== 0) {
    return {
      id: selected.id,
      name: selected.name ?? "",
      phone: selected.phone,
      email: selected.email,
      country_calling_code: selected.country_calling_code,
      cover: selected.cover,
      isWalkIn: isWalkInCustomer(selected)
    };
  }
  if (tempOrder && (tempOrder.customer_id != null || tempOrder.customer_name)) {
    const id = tempOrder.customer_id ?? "";
    return {
      id,
      name: tempOrder.customer_name ?? "",
      phone: tempOrder.phone,
      email: tempOrder.email,
      country_calling_code: tempOrder.country_calling_code,
      cover: void 0,
      isWalkIn: isWalkInCustomer({ id })
    };
  }
  return null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildCartView,
  buildCustomerView,
  indexBookingsByProductUid,
  isWalkInCustomer
});
