import { MAX_DATE, MAX_MONTH, MAX_YEAR, MIN_DATE, MIN_MONTH, MIN_YEAR } from "../constants";
import { InternalDateComponentType } from "../types";
var calculateMonth = function calculateMonth(month) {
  if (month <= 7) {
    return month + 1;
  }
  return month;
};
var getIsLeapYear = function getIsLeapYear(year) {
  // The year is considered leap if it's either dividable by 4, 100 or 400.
  var isDividableByFour = year % 4 === 0;
  var isDividableByOneHundred = year % 100 === 0;
  var isDividableByFourHundred = year % 400 === 0;
  return isDividableByFour || isDividableByOneHundred || isDividableByFourHundred;
};
export var InternalDateGetter = /*#__PURE__*/function () {
  function InternalDateGetter() {}
  InternalDateGetter.getMaxDaysInMonth = function getMaxDaysInMonth(month, year) {
    if (month === 2) {
      var isLeapYear = year ? getIsLeapYear(year) : false;
      return isLeapYear ? 29 : 28;
    }
    var calculatedMonth = calculateMonth(month);
    return calculatedMonth % 2 === 0 ? 31 : 30;
  };
  InternalDateGetter.getValueDateComponent = function getValueDateComponent(type, components) {
    if (type === InternalDateComponentType.Year) {
      return components.year;
    }
    if (type === InternalDateComponentType.Month) {
      return components.month;
    }
    return components.date;
  };
  InternalDateGetter.getDefaultMin = function getDefaultMin(type) {
    if (type === InternalDateComponentType.Year) {
      return MIN_YEAR;
    } else if (type === InternalDateComponentType.Month) {
      return MIN_MONTH;
    } else if (type === InternalDateComponentType.Date) {
      return MIN_DATE;
    }
    return MIN_DATE;
  };
  InternalDateGetter.getDefaultMax = function getDefaultMax(type, components, dateInputAllowInvalidValuesInDays) {
    if (type === InternalDateComponentType.Year) {
      return MAX_YEAR;
    } else if (type === InternalDateComponentType.Month) {
      return MAX_MONTH;
    } else if (type === InternalDateComponentType.Date) {
      if (dateInputAllowInvalidValuesInDays) {
        return MAX_DATE;
      }
      if (components === undefined) {
        return MAX_DATE;
      }
      var year = components.year,
        month = components.month;
      return year && month ? InternalDateGetter.getMaxDaysInMonth(month, year) : MAX_DATE;
    }
    return MAX_DATE;
  };
  InternalDateGetter.getTodayComponents = function getTodayComponents() {
    var date = new Date();
    return {
      date: date.getDate(),
      month: date.getMonth() + 1,
      year: date.getFullYear()
    };
  };
  return InternalDateGetter;
}();
InternalDateGetter.max = function (datesCustom) {
  return datesCustom.sort(function (a, b) {
    return b.toNumber() - a.toNumber();
  })[0];
};
InternalDateGetter.min = function (datesCustom) {
  return datesCustom.sort(function (a, b) {
    return a.toNumber() - b.toNumber();
  })[0];
};