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/modules/Date/utils.ts
var utils_exports = {};
__export(utils_exports, {
  disableAllDates: () => disableAllDates,
  disableDatesBeforeOneDay: () => disableDatesBeforeOneDay,
  generateMonthDates: () => generateMonthDates,
  handleAvailableDateByResource: () => handleAvailableDateByResource,
  handleAvailableDatesByRules: () => handleAvailableDatesByRules
});
module.exports = __toCommonJS(utils_exports);
var import_dayjs = __toESM(require("dayjs"));
var generateMonthDates = (startDate, endDate, type) => {
  const start = type === "day" ? (0, import_dayjs.default)(startDate).format("YYYY-MM-DD") : (0, import_dayjs.default)(startDate).format("YYYY-MM-01");
  const end = (0, import_dayjs.default)(endDate).format("YYYY-MM-DD");
  const dates = [];
  let currentDate = (0, import_dayjs.default)(start);
  while ((0, import_dayjs.default)(currentDate).isBefore((0, import_dayjs.default)(end), "day") || (0, import_dayjs.default)(currentDate).isSame((0, import_dayjs.default)(end), "day")) {
    dates.push({
      date: (0, import_dayjs.default)(currentDate).format("YYYY-MM-DD"),
      week: (0, import_dayjs.default)(currentDate).format("ddd"),
      weekNum: (0, import_dayjs.default)(currentDate).day(),
      status: "available"
    });
    currentDate = (0, import_dayjs.default)(currentDate).add(1, "day");
  }
  return dates;
};
var disableAllDates = (dates) => {
  return dates.map((date) => ({ ...date, status: "unavailable" }));
};
var disableDatesBeforeOneDay = (dates, oneDay) => {
  const day = oneDay ? (0, import_dayjs.default)(oneDay).format("YYYY-MM-DD") : (0, import_dayjs.default)().format("YYYY-MM-DD");
  return dates.map((date) => {
    if ((0, import_dayjs.default)(date.date).isBefore(day, "day")) {
      return { ...date, status: "unavailable" };
    } else {
      return date;
    }
  });
};
var handleAvailableDateByResource = (resources, dates) => {
  const dateMap = {};
  resources.forEach((resource) => {
    const { form_id, id, times, ...others } = resource;
    times.forEach((time) => {
      var _a;
      const { start_at } = time;
      const date = (0, import_dayjs.default)(start_at).format("YYYY-MM-DD");
      if (!dateMap[date]) {
        dateMap[date] = [{ form_id, id, ...others, times: [time] }];
      } else {
        const temp = (_a = dateMap[date].filter((item) => item.id === id)) == null ? void 0 : _a[0];
        if (temp) {
          temp.times.push(time);
        } else {
          dateMap[date].push({ form_id, id, ...others, times: [time] });
        }
      }
    });
  });
  return dates.map((date) => {
    const resource = dateMap[date.date];
    if (resource) {
      return { ...date, resource };
    } else {
      return { ...date, status: "unavailable", resource: [] };
    }
  });
};
var handleAvailableDatesByRules = (dates, rules) => {
  let newDates = disableDatesBeforeOneDay(dates);
  if (!(rules == null ? void 0 : rules.length)) {
    return newDates;
  }
  let scheduleMap = {};
  let latestStartDate = "";
  let earliestEndDate = "";
  let resourceFormIds = [];
  rules.forEach((rule) => {
    var _a, _b;
    if ((_a = rule == null ? void 0 : rule.schedule) == null ? void 0 : _a.length) {
      for (const item of rule.schedule) {
        if ((0, import_dayjs.default)(item.date).isBefore((0, import_dayjs.default)(newDates[0].date), "day")) {
          continue;
        }
        if ((0, import_dayjs.default)(item.date).isAfter(
          (0, import_dayjs.default)(newDates[newDates.length - 1].date),
          "day"
        )) {
          break;
        }
        if (!scheduleMap[item.date]) {
          scheduleMap[item.date] = item;
        } else {
          if (!item.isExcluded) {
            scheduleMap[item.date].isExcluded = false;
          }
        }
      }
    }
    const { future_day, unit, unit_type } = (rule == null ? void 0 : rule.cut_off_time) || {};
    if (future_day) {
      const endDate = (0, import_dayjs.default)().add(future_day, "day");
      if (!earliestEndDate || endDate.isBefore((0, import_dayjs.default)(earliestEndDate), "day")) {
        earliestEndDate = endDate.format("YYYY-MM-DD");
      }
    } else if (future_day === 0) {
      const endDate = (0, import_dayjs.default)();
      if (!earliestEndDate || endDate.isBefore((0, import_dayjs.default)(earliestEndDate), "day")) {
        earliestEndDate = endDate.format("YYYY-MM-DD");
      }
    }
    if (unit && unit_type) {
      const startDate = (0, import_dayjs.default)(
        (0, import_dayjs.default)().add(unit, unit_type).format("YYYY-MM-DD")
      );
      if (!latestStartDate || startDate.isAfter((0, import_dayjs.default)(latestStartDate), "day")) {
        latestStartDate = startDate.format("YYYY-MM-DD");
      }
    }
    if ((_b = rule == null ? void 0 : rule.resourceFormIds) == null ? void 0 : _b.length) {
      resourceFormIds = [
        .../* @__PURE__ */ new Set([...resourceFormIds, ...rule == null ? void 0 : rule.resourceFormIds])
      ];
    }
  });
  newDates = newDates.map((date) => {
    var _a;
    const disabledDate = { ...date, status: "unavailable" };
    const hasScheduleConfig = rules.find((n) => {
      var _a2;
      return (_a2 = n.schedule) == null ? void 0 : _a2.length;
    });
    if (Object.keys(scheduleMap).length === 0 && hasScheduleConfig) {
      return { ...date, color: "", status: "unavailable" };
    }
    if (Object.keys(scheduleMap).length) {
      const schedule = scheduleMap[date.date];
      if (!schedule || (schedule == null ? void 0 : schedule.isExcluded)) {
        return { ...date, color: schedule == null ? void 0 : schedule.color, status: "unavailable" };
      } else {
        date.color = schedule == null ? void 0 : schedule.color;
        disabledDate.color = schedule == null ? void 0 : schedule.color;
      }
    }
    if (!((_a = date == null ? void 0 : date.resource) == null ? void 0 : _a.length)) {
      return disabledDate;
    }
    if (latestStartDate || earliestEndDate) {
      if (latestStartDate && (0, import_dayjs.default)(date.date).isBefore(latestStartDate, "day")) {
        return disabledDate;
      }
      if (earliestEndDate && (0, import_dayjs.default)(date.date).isAfter(earliestEndDate, "day")) {
        return disabledDate;
      }
    }
    if (resourceFormIds == null ? void 0 : resourceFormIds.length) {
      let hasReourceCount = 0;
      resourceFormIds.forEach((formId) => {
        var _a2;
        if (((_a2 = date == null ? void 0 : date.resource) == null ? void 0 : _a2.find((item) => item.form_id === formId)) !== void 0) {
          hasReourceCount++;
        }
      });
      if (hasReourceCount < resourceFormIds.length) {
        return disabledDate;
      }
    }
    return date;
  });
  return newDates;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  disableAllDates,
  disableDatesBeforeOneDay,
  generateMonthDates,
  handleAvailableDateByResource,
  handleAvailableDatesByRules
});
