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/adjustTime/utils/time.ts
var time_exports = {};
__export(time_exports, {
  addMinutes: () => addMinutes,
  applyTimeToDate: () => applyTimeToDate,
  formatClockTime: () => formatClockTime,
  formatDate: () => formatDate,
  formatDuration: () => formatDuration,
  formatRange: () => formatRange,
  formatTime: () => formatTime,
  getDefaultManualEndTime: () => getDefaultManualEndTime,
  getDelayToNextBillableMinuteChange: () => getDelayToNextBillableMinuteChange,
  getShopClosingTime: () => getShopClosingTime,
  minutesBetween: () => minutesBetween,
  resolveBillableMinuteDate: () => resolveBillableMinuteDate,
  resolveFlexibleBusinessEnd: () => resolveFlexibleBusinessEnd,
  resolveManualEndDate: () => resolveManualEndDate,
  resolvePayOvertimeEnd: () => resolvePayOvertimeEnd,
  resolveUnlimitedExtension: () => resolveUnlimitedExtension,
  toDate: () => toDate
});
module.exports = __toCommonJS(time_exports);
var toDate = (value, fallback = /* @__PURE__ */ new Date()) => {
  if (!value) return fallback;
  if (value instanceof Date) return value;
  const parsed = new Date(value);
  return Number.isNaN(parsed.getTime()) ? fallback : parsed;
};
var addMinutes = (date, minutes) => {
  return new Date(date.getTime() + minutes * 60 * 1e3);
};
var minutesBetween = (start, end) => {
  return Math.max(0, Math.ceil((end.getTime() - start.getTime()) / 6e4));
};
var resolveBillableMinuteDate = (date) => {
  const minute = new Date(date);
  minute.setSeconds(0, 0);
  return minute;
};
var getDelayToNextBillableMinuteChange = (date = /* @__PURE__ */ new Date()) => {
  if (date.getSeconds() === 0 && date.getMilliseconds() === 0) {
    return 60 * 1e3;
  }
  const nextMinute = new Date(date);
  nextMinute.setMinutes(nextMinute.getMinutes() + 1, 0, 0);
  return Math.max(100, nextMinute.getTime() - date.getTime());
};
var pad = (value) => `${value}`.padStart(2, "0");
var formatTime = (date) => {
  return `${pad(date.getHours())}:${pad(date.getMinutes())}`;
};
var formatClockTime = (date) => {
  return `${formatTime(date)}:${pad(date.getSeconds())}`;
};
var formatDate = (date) => {
  return `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`;
};
var formatRange = (start, end) => {
  return `${formatTime(start)} ${formatDate(start)}-${formatTime(end)} ${formatDate(end)}`;
};
var formatDuration = (minutes) => {
  const total = Math.max(0, Math.ceil(minutes));
  const days = Math.floor(total / 1440);
  const hours = Math.floor(total % 1440 / 60);
  const mins = total % 60;
  const parts = [];
  if (days) parts.push(`${days} ${days === 1 ? "day" : "days"}`);
  if (hours) parts.push(`${hours} ${hours === 1 ? "hour" : "hours"}`);
  if (mins || !parts.length) parts.push(`${mins} min`);
  return parts.join(" ");
};
var applyTimeToDate = (baseDate, timeStr) => {
  const [hours = 0, minutes = 0] = timeStr.split(":").map((part) => Number(part) || 0);
  const result = new Date(baseDate);
  result.setHours(hours, minutes, 0, 0);
  return result;
};
var getShopClosingTime = () => {
  if (typeof localStorage !== "undefined") {
    return localStorage.getItem("shopOpeningHours") || "23:59";
  }
  return "23:59";
};
var resolveFlexibleBusinessEnd = (bookingStart, _resourceEndTime, shopClosingTime) => {
  const shopClose = shopClosingTime || getShopClosingTime();
  let businessEnd = applyTimeToDate(bookingStart, shopClose);
  if (bookingStart.getTime() > businessEnd.getTime()) {
    businessEnd = addMinutes(businessEnd, 24 * 60);
  }
  return businessEnd;
};
var resolvePayOvertimeEnd = (current, _resourceEndTime, _shopClosingTime) => {
  return current;
};
var resolveUnlimitedExtension = (bookingStart, bookingEnd, resourceEndTime, shopClosingTime) => {
  const newEnd = resolveFlexibleBusinessEnd(bookingStart, resourceEndTime, shopClosingTime);
  const targetMinutes = minutesBetween(bookingEnd, newEnd);
  return { newEnd, targetMinutes };
};
var resolveManualEndDate = (referenceDate, timeStr, notBefore) => {
  const candidate = applyTimeToDate(referenceDate, timeStr);
  if (candidate.getTime() <= notBefore.getTime()) {
    return addMinutes(candidate, 24 * 60);
  }
  return candidate;
};
var getDefaultManualEndTime = (baseEnd, offsetMinutes = 90) => {
  return formatTime(addMinutes(baseEnd, offsetMinutes));
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  addMinutes,
  applyTimeToDate,
  formatClockTime,
  formatDate,
  formatDuration,
  formatRange,
  formatTime,
  getDefaultManualEndTime,
  getDelayToNextBillableMinuteChange,
  getShopClosingTime,
  minutesBetween,
  resolveBillableMinuteDate,
  resolveFlexibleBusinessEnd,
  resolveManualEndDate,
  resolvePayOvertimeEnd,
  resolveUnlimitedExtension,
  toDate
});
