import { themeConfig } from "../config";
import { MonthViewModel } from "../MonthViewModel";
import { isGreater, isLess } from "../CalendarDateShape";
export var calculateScrollPosition = function calculateScrollPosition(months, scrollPosition, deltaY, theme) {
  var scrollDirection = deltaY > 0 ? 1 : -1;
  var nextScrollPosition = scrollPosition - deltaY;
  var nextMonths = months;
  var firstMonth = months[0];
  if (scrollDirection < 0 && nextScrollPosition >= firstMonth.getHeight(theme)) {
    do {
      nextScrollPosition -= nextMonths[0].getHeight(theme);
      nextMonths = getMonths(firstMonth.month, firstMonth.year);
    } while (nextScrollPosition >= nextMonths[0].getHeight(theme));
  }
  var lastMonth = months[months.length - 1];
  if (scrollDirection > 0 && nextScrollPosition < 0) {
    do {
      nextScrollPosition += nextMonths[1].getHeight(theme);
      nextMonths = getMonths(lastMonth.month, lastMonth.year);
    } while (nextScrollPosition < 0);
  }
  return {
    scrollPosition: nextScrollPosition,
    months: nextMonths,
    scrollDirection: scrollDirection
  };
};
export var applyDelta = function applyDelta(deltaY, theme) {
  return function (_ref, _ref2) {
    var scrollPosition = _ref.scrollPosition,
      months = _ref.months;
    var minDate = _ref2.minDate,
      maxDate = _ref2.maxDate;
    var scrollDirection = deltaY > 0 ? 1 : -1;
    var isMinDateExceeded = minDate && scrollDirection < 0 && minDate.year * 12 + minDate.month > months[0].year * 12 + months[0].month;
    var isMaxDateExceeded = maxDate && scrollDirection > 0 && maxDate.year * 12 + maxDate.month < months[1].year * 12 + months[1].month;
    if (isMinDateExceeded) {
      return {
        scrollPosition: 0,
        scrollDirection: scrollDirection
      };
    }
    if (isMaxDateExceeded) {
      return {
        scrollPosition: months[2].getHeight(theme),
        scrollDirection: scrollDirection
      };
    }
    return calculateScrollPosition(months, scrollPosition, deltaY, theme);
  };
};
export var isMonthVisible = function isMonthVisible(top, month, theme) {
  return top < themeConfig(theme).WRAPPER_HEIGHT && top > -month.getHeight(theme);
};
export var getMonthsHeight = function getMonthsHeight(months, theme) {
  return months.reduce(function (a, b) {
    return a + b.getHeight(theme);
  }, 0);
};
export var getMonths = function getMonths(month, year) {
  return [-1, 0, 1].map(function (x) {
    return MonthViewModel.create(month + x, year);
  });
};
export var getMonthInNativeFormat = function getMonthInNativeFormat(month) {
  return month - 1;
};
export var getMonthInHumanFormat = function getMonthInHumanFormat(month) {
  return month + 1;
};
export var getInitialDate = function getInitialDate(_ref3) {
  var today = _ref3.today,
    date = _ref3.date,
    minDate = _ref3.minDate,
    maxDate = _ref3.maxDate;
  if (date) {
    return date;
  }
  if (minDate && isLess(today, minDate)) {
    return minDate;
  }
  if (maxDate && isGreater(today, maxDate)) {
    return maxDate;
  }
  return today;
};
export var getTodayDate = function getTodayDate() {
  var date = new Date();
  return {
    date: date.getDate(),
    month: date.getMonth(),
    year: date.getFullYear()
  };
};