import padStart from "lodash.padstart";
import isNil from "lodash.isnil";
import { BalDate } from "../../../utils/date";
export function getDaysInMonth(year, month) {
  if (month < 1 || year < 0) {
    return undefined;
  }
  return new Date(year, month, 0).getDate();
}
export function emptyCell() {
  return {
    day: 0,
    month: 0,
    year: 0,
    isoDate: '',
    fullDate: '',
    today: false,
    disabled: false,
  };
}
function isoDateOfDay(day, month, year) {
  return `${year}-${padStart(`${month}`, 2, '0')}-${padStart(`${day}`, 2, '0')}`;
}
function isoDateOfToday() {
  const today = new Date();
  return `${today.getFullYear()}-${padStart(`${today.getMonth() + 1}`, 2, '0')}-${padStart(`${today.getDate()}`, 2, '0')}`;
}
export function generateCalendarGrid(year, month, min, max, allowedDates) {
  const grid = [];
  const numDays = getDaysInMonth(year, month);
  const firstDay = getFirstDayOfMonth(year, month);
  if (numDays !== undefined && firstDay) {
    const isoToday = isoDateOfToday();
    const dateMin = BalDate.fromISO(min);
    const dateMax = BalDate.fromISO(max);
    const allowedDate = (isoDate) => {
      if (isNil(allowedDates)) {
        return false;
      }
      return !allowedDates(isoDate);
    };
    for (let day = 1; day <= numDays; day++) {
      const isoDate = isoDateOfDay(day, month, year);
      grid.push({
        day,
        month,
        year,
        isoDate,
        fullDate: BalDate.fromISO(isoDate).toFormat(),
        today: isoToday === isoDate,
        disabled: dateMin.isAfter(isoDate) || dateMax.isBefore(isoDate) || allowedDate(isoDate),
      });
    }
  }
  return grid;
}
export function getFirstWeekdayOfMonth(year, month) {
  if (month < 1 || year < 0) {
    return 1;
  }
  const date = new Date(year, month - 1, 1);
  const weekday = date.getDay();
  return weekday === 0 ? 7 : weekday;
}
export function validateLanguage(language) {
  if (['en', 'de', 'fr', 'it', 'nl', 'es', 'pl', 'pt', 'sv', 'fi'].includes(language)) {
    return language;
  }
  return 'de';
}
export function generateYears(currentYear, minYear, maxYear) {
  const list = [];
  const today = new Date();
  const todayYear = today.getFullYear();
  for (let year = minYear; year <= maxYear; year++) {
    list.push({
      value: year,
      label: `${year}`,
      disabled: false,
      today: todayYear === year,
      selected: currentYear === year,
    });
  }
  return list;
}
export function generateMonths(language, currentYear, selectedDate, min, max) {
  const locale = validateLanguage(language);
  const months = BalDate.infoMonths({ format: 'long', locale });
  const selected = BalDate.fromISO(selectedDate);
  const today = new Date();
  const todayYear = today.getFullYear();
  const todayMonth = today.getMonth() + 1;
  let minMonth = 0;
  if (min && currentYear !== undefined) {
    const minDate = BalDate.fromISO(min);
    if (minDate.isValid) {
      if (currentYear > minDate.year) {
        minMonth = 0;
      }
      else {
        minMonth = minDate.month || minMonth;
      }
    }
  }
  let maxMonth = 12;
  if (max && currentYear !== undefined) {
    const maxDate = BalDate.fromISO(max);
    if (maxDate.isValid) {
      if (currentYear < maxDate.year) {
        maxMonth = 12;
      }
      else {
        maxMonth = maxDate.month || maxMonth;
      }
    }
  }
  return months.map((label, index) => {
    const value = index + 1;
    return {
      label,
      value,
      today: value === todayMonth && currentYear === todayYear,
      selected: value === selected.month && currentYear === selected.year,
      disabled: value < minMonth || value > maxMonth,
    };
  });
}
export function generateWeekDays(language) {
  const locale = validateLanguage(language);
  const weekdaysMin = BalDate.infoWeekdays({ format: 'short', locale });
  const weekdays = BalDate.infoWeekdays({ format: 'long', locale });
  return weekdaysMin.map((weekdayMin, index) => ({
    ariaLabel: weekdays[index],
    textContent: weekdayMin,
  }));
}
export function getFirstDayOfMonth(year, month) {
  if (month < 1 || month > 12 || year < 0) {
    return undefined;
  }
  return new Date(year, month - 1, 1);
}
