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/server/modules/schedule/utils.ts
var utils_exports = {};
__export(utils_exports, {
  getDateIsInSchedule: () => getDateIsInSchedule,
  getScheduleStartEndTimePoints: () => getScheduleStartEndTimePoints
});
module.exports = __toCommonJS(utils_exports);
var import_dayjs = __toESM(require("dayjs"));
var getDateIsInSchedule = (dateTime, scheduleList) => {
  if (!dateTime || !scheduleList || scheduleList.length === 0) {
    return false;
  }
  const targetDate = (0, import_dayjs.default)(dateTime);
  if (!targetDate.isValid()) {
    return false;
  }
  for (const schedule of scheduleList) {
    if (isTimeInScheduleItem(dateTime, schedule)) {
      return true;
    }
  }
  return false;
};
var isTimeInScheduleItem = (dateTime, schedule) => {
  const targetDate = (0, import_dayjs.default)(dateTime);
  const targetDateString = dateTime.split(" ")[0];
  const targetTimeString = dateTime.split(" ")[1] || "00:00:00";
  switch (schedule.type) {
    case "standard":
      return isInStandardSchedule(targetDate, targetDateString, targetTimeString, schedule);
    case "time-slots":
      return isInTimeSlotsSchedule(targetDate, targetDateString, targetTimeString, schedule);
    case "designation":
      return isInDesignationSchedule(targetDate, targetDateString, targetTimeString, schedule);
    default:
      return false;
  }
};
var isInStandardSchedule = (targetDate, targetDateString, targetTimeString, schedule) => {
  if (!schedule.start_time || !schedule.end_time) {
    return false;
  }
  const startDate = (0, import_dayjs.default)(schedule.start_time);
  const endDate = (0, import_dayjs.default)(schedule.end_time);
  const isInBasicRange = targetDate.isSameOrAfter(startDate) && targetDate.isSameOrBefore(endDate);
  if (schedule.repeat_type === "none") {
    return isInBasicRange;
  }
  return isInRepeatingSchedule(targetDate, targetDateString, targetTimeString, schedule, startDate, endDate);
};
var isInTimeSlotsSchedule = (targetDate, targetDateString, targetTimeString, schedule) => {
  if (!schedule.start_time) {
    return false;
  }
  const scheduleDate = schedule.start_time.split(" ")[0];
  if (targetDateString !== scheduleDate) {
    return false;
  }
  for (const timeSlot of schedule.time_slot) {
    const slotStart = `${scheduleDate} ${timeSlot.start_time}:00`;
    const slotEnd = `${scheduleDate} ${timeSlot.end_time}:00`;
    const slotStartDate = (0, import_dayjs.default)(slotStart);
    const slotEndDate = (0, import_dayjs.default)(slotEnd);
    if (targetDate.isSameOrAfter(slotStartDate) && targetDate.isSameOrBefore(slotEndDate)) {
      return true;
    }
  }
  return false;
};
var isInDesignationSchedule = (targetDate, targetDateString, targetTimeString, schedule) => {
  if (!schedule.designation) {
    return false;
  }
  for (const designation of schedule.designation) {
    const startDateTime = `${designation.start_date} ${designation.start_time}:00`;
    const endDateTime = `${designation.end_date} ${designation.end_time}:00`;
    const startDate = (0, import_dayjs.default)(startDateTime);
    const endDate = (0, import_dayjs.default)(endDateTime);
    if (targetDate.isSameOrAfter(startDate) && targetDate.isSameOrBefore(endDate)) {
      return true;
    }
  }
  return false;
};
var isInRepeatingSchedule = (targetDate, targetDateString, targetTimeString, schedule, startDate, endDate) => {
  if (!schedule.repeat_rule) {
    return false;
  }
  const { repeat_rule, repeat_type } = schedule;
  const targetDateOnly = (0, import_dayjs.default)(targetDateString);
  if (repeat_rule.included_date && repeat_rule.included_date.length > 0) {
    for (const includedDate of repeat_rule.included_date) {
      const includeStartDate = (0, import_dayjs.default)(includedDate.start);
      const includeEndDate = (0, import_dayjs.default)(includedDate.end);
      if (targetDateOnly.isSameOrAfter(includeStartDate, "day") && targetDateOnly.isSameOrBefore(includeEndDate, "day")) {
        return true;
      }
    }
  }
  for (const excludedDate of repeat_rule.excluded_date) {
    const excludeStartDate = (0, import_dayjs.default)(excludedDate.start);
    const excludeEndDate = (0, import_dayjs.default)(excludedDate.end);
    if (targetDateOnly.isSameOrAfter(excludeStartDate, "day") && targetDateOnly.isSameOrBefore(excludeEndDate, "day")) {
      return false;
    }
  }
  if (repeat_rule.end.type === "date" && repeat_rule.end.end_date) {
    const ruleEndDate = (0, import_dayjs.default)(repeat_rule.end.end_date);
    if (targetDate.isAfter(ruleEndDate)) {
      return false;
    }
  }
  switch (repeat_type) {
    case "daily":
      return isInDailyRepeat(targetDate, startDate, endDate, repeat_rule);
    case "weekly":
      return isInWeeklyRepeat(targetDate, startDate, endDate, repeat_rule);
    default:
      return false;
  }
};
var isInDailyRepeat = (targetDate, startDate, endDate, repeatRule) => {
  const daysDiff = targetDate.diff(startDate, "day");
  if (daysDiff < 0) {
    return false;
  }
  if (daysDiff % repeatRule.frequency !== 0) {
    return false;
  }
  const targetTimeOfDay = targetDate.hour() * 3600 + targetDate.minute() * 60 + targetDate.second();
  const startTimeOfDay = startDate.hour() * 3600 + startDate.minute() * 60 + startDate.second();
  const endTimeOfDay = endDate.hour() * 3600 + endDate.minute() * 60 + endDate.second();
  return targetTimeOfDay >= startTimeOfDay && targetTimeOfDay <= endTimeOfDay;
};
var isInWeeklyRepeat = (targetDate, startDate, endDate, repeatRule) => {
  const targetDayOfWeek = targetDate.day();
  if (!repeatRule.frequency_date.includes(targetDayOfWeek)) {
    return false;
  }
  const weeksDiff = targetDate.diff(startDate, "week");
  if (weeksDiff < 0) {
    return false;
  }
  if (weeksDiff % repeatRule.frequency !== 0) {
    return false;
  }
  const targetTimeOfDay = targetDate.hour() * 3600 + targetDate.minute() * 60 + targetDate.second();
  const startTimeOfDay = startDate.hour() * 3600 + startDate.minute() * 60 + startDate.second();
  const endTimeOfDay = endDate.hour() * 3600 + endDate.minute() * 60 + endDate.second();
  return targetTimeOfDay >= startTimeOfDay && targetTimeOfDay <= endTimeOfDay;
};
var getScheduleStartEndTimePoints = (date, scheduleList) => {
  if (!date || !scheduleList || scheduleList.length === 0) {
    return [];
  }
  const targetDate = (0, import_dayjs.default)(date);
  if (!targetDate.isValid()) {
    return [];
  }
  const timeSlots = [];
  for (const schedule of scheduleList) {
    const slots = extractTimeSlotsFromSchedule(date, schedule);
    timeSlots.push(...slots);
  }
  return mergeAndSortTimeSlots(timeSlots);
};
var extractTimeSlotsFromSchedule = (date, schedule) => {
  switch (schedule.type) {
    case "standard":
      return extractStandardScheduleTimeSlots(date, schedule);
    case "time-slots":
      return extractTimeSlotsScheduleTimeSlots(date, schedule);
    case "designation":
      return extractDesignationScheduleTimeSlots(date, schedule);
    default:
      return [];
  }
};
var extractStandardScheduleTimeSlots = (date, schedule) => {
  if (!schedule.start_time || !schedule.end_time) {
    return [];
  }
  const targetDate = (0, import_dayjs.default)(date);
  const startDate = (0, import_dayjs.default)(schedule.start_time);
  const endDate = (0, import_dayjs.default)(schedule.end_time);
  if (!isDateInStandardSchedule(targetDate, schedule, startDate, endDate)) {
    return [];
  }
  const startTime = startDate.format("HH:mm");
  const endTime = endDate.format("HH:mm");
  const startAt = (0, import_dayjs.default)(`${date} ${startTime}:00`);
  const endAt = (0, import_dayjs.default)(`${date} ${endTime}:00`);
  return [{
    start_time: startTime,
    end_time: endTime,
    start_at: startAt,
    end_at: endAt
  }];
};
var extractTimeSlotsScheduleTimeSlots = (date, schedule) => {
  if (!schedule.start_time || !schedule.time_slot || schedule.time_slot.length === 0) {
    return [];
  }
  const targetDate = (0, import_dayjs.default)(date);
  const scheduleStartDate = (0, import_dayjs.default)(schedule.start_time.split(" ")[0]);
  if (!isDateInTimeSlotsSchedule(targetDate, schedule, scheduleStartDate)) {
    return [];
  }
  const slots = [];
  for (const timeSlot of schedule.time_slot) {
    if (!timeSlot.start_time || !timeSlot.end_time) {
      continue;
    }
    const startAt = (0, import_dayjs.default)(`${date} ${timeSlot.start_time}:00`);
    const endAt = (0, import_dayjs.default)(`${date} ${timeSlot.end_time}:00`);
    slots.push({
      start_time: timeSlot.start_time,
      end_time: timeSlot.end_time,
      start_at: startAt,
      end_at: endAt
    });
  }
  return slots;
};
var isDateInTimeSlotsSchedule = (targetDate, schedule, scheduleStartDate) => {
  if (schedule.repeat_type === "none") {
    return targetDate.isSame(scheduleStartDate, "day");
  }
  if (!schedule.repeat_rule) {
    return false;
  }
  const { repeat_rule, repeat_type } = schedule;
  if (repeat_rule.included_date && repeat_rule.included_date.length > 0) {
    for (const includedDate of repeat_rule.included_date) {
      const includeStartDate = (0, import_dayjs.default)(includedDate.start);
      const includeEndDate = (0, import_dayjs.default)(includedDate.end);
      if (targetDate.isSameOrAfter(includeStartDate, "day") && targetDate.isSameOrBefore(includeEndDate, "day")) {
        return true;
      }
    }
  }
  if (repeat_rule.excluded_date && repeat_rule.excluded_date.length > 0) {
    for (const excludedDate of repeat_rule.excluded_date) {
      const excludeStartDate = (0, import_dayjs.default)(excludedDate.start);
      const excludeEndDate = (0, import_dayjs.default)(excludedDate.end);
      if (targetDate.isSameOrAfter(excludeStartDate, "day") && targetDate.isSameOrBefore(excludeEndDate, "day")) {
        return false;
      }
    }
  }
  if (repeat_rule.end.type === "date" && repeat_rule.end.end_date) {
    const ruleEndDate = (0, import_dayjs.default)(repeat_rule.end.end_date);
    if (targetDate.isAfter(ruleEndDate, "day")) {
      return false;
    }
  }
  if (targetDate.isBefore(scheduleStartDate, "day")) {
    return false;
  }
  switch (repeat_type) {
    case "daily":
      return isDateInDailyRepeat(targetDate, scheduleStartDate, repeat_rule);
    case "weekly":
      return isDateInWeeklyRepeat(targetDate, scheduleStartDate, repeat_rule);
    default:
      return false;
  }
};
var extractDesignationScheduleTimeSlots = (date, schedule) => {
  if (!schedule.designation) {
    return [];
  }
  const targetDate = (0, import_dayjs.default)(date);
  const designations = Array.isArray(schedule.designation) ? schedule.designation : [schedule.designation];
  const slots = [];
  for (const designation of designations) {
    const startDate = (0, import_dayjs.default)(designation.start_date);
    const endDate = (0, import_dayjs.default)(designation.end_date);
    if (isDateInDesignationSchedule(targetDate, schedule, designation, startDate, endDate)) {
      const startAt = (0, import_dayjs.default)(`${date} ${designation.start_time}:00`);
      const endAt = (0, import_dayjs.default)(`${date} ${designation.end_time}:00`);
      slots.push({
        start_time: designation.start_time,
        end_time: designation.end_time,
        start_at: startAt,
        end_at: endAt
      });
    }
  }
  return slots;
};
var isDateInDesignationSchedule = (targetDate, schedule, designation, startDate, endDate) => {
  if (schedule.repeat_type === "none") {
    return targetDate.isSameOrAfter(startDate, "day") && targetDate.isSameOrBefore(endDate, "day");
  }
  if (!schedule.repeat_rule) {
    return false;
  }
  const { repeat_rule, repeat_type } = schedule;
  if (repeat_rule.included_date && repeat_rule.included_date.length > 0) {
    for (const includedDate of repeat_rule.included_date) {
      const includeStartDate = (0, import_dayjs.default)(includedDate.start);
      const includeEndDate = (0, import_dayjs.default)(includedDate.end);
      if (targetDate.isSameOrAfter(includeStartDate, "day") && targetDate.isSameOrBefore(includeEndDate, "day")) {
        return true;
      }
    }
  }
  if (repeat_rule.excluded_date && repeat_rule.excluded_date.length > 0) {
    for (const excludedDate of repeat_rule.excluded_date) {
      const excludeStartDate = (0, import_dayjs.default)(excludedDate.start);
      const excludeEndDate = (0, import_dayjs.default)(excludedDate.end);
      if (targetDate.isSameOrAfter(excludeStartDate, "day") && targetDate.isSameOrBefore(excludeEndDate, "day")) {
        return false;
      }
    }
  }
  if (repeat_rule.end.type === "date" && repeat_rule.end.end_date) {
    const ruleEndDate = (0, import_dayjs.default)(repeat_rule.end.end_date);
    if (targetDate.isAfter(ruleEndDate, "day")) {
      return false;
    }
  }
  if (!targetDate.isSameOrAfter(startDate, "day") || !targetDate.isSameOrBefore(endDate, "day")) {
    if (targetDate.isBefore(startDate, "day")) {
      return false;
    }
  }
  switch (repeat_type) {
    case "daily":
      return isDateInDailyRepeat(targetDate, startDate, repeat_rule);
    case "weekly":
      return isDateInWeeklyRepeat(targetDate, startDate, repeat_rule);
    default:
      return false;
  }
};
var isDateInStandardSchedule = (targetDate, schedule, startDate, endDate) => {
  const targetDateString = targetDate.format("YYYY-MM-DD");
  if (schedule.repeat_type === "none") {
    const scheduleStartDate = startDate.format("YYYY-MM-DD");
    const scheduleEndDate = endDate.format("YYYY-MM-DD");
    return targetDate.isSameOrAfter(scheduleStartDate, "day") && targetDate.isSameOrBefore(scheduleEndDate, "day");
  }
  if (!schedule.repeat_rule) {
    return false;
  }
  const { repeat_rule, repeat_type } = schedule;
  if (repeat_rule.included_date && repeat_rule.included_date.length > 0) {
    for (const includedDate of repeat_rule.included_date) {
      const includeStartDate = (0, import_dayjs.default)(includedDate.start);
      const includeEndDate = (0, import_dayjs.default)(includedDate.end);
      if (targetDate.isSameOrAfter(includeStartDate, "day") && targetDate.isSameOrBefore(includeEndDate, "day")) {
        return true;
      }
    }
  }
  for (const excludedDate of repeat_rule.excluded_date) {
    const excludeStartDate = (0, import_dayjs.default)(excludedDate.start);
    const excludeEndDate = (0, import_dayjs.default)(excludedDate.end);
    if (targetDate.isSameOrAfter(excludeStartDate, "day") && targetDate.isSameOrBefore(excludeEndDate, "day")) {
      return false;
    }
  }
  if (repeat_rule.end.type === "date" && repeat_rule.end.end_date) {
    const ruleEndDate = (0, import_dayjs.default)(repeat_rule.end.end_date);
    if (targetDate.isAfter(ruleEndDate, "day")) {
      return false;
    }
  }
  if (targetDate.isBefore(startDate, "day")) {
    return false;
  }
  switch (repeat_type) {
    case "daily":
      return isDateInDailyRepeat(targetDate, startDate, repeat_rule);
    case "weekly":
      return isDateInWeeklyRepeat(targetDate, startDate, repeat_rule);
    default:
      return false;
  }
};
var isDateInDailyRepeat = (targetDate, startDate, repeatRule) => {
  const daysDiff = targetDate.diff(startDate, "day");
  if (daysDiff < 0) {
    return false;
  }
  return daysDiff % repeatRule.frequency === 0;
};
var isDateInWeeklyRepeat = (targetDate, startDate, repeatRule) => {
  const targetDayOfWeek = targetDate.day();
  if (!repeatRule.frequency_date.includes(targetDayOfWeek)) {
    return false;
  }
  const weeksDiff = targetDate.diff(startDate, "week");
  if (weeksDiff < 0) {
    return false;
  }
  return weeksDiff % repeatRule.frequency === 0;
};
var mergeAndSortTimeSlots = (slots) => {
  if (slots.length === 0) {
    return [];
  }
  const sorted = [...slots].sort((a, b) => {
    return a.start_at.valueOf() - b.start_at.valueOf();
  });
  const unique = [];
  const seenKeys = /* @__PURE__ */ new Set();
  for (const slot of sorted) {
    const key = `${slot.start_time}-${slot.end_time}`;
    if (!seenKeys.has(key)) {
      seenKeys.add(key);
      unique.push(slot);
    }
  }
  return unique;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getDateIsInSchedule,
  getScheduleStartEndTimePoints
});
