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/components/pisellTimeNavigator/utils/index.ts
var utils_exports = {};
__export(utils_exports, {
  DEFAULT_CALENDAR_SLOT_STEP_MINUTES: () => DEFAULT_CALENDAR_SLOT_STEP_MINUTES,
  buildCalendarHourSlotsFromRange: () => buildCalendarHourSlotsFromRange,
  buildCalendarTimelineSlotStartsFromRange: () => buildCalendarTimelineSlotStartsFromRange,
  getAxisCells: () => getAxisCells,
  getHorizontalAxisSlotCount: () => getHorizontalAxisSlotCount,
  getRangeBounds: () => getRangeBounds,
  getTicks: () => getTicks,
  isInDisabledRanges: () => isInDisabledRanges,
  parseValue: () => parseValue,
  pickReferenceDateContainingNow: () => pickReferenceDateContainingNow,
  snapToStepMinutes: () => snapToStepMinutes
});
module.exports = __toCommonJS(utils_exports);
var import_dayjs = __toESM(require("dayjs"));
function parseTimeOfDay(timeStr, reference) {
  const [h, m, s] = timeStr.trim().split(":").map(Number);
  return reference.hour(isNaN(h) ? 0 : h).minute(isNaN(m) ? 0 : m).second(isNaN(s) ? 0 : s).millisecond(0);
}
var TIME_ONLY_REGEX = /^\s*\d{1,2}:\d{2}(:\d{2})?\s*$/;
function parseValue(value, reference) {
  if (value == null) return null;
  const ref = reference ?? (0, import_dayjs.default)();
  if (typeof value === "string" && TIME_ONLY_REGEX.test(value)) {
    return parseTimeOfDay(value, ref);
  }
  const d = (0, import_dayjs.default)(value);
  return d.isValid() ? d : null;
}
var DEFAULT_CALENDAR_SLOT_STEP_MINUTES = 60;
function buildCalendarTimelineSlotStartsFromRange(range, anchorDate, slotStepMinutes = DEFAULT_CALENDAR_SLOT_STEP_MINUTES) {
  const step = Math.max(
    1,
    Math.min(24 * 60, Math.round(Number(slotStepMinutes)) || 60)
  );
  const ref = (0, import_dayjs.default)(anchorDate).startOf("day");
  const [rs, re] = getRangeBounds(range, ref);
  const out = [];
  let cur = rs;
  while (cur.isBefore(re)) {
    out.push(cur.diff(ref, "minute", true) / 60);
    cur = cur.add(step, "minute");
  }
  if (out.length === 0) {
    for (let m = 8 * 60; m < 20 * 60; m += step) out.push(m / 60);
  }
  return out;
}
function buildCalendarHourSlotsFromRange(range, anchorDate) {
  return buildCalendarTimelineSlotStartsFromRange(
    range,
    anchorDate,
    DEFAULT_CALENDAR_SLOT_STEP_MINUTES
  );
}
function getRangeBounds(range, reference) {
  if (!range) {
    return [reference.startOf("day"), reference.endOf("day")];
  }
  const start = parseTimeOfDay(range.start, reference);
  let end = parseTimeOfDay(range.end, reference);
  if (end.isBefore(start) || end.isSame(start)) {
    end = parseTimeOfDay(range.end, reference.add(1, "day"));
  }
  return [start, end];
}
function pickReferenceDateContainingNow(range, now) {
  const today = now.startOf("day");
  const candidates = [today, today.subtract(1, "day"), today.add(1, "day")];
  for (const cand of candidates) {
    const [s, e] = getRangeBounds(range, cand);
    if (!now.isBefore(s) && !now.isAfter(e)) {
      return cand;
    }
  }
  return today;
}
function snapToStepMinutes(t, stepMinutes) {
  const m = t.minute();
  const snapped = Math.round(m / stepMinutes) * stepMinutes;
  if (snapped >= 60) {
    return t.add(1, "hour").minute(0).second(0).millisecond(0);
  }
  return t.minute(snapped).second(0).millisecond(0);
}
function isInDisabledRanges(t, disabledRanges) {
  if (!(disabledRanges == null ? void 0 : disabledRanges.length)) return false;
  const tMin = t.startOf("minute");
  for (const r of disabledRanges) {
    const [s, e] = getRangeBounds(r, t.startOf("day"));
    if ((tMin.isAfter(s) || tMin.isSame(s)) && tMin.isBefore(e)) return true;
  }
  return false;
}
function getTicks(rangeStart, rangeEnd, stepMinutes) {
  const out = [];
  let cur = rangeStart;
  while (!cur.isAfter(rangeEnd)) {
    out.push(cur);
    cur = cur.add(stepMinutes, "minute");
  }
  return out;
}
function getAxisCells(rangeStart, rangeEnd, stepMinutes) {
  const out = [];
  if (!rangeEnd.isAfter(rangeStart) || stepMinutes <= 0) return out;
  const startMin = rangeStart.minute();
  const remainder = (startMin % stepMinutes + stepMinutes) % stepMinutes;
  const isStartAligned = remainder === 0 && rangeStart.second() === 0 && rangeStart.millisecond() === 0;
  const firstAligned = isStartAligned ? rangeStart : rangeStart.startOf("hour").minute(startMin - remainder + stepMinutes).second(0).millisecond(0);
  if (firstAligned.isAfter(rangeStart)) {
    const headMin = firstAligned.diff(rangeStart, "minute", true);
    if (headMin > 0) {
      out.push({ start: rangeStart, widthMinutes: headMin });
    }
  }
  let cur = firstAligned;
  const safetyLimit = 2 * 24 * 60 / stepMinutes + 4;
  while (cur.isBefore(rangeEnd) && out.length < safetyLimit) {
    const next = cur.add(stepMinutes, "minute");
    if (next.isAfter(rangeEnd)) {
      const tailMin = rangeEnd.diff(cur, "minute", true);
      if (tailMin > 0) {
        out.push({ start: cur, widthMinutes: tailMin });
      }
      break;
    }
    out.push({ start: cur, widthMinutes: stepMinutes });
    cur = next;
  }
  return out;
}
function getHorizontalAxisSlotCount(range, referenceDate, stepMinutes) {
  const [rangeStart, rangeEnd] = getRangeBounds(range, referenceDate);
  return getAxisCells(rangeStart, rangeEnd, stepMinutes).length;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DEFAULT_CALENDAR_SLOT_STEP_MINUTES,
  buildCalendarHourSlotsFromRange,
  buildCalendarTimelineSlotStartsFromRange,
  getAxisCells,
  getHorizontalAxisSlotCount,
  getRangeBounds,
  getTicks,
  isInDisabledRanges,
  parseValue,
  pickReferenceDateContainingNow,
  snapToStepMinutes
});
