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/modules/BookingContext/utils/cacheItemToBookingInput.ts
var cacheItemToBookingInput_exports = {};
__export(cacheItemToBookingInput_exports, {
  cacheItemToBookingInput: () => cacheItemToBookingInput
});
module.exports = __toCommonJS(cacheItemToBookingInput_exports);
var import_dayjs = __toESM(require("dayjs"));
var import_serviceTimes = require("./serviceTimes");
var import_resources = require("./resources");
var import_flexible = require("./flexible");
function normalizeExtendCapacityForMetadata(capacity) {
  if (!Array.isArray(capacity) || capacity.length === 0)
    return void 0;
  const normalized = capacity.map((item) => {
    if (!item || typeof item !== "object")
      return null;
    if (item.id === void 0 || item.id === null)
      return null;
    const row = {
      id: item.id,
      value: Number(item.value ?? 0)
    };
    if (item.name !== void 0)
      row.name = item.name;
    return row;
  }).filter((item) => item != null);
  return normalized.length > 0 ? normalized : void 0;
}
function safeFormat(value, fmt) {
  if (value === void 0 || value === null)
    return void 0;
  const d = (0, import_dayjs.default)(value);
  if (!d.isValid())
    return void 0;
  return d.format(fmt);
}
function normalizeTimeString(value) {
  if (value === void 0 || value === null)
    return void 0;
  if (typeof value === "string") {
    const t = value.trim();
    const hhmmFromHhmmss = t.match(/^(\d{1,2}:\d{2}):\d{2}$/);
    if (hhmmFromHhmmss)
      return hhmmFromHhmmss[1];
    if (/^\d{1,2}:\d{2}$/.test(t))
      return t;
  }
  const d = (0, import_dayjs.default)(value);
  if (d.isValid())
    return d.format("HH:mm");
  return void 0;
}
function resolveDurationMinutes(value) {
  if (typeof value === "number" && Number.isFinite(value))
    return value;
  if (typeof value !== "string")
    return void 0;
  if (import_flexible.flexibleObj.isFlexible(value))
    return import_flexible.flexibleObj.getValue(value);
  const n = Number(value);
  return Number.isNaN(n) ? void 0 : n;
}
function flattenResources(cacheItem) {
  var _a;
  const resourceMap = ((_a = cacheItem == null ? void 0 : cacheItem._extend) == null ? void 0 : _a.resource) || {};
  const topLevelCapacity = (0, import_resources.getSumCapacity)(cacheItem, false);
  const result = [];
  Object.values(resourceMap).forEach((arr) => {
    if (!Array.isArray(arr))
      return;
    arr.forEach((r) => {
      const item = {
        form_id: r == null ? void 0 : r.form_id,
        relation_type: (r == null ? void 0 : r.relation_type) || "form",
        relation_id: r == null ? void 0 : r.relation_id,
        capacity: topLevelCapacity
      };
      item.like_status = (r == null ? void 0 : r.like_status) || "common";
      if ((r == null ? void 0 : r.metadata) !== void 0) {
        item.metadata = { ...r.metadata };
      }
      if ((r == null ? void 0 : r.children) !== void 0)
        item.children = r.children;
      result.push(item);
    });
  });
  return result;
}
function deriveResourceSelectType(cacheItem) {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  const localFirst = (_d = (_c = (_b = (_a = cacheItem == null ? void 0 : cacheItem._data) == null ? void 0 : _a.resources) == null ? void 0 : _b[0]) == null ? void 0 : _c.select_type) == null ? void 0 : _d.type;
  if (localFirst)
    return localFirst;
  const productFirst = (_h = (_g = (_f = (_e = cacheItem == null ? void 0 : cacheItem.product_resource) == null ? void 0 : _e.resources) == null ? void 0 : _f[0]) == null ? void 0 : _g.select_type) == null ? void 0 : _h.type;
  if (productFirst)
    return productFirst;
  return void 0;
}
function cacheItemToBookingInput(cacheItem, options) {
  var _a, _b;
  if (!cacheItem)
    return { metadata: { holder_id: null } };
  const ext = cacheItem._extend || {};
  const isMultiDay = (0, import_serviceTimes.isMultiDayProduct)(cacheItem) || ext.is_all === 1 || ext.sub_type === "days";
  const isSession = (0, import_serviceTimes.isSessionProduct)(cacheItem);
  const result = {};
  if (isSession && ((_a = ext == null ? void 0 : ext.other) == null ? void 0 : _a.session)) {
    const session = ext.other.session;
    result.start_date = safeFormat(ext.start_date || ext.startDate || session.start_at, "YYYY-MM-DD");
    result.start_time = normalizeTimeString(ext.start_time) || normalizeTimeString(session.start_time) || safeFormat(session.start_at, "HH:mm");
    result.end_date = safeFormat(ext.end_date || ext.endDate || session.end_at, "YYYY-MM-DD");
    result.end_time = normalizeTimeString(ext.end_time) || normalizeTimeString(session.end_time) || safeFormat(session.end_at, "HH:mm");
  } else {
    result.start_date = safeFormat(ext.start_date || ext.startDate, "YYYY-MM-DD");
    result.start_time = normalizeTimeString(ext.start_time) || safeFormat(ext.startDate, "HH:mm");
    result.end_date = safeFormat(ext.end_date || ext.endDate, "YYYY-MM-DD");
    result.end_time = normalizeTimeString(ext.end_time) || safeFormat(ext.endDate, "HH:mm");
  }
  if (isMultiDay) {
    result.is_all = 1;
    result.sub_type = "days";
    result.duration = Number(ext.duration) || (0, import_serviceTimes.getDays)(cacheItem, "days");
    if (result.start_date)
      result.select_date = result.start_date;
    if (result.start_time && (!result.end_time || result.end_time === "00:00")) {
      result.end_time = result.start_time;
    }
  } else {
    result.is_all = 0;
    result.sub_type = ext.sub_type || "minutes";
    const duration = resolveDurationMinutes(ext.duration);
    if (duration !== void 0)
      result.duration = duration;
  }
  result.number = (0, import_resources.getSumCapacity)(cacheItem, true);
  result.resources = flattenResources(cacheItem);
  if (ext.holder !== void 0)
    result.holder = ext.holder;
  if (Array.isArray(ext.relation_forms))
    result.relation_forms = ext.relation_forms;
  result.like_status = ext.like_status || "common";
  if (cacheItem.schedule_event_id !== void 0) {
    result.schedule_event_id = cacheItem.schedule_event_id;
  }
  const metadata = { holder_id: null };
  if (ext.holder_id !== void 0 && ext.holder_id !== null) {
    metadata.holder_id = ext.holder_id;
  }
  if (ext.collect_pax !== void 0)
    metadata.collect_pax = ext.collect_pax;
  const extendCapacity = normalizeExtendCapacityForMetadata(ext.capacity);
  if (extendCapacity) {
    metadata.capacity = extendCapacity;
  } else if (ext.capacity_snapshot !== void 0) {
    metadata.capacity = ext.capacity_snapshot;
  }
  const selectType = deriveResourceSelectType(cacheItem) || ((_b = options == null ? void 0 : options.bookingConfig) == null ? void 0 : _b.resource_select_type);
  if (selectType)
    metadata.resource_select_type = selectType;
  result.metadata = metadata;
  result.schedule_id = 0;
  result.relation_id = 0;
  result.form_id = 0;
  result.relation_type = "";
  return result;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  cacheItemToBookingInput
});
