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/serviceTimes.ts
var serviceTimes_exports = {};
__export(serviceTimes_exports, {
  applySessionTimesToExtend: () => applySessionTimesToExtend,
  formatDayDate: () => formatDayDate,
  getDays: () => getDays,
  getDurationValue: () => import_flexible.getDurationValue,
  getServeStartTime: () => getServeStartTime,
  getServiceTimes: () => getServiceTimes,
  getSessionFromItem: () => getSessionFromItem,
  isBookingProduct: () => isBookingProduct,
  isMultiDayProduct: () => isMultiDayProduct,
  isSessionProduct: () => isSessionProduct
});
module.exports = __toCommonJS(serviceTimes_exports);
var import_dayjs = __toESM(require("dayjs"));
var import_flexible = require("./flexible");
function isMultiDayProduct(product) {
  var _a;
  return ((_a = product == null ? void 0 : product.duration) == null ? void 0 : _a.type) === "days";
}
function isSessionProduct(cacheItem) {
  return ["session_product", "session_ticket"].includes(cacheItem == null ? void 0 : cacheItem.extension_type);
}
function getSessionFromItem(item) {
  var _a, _b, _c;
  const session = ((_a = item == null ? void 0 : item.cartDetailValue) == null ? void 0 : _a.session) ?? ((_c = (_b = item == null ? void 0 : item._extend) == null ? void 0 : _b.other) == null ? void 0 : _c.session);
  return session && typeof session === "object" ? session : null;
}
function applySessionTimesToExtend(item, extend, ctx) {
  var _a;
  const session = ((_a = extend == null ? void 0 : extend.other) == null ? void 0 : _a.session) ?? getSessionFromItem(item);
  if (!session || !isSessionProduct(item)) {
    return extend;
  }
  const baseDate = (ctx == null ? void 0 : ctx.date) ? (0, import_dayjs.default)(ctx.date) : (0, import_dayjs.default)();
  const startAt = (0, import_dayjs.default)(
    `${baseDate.format("YYYY-MM-DD")} ${(0, import_dayjs.default)(session.start_at).format("HH:mm:ss")}`
  );
  const endAt = (0, import_dayjs.default)(
    `${baseDate.format("YYYY-MM-DD")} ${(0, import_dayjs.default)(session.end_at).format("HH:mm:ss")}`
  );
  const nextExtend = {
    ...extend,
    other: { ...extend.other || {}, session },
    start_date: startAt,
    startDate: startAt,
    start_time: session.start_time ?? (0, import_dayjs.default)(session.start_at).format("HH:mm"),
    end_date: endAt,
    endDate: endAt,
    end_time: session.end_time ?? (0, import_dayjs.default)(session.end_at).format("HH:mm")
  };
  return {
    ...nextExtend,
    duration: getDays({ _extend: nextExtend }, "minutes")
  };
}
function isBookingProduct(item) {
  var _a, _b;
  if (!item)
    return false;
  if (item.duration)
    return true;
  if ((_a = item == null ? void 0 : item.schedules) == null ? void 0 : _a.length)
    return true;
  if ((_b = item == null ? void 0 : item["schedule.ids"]) == null ? void 0 : _b.length)
    return true;
  if (isSessionProduct(item))
    return true;
  if (getSessionFromItem(item))
    return true;
  return false;
}
function getServeStartTime(startTime) {
  if (startTime === "now")
    return (0, import_dayjs.default)().format("HH:mm");
  return startTime || "";
}
function getServiceTimes(service) {
  var _a, _b, _c, _d, _e, _f, _g, _h;
  const startDate = (0, import_dayjs.default)(
    `${(0, import_dayjs.default)((_a = service == null ? void 0 : service._extend) == null ? void 0 : _a.start_date).format("YYYY-MM-DD")} ${getServeStartTime(
      (_b = service == null ? void 0 : service._extend) == null ? void 0 : _b.start_time
    )}`
  );
  if (isMultiDayProduct(service)) {
    const startTime = getServeStartTime((_c = service == null ? void 0 : service._extend) == null ? void 0 : _c.start_time);
    const endDateRaw = ((_d = service == null ? void 0 : service._extend) == null ? void 0 : _d.end_date) ?? ((_e = service == null ? void 0 : service._extend) == null ? void 0 : _e.endDate);
    const endBase = (0, import_dayjs.default)(endDateRaw);
    const endDate2 = endBase.isValid() ? (0, import_dayjs.default)(`${endBase.format("YYYY-MM-DD")} ${startTime}`) : endBase;
    return {
      startDate,
      endDate: endDate2
    };
  }
  const session = (_g = (_f = service == null ? void 0 : service._extend) == null ? void 0 : _f.other) == null ? void 0 : _g.session;
  if (isSessionProduct(service) && session) {
    return {
      startDate: (0, import_dayjs.default)(session.start_at),
      endDate: (0, import_dayjs.default)(session.end_at)
    };
  }
  const endDate = startDate.add(import_flexible.flexibleObj.getValue((_h = service == null ? void 0 : service._extend) == null ? void 0 : _h.duration), "minute");
  return { startDate, endDate };
}
function formatDayDate(_extend) {
  return {
    start_date: (0, import_dayjs.default)(_extend == null ? void 0 : _extend.start_date).startOf("day"),
    end_date: (0, import_dayjs.default)(_extend == null ? void 0 : _extend.end_date).startOf("day")
  };
}
function getDays(cacheItem, type) {
  var _a, _b;
  if (type === "days") {
    const { start_date, end_date } = formatDayDate(cacheItem._extend);
    return end_date.diff(start_date, "day");
  }
  const startDate = (0, import_dayjs.default)((_a = cacheItem == null ? void 0 : cacheItem._extend) == null ? void 0 : _a.start_date);
  const endDate = (0, import_dayjs.default)((_b = cacheItem == null ? void 0 : cacheItem._extend) == null ? void 0 : _b.end_date);
  return endDate.diff(startDate, type);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  applySessionTimesToExtend,
  formatDayDate,
  getDays,
  getDurationValue,
  getServeStartTime,
  getServiceTimes,
  getSessionFromItem,
  isBookingProduct,
  isMultiDayProduct,
  isSessionProduct
});
