'use strict';
import { dateFormates } from '../../../fd-toolbox/dates/date-time.js';
import { getNow, parseExact, subtractTime } from '../../../fd-toolbox/time/date-time.js';
import { dateFormats } from '../../../fd-toolbox/dates/date-formats.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
const weekDays = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"];
const todayDate = formatDateKey(getNow());
function getCalendarDays(year, month) {
  const monthStart = startOfMonth(year, month);
  const monthEnd = endOfMonth(year, month);
  const firstWeekDay = (monthStart.getDay() + 6) % 7;
  const daysInMonth = monthEnd.getDate();
  const days = [];
  for (let i = firstWeekDay; i > 0; i--) {
    days.push({
      date: addDays(monthStart, -i),
      isCurrentMonth: false
    });
  }
  for (let day = 1; day <= daysInMonth; day++) {
    days.push({
      date: buildDate(year, month, day),
      isCurrentMonth: true
    });
  }
  while (days.length % 7 !== 0) {
    const lastDate = days[days.length - 1].date;
    days.push({
      date: addDays(lastDate, 1),
      isCurrentMonth: false
    });
  }
  return days;
}
__name(getCalendarDays, "getCalendarDays");
function formatEventTime(event) {
  const locale = navigator.language;
  return new Intl.DateTimeFormat(locale, {
    hour: dateFormates.numeric,
    minute: dateFormates.digit
  }).format(event.dateTime);
}
__name(formatEventTime, "formatEventTime");
function getDayCellData(days, index, eventsByDate) {
  const day = days[index];
  const dateKey = formatDateKey(day.date);
  return {
    date: day.date,
    dateKey,
    isCurrentMonth: day.isCurrentMonth,
    dayEvents: eventsByDate.get(dateKey) ?? [],
    isLastColumn: (index + 1) % 7 === 0,
    isLastRow: index >= days.length - 7
  };
}
__name(getDayCellData, "getDayCellData");
function buildEventsByDateMap(events) {
  const eventsByDate = /* @__PURE__ */new Map();
  for (const event of events) {
    const dateKey = formatDateKey(event.dateTime);
    const dayEvents = eventsByDate.get(dateKey);
    if (dayEvents) {
      dayEvents.push(event);
    } else {
      eventsByDate.set(dateKey, [event]);
    }
  }
  for (const dayEvents of eventsByDate.values()) {
    dayEvents.sort((a, b) => a.dateTime.getTime() - b.dateTime.getTime());
  }
  return eventsByDate;
}
__name(buildEventsByDateMap, "buildEventsByDateMap");
function buildDate(year, month, day) {
  return parseExact(`${year}-${month + 1}-${day} 00:00:00`, dateFormats.isoWithoutLeadingZeros);
}
__name(buildDate, "buildDate");
function addDays(date, days) {
  return subtractTime(date, -days * 24, 0);
}
__name(addDays, "addDays");
function startOfMonth(year, month) {
  return buildDate(year, month, 1);
}
__name(startOfMonth, "startOfMonth");
function endOfMonth(year, month) {
  const firstDayOfNextMonth = buildDate(year, month + 1, 1);
  return subtractTime(firstDayOfNextMonth, 24, 0);
}
__name(endOfMonth, "endOfMonth");
function formatDateKey(date) {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  return `${year}-${month}-${day}`;
}
__name(formatDateKey, "formatDateKey");

export { buildEventsByDateMap, formatEventTime, getCalendarDays, getDayCellData, todayDate, weekDays };
