"use strict";
require("./index-QSf6WJj0.cjs");
const dayjs = require("dayjs");
const defaultDayRange = {
  start: new Date((/* @__PURE__ */ new Date()).setUTCHours(9, 0, 0, 0)),
  end: new Date((/* @__PURE__ */ new Date()).setUTCHours(17, 0, 0, 0))
};
const DEFAULT_SCHEDULE = [
  [],
  [defaultDayRange],
  [defaultDayRange],
  [defaultDayRange],
  [defaultDayRange],
  [defaultDayRange],
  []
];
function getAvailabilityFromSchedule(schedule) {
  return schedule.reduce((availability, times, day) => {
    const addNewTime = (time) => ({
      days: [day],
      startTime: time.start,
      endTime: time.end
    });
    const filteredTimes = times.filter((time) => {
      let idx;
      if ((idx = availability.findIndex(
        (schedule2) => schedule2.startTime.toString() === time.start.toString() && schedule2.endTime.toString() === time.end.toString()
      )) !== -1) {
        availability[idx].days.push(day);
        return false;
      }
      return true;
    });
    filteredTimes.forEach((time) => {
      availability.push(addNewTime(time));
    });
    return availability;
  }, []);
}
const MINUTES_IN_DAY = 60 * 24;
const MINUTES_DAY_END = MINUTES_IN_DAY - 1;
const MINUTES_DAY_START = 0;
function getWorkingHours(relativeTimeUnit, availability) {
  if (!availability.length) {
    return [];
  }
  const utcOffset = relativeTimeUnit.utcOffset ?? (relativeTimeUnit.timeZone ? dayjs().tz(relativeTimeUnit.timeZone).utcOffset() : 0);
  const workingHours = availability.reduce((currentWorkingHours, schedule) => {
    if (!schedule.days.length)
      return currentWorkingHours;
    const startTime = dayjs.utc(schedule.startTime).get("hour") * 60 + dayjs.utc(schedule.startTime).get("minute") - utcOffset;
    const endTime = dayjs.utc(schedule.endTime).get("hour") * 60 + dayjs.utc(schedule.endTime).get("minute") - utcOffset;
    const sameDayStartTime = Math.max(MINUTES_DAY_START, Math.min(MINUTES_DAY_END, startTime));
    const sameDayEndTime = Math.max(MINUTES_DAY_START, Math.min(MINUTES_DAY_END, endTime));
    if (sameDayEndTime < sameDayStartTime) {
      return currentWorkingHours;
    }
    if (sameDayStartTime !== sameDayEndTime) {
      const newWorkingHours = {
        days: schedule.days,
        startTime: sameDayStartTime,
        endTime: sameDayEndTime
      };
      if (schedule.userId)
        newWorkingHours.userId = schedule.userId;
      currentWorkingHours.push(newWorkingHours);
    }
    if (startTime < MINUTES_DAY_START || endTime < MINUTES_DAY_START) {
      const newWorkingHours = {
        days: schedule.days.map((day) => day - 1 >= 0 ? day - 1 : 6),
        startTime: startTime + MINUTES_IN_DAY,
        endTime: Math.min(endTime + MINUTES_IN_DAY, MINUTES_DAY_END)
      };
      if (schedule.userId)
        newWorkingHours.userId = schedule.userId;
      currentWorkingHours.push(newWorkingHours);
    } else if (startTime > MINUTES_DAY_END || endTime > MINUTES_IN_DAY) {
      const newWorkingHours = {
        days: schedule.days.map((day) => (day + 1) % 7),
        startTime: Math.max(startTime - MINUTES_IN_DAY, MINUTES_DAY_START),
        endTime: endTime - MINUTES_IN_DAY
      };
      if (schedule.userId)
        newWorkingHours.userId = schedule.userId;
      currentWorkingHours.push(newWorkingHours);
    }
    return currentWorkingHours;
  }, []);
  workingHours.sort((a, b) => a.startTime - b.startTime);
  return workingHours;
}
exports.DEFAULT_SCHEDULE = DEFAULT_SCHEDULE;
exports.getAvailabilityFromSchedule = getAvailabilityFromSchedule;
exports.getWorkingHours = getWorkingHours;
