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/plus/pisellReservation/data/reservationDensity.ts
var reservationDensity_exports = {};
__export(reservationDensity_exports, {
  computeReservationDensityBuckets: () => computeReservationDensityBuckets,
  fitTimelineHighlightsToSlotCount: () => fitTimelineHighlightsToSlotCount,
  normalizeSalesTimelineHighlightsRaw: () => normalizeSalesTimelineHighlightsRaw,
  reservationTimelineAxisSlotCount: () => reservationTimelineAxisSlotCount,
  reservationTimelineRangeStrings: () => reservationTimelineRangeStrings
});
module.exports = __toCommonJS(reservationDensity_exports);
var import_dayjs = __toESM(require("dayjs"));
var import_materials = require("@pisell/materials");
var import_reservationConstants = require("../reservationConstants");
var import_usePisellReservationBookingData = require("../hooks/usePisellReservationBookingData");
var TIMELINE_RANGE_FORMAT = "YYYY-MM-DD HH:mm";
function reservationTimelineRangeStrings(scheduleDate, range = import_reservationConstants.RESERVATION_TIMELINE_BUSINESS_RANGE, operatingDayBoundary, wallClock = (0, import_dayjs.default)()) {
  const [rangeStart, rangeEnd] = (0, import_usePisellReservationBookingData.bookingTimeWindowForReservationView)(
    scheduleDate,
    operatingDayBoundary,
    range,
    wallClock
  );
  return {
    startStr: rangeStart.format(TIMELINE_RANGE_FORMAT),
    endStr: rangeEnd.format(TIMELINE_RANGE_FORMAT)
  };
}
function normalizeSalesTimelineHighlightsRaw(raw) {
  let values;
  if (Array.isArray(raw)) {
    values = raw;
  } else if (raw != null && typeof raw === "object") {
    if (Symbol.iterator in raw) {
      try {
        values = Array.from(raw);
      } catch {
        return null;
      }
    } else {
      const keys = Object.keys(raw).filter((k) => /^\d+$/.test(k)).sort((a, b) => Number(a) - Number(b));
      if (keys.length === 0) return null;
      values = keys.map((k) => raw[k]);
    }
  } else {
    return null;
  }
  const out = values.map((v) => {
    const n = typeof v === "number" ? v : Number(v);
    return Number.isFinite(n) ? Math.max(0, Math.round(n)) : 0;
  });
  return out.length > 0 ? out : null;
}
function fitTimelineHighlightsToSlotCount(series, slotCount) {
  const n = Math.max(1, slotCount);
  if (series.length === n) return [...series];
  if (series.length > n) return series.slice(0, n);
  return [...series, ...new Array(n - series.length).fill(0)];
}
function reservationTimelineAxisSlotCount(referenceDate, stepMinutes, range = import_reservationConstants.RESERVATION_TIMELINE_BUSINESS_RANGE) {
  return (0, import_materials.getHorizontalAxisSlotCount)(
    range,
    referenceDate.startOf("day"),
    stepMinutes
  );
}
function computeReservationDensityBuckets(densityRows, referenceDate, stepMinutes) {
  const day = referenceDate.startOf("day");
  const slotCount = Math.max(
    1,
    Math.round(24 * 60 / Math.max(1, stepMinutes))
  );
  const buckets = new Array(slotCount).fill(0);
  for (const row of densityRows) {
    const raw = row.bookingTimeStart ?? row.start_at ?? (row.start_date && row.start_time ? `${row.start_date} ${row.start_time}` : void 0);
    if (!raw || typeof raw !== "string") continue;
    const t = (0, import_dayjs.default)(raw);
    if (!t.isValid() || !t.isSame(day, "day")) continue;
    const idx = Math.min(
      slotCount - 1,
      Math.floor((t.hour() * 60 + t.minute()) / stepMinutes)
    );
    buckets[idx] += 1;
  }
  if (!buckets.some((v) => v > 0)) {
    return buckets.map((_, i) => {
      const waveA = Math.sin(i / Math.max(1, slotCount - 1) * Math.PI * 2);
      const waveB = Math.sin(i / Math.max(1, slotCount - 1) * Math.PI * 6);
      return Math.max(0, Math.round(2 + waveA * 2 + waveB));
    });
  }
  return buckets;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  computeReservationDensityBuckets,
  fitTimelineHighlightsToSlotCount,
  normalizeSalesTimelineHighlightsRaw,
  reservationTimelineAxisSlotCount,
  reservationTimelineRangeStrings
});
