'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var types = require('../store/types.js');
var calendarGridHelper = require('./calendarGridHelper.js');

var dateOperationHandler = {
  Days: {
    add: function add(date) {
      return date.setDate(date.getDate() + 1);
    },
    subtract: function subtract(date) {
      return date.setDate(date.getDate() - 1);
    }
  },
  Months: {
    add: function add(date) {
      return date.setMonth(date.getMonth() + 1);
    },
    subtract: function subtract(date) {
      return date.setMonth(date.getMonth() - 1);
    }
  },
  Quarters: {
    add: function add(date) {
      return date.setMonth(date.getMonth() + 3);
    },
    subtract: function subtract(date) {
      return date.setMonth(date.getMonth() - 3);
    }
  },
  Years: {
    add: function add(date) {
      return date.setFullYear(date.getFullYear() + 1);
    },
    subtract: function subtract(date) {
      return date.setFullYear(date.getFullYear() - 1);
    }
  }
};
var normalizeDate = function normalizeDate(date, type) {
  if (type === types.CalendarState.Months || type === types.CalendarState.Years) {
    date.setDate(1);
  }
  if (type === types.CalendarState.Years) {
    date.setMonth(0);
  }
};
var isDisabledNextDate = function isDisabledNextDate(_ref, type, max, includeEdgeDates) {
  var year = _ref.year,
    monthIndex = _ref.monthIndex,
    day = _ref.day;
  if (!max) {
    return false;
  }
  var maxDate = new Date(max);
  normalizeDate(maxDate, type);
  var currentDate = new Date(year, monthIndex, day);
  dateOperationHandler[type].add(currentDate);
  var isOut = true;
  while (isOut && (includeEdgeDates ? currentDate < maxDate : currentDate <= maxDate)) {
    isOut = includeEdgeDates ? maxDate < currentDate : maxDate <= currentDate;
    dateOperationHandler[type].add(currentDate);
  }
  return isOut;
};
var isDisabledPreviousDate = function isDisabledPreviousDate(_ref2, type, min, includeEdgeDates) {
  var year = _ref2.year,
    monthIndex = _ref2.monthIndex,
    day = _ref2.day;
  if (!min) {
    return false;
  }
  var minDate = new Date(min);
  normalizeDate(minDate, type);
  var currentDate = new Date(year, monthIndex, day);
  dateOperationHandler[type].subtract(currentDate);
  var isOut = true;
  while (isOut && (includeEdgeDates ? currentDate > minDate : currentDate >= minDate)) {
    isOut = includeEdgeDates ? minDate > currentDate : minDate >= currentDate;
    dateOperationHandler[type].subtract(currentDate);
  }
  return isOut;
};
var getDisabledDates = function getDisabledDates(list, type, min, max, includeEdgeDates) {
  var disabledDate = [];
  if (isDisabledPreviousDate(list[0], type, min, includeEdgeDates)) {
    disabledDate.push('previous');
  }
  if (isDisabledNextDate(list[list.length - 1], type, max, includeEdgeDates)) {
    disabledDate.push('next');
  }
  return disabledDate.join(',');
};
var isDisabledArrowLeft = function isDisabledArrowLeft(date, min, includeEdgeDates) {
  var currentDate = new Date(date);
  currentDate.setDate(currentDate.getDate() - 1);
  var disableCondition = min && (includeEdgeDates ? min > currentDate : min >= currentDate);
  return disableCondition;
};
var isDisabledArrowRight = function isDisabledArrowRight(date, max, includeEdgeDates) {
  var currentDate = new Date(date);
  currentDate.setDate(currentDate.getDate() + 1);
  var disableCondition = max && (includeEdgeDates ? max < currentDate : max <= currentDate);
  return disableCondition;
};
var isDisabledArrowUp = function isDisabledArrowUp(date, min, includeEdgeDates) {
  var currentDate = new Date(date);
  currentDate.setDate(date.getDate() - 7);
  var disableCondition = min && (includeEdgeDates ? min > currentDate : min >= currentDate);
  return disableCondition;
};
var isDisabledArrowDown = function isDisabledArrowDown(date, max, includeEdgeDates) {
  var currentDate = new Date(date);
  currentDate.setDate(date.getDate() + 7);
  var disableCondition = max && (includeEdgeDates ? max < currentDate : max <= currentDate);
  return disableCondition;
};
var getDisabledArrowKey = function getDisabledArrowKey(currentDate, min, max, includeEdgeDates) {
  var disabledArrowKey = [];
  if (isDisabledArrowLeft(currentDate, min, includeEdgeDates)) {
    disabledArrowKey.push('left');
  }
  if (isDisabledArrowRight(currentDate, max, includeEdgeDates)) {
    disabledArrowKey.push('right');
  }
  if (isDisabledArrowDown(currentDate, max, includeEdgeDates)) {
    disabledArrowKey.push('down');
  }
  if (isDisabledArrowUp(currentDate, min, includeEdgeDates)) {
    disabledArrowKey.push('up');
  }
  return disabledArrowKey.join(',');
};

/**
 * Метод модифицирующий дату (добавляющий свойства events и disabled).
 */
var getDatesWithModifications = function getDatesWithModifications(_ref3) {
  var dates = _ref3.dates,
    type = _ref3.type,
    _ref3$eventList = _ref3.eventList,
    eventList = _ref3$eventList === void 0 ? [] : _ref3$eventList,
    _ref3$disabledList = _ref3.disabledList,
    disabledList = _ref3$disabledList === void 0 ? [] : _ref3$disabledList,
    includeEdgeDates = _ref3.includeEdgeDates,
    min = _ref3.min,
    max = _ref3.max;
  var eventsMap = calendarGridHelper.getPropsMap(eventList);
  var disabledDatesMap = calendarGridHelper.getPropsMap(disabledList);
  var filteredDates = type === types.CalendarState.Days ? dates.filter(function (_ref4) {
    var isDayInCurrentMonth = _ref4.isDayInCurrentMonth;
    return isDayInCurrentMonth;
  }) : dates;
  var datesList = filteredDates.map(function (_ref5) {
    var date = _ref5.date;
    return date;
  });
  var disabledDates = getDisabledDates(datesList, type, min, max, includeEdgeDates);
  return dates.map(function (dateItem) {
    var date = dateItem.date;
    var year = date.year,
      monthIndex = date.monthIndex,
      day = date.day;
    var keyDate = "".concat(year, "-").concat(monthIndex, "-").concat(day);
    var currentDate = new Date(year, monthIndex, day);
    var minDate = min && new Date(min);
    minDate && normalizeDate(minDate, type);
    var maxDate = max && new Date(max);
    maxDate && normalizeDate(maxDate, type);
    var minValue;
    var maxValue;
    if (type === types.CalendarState.Days) {
      minValue = min && (includeEdgeDates ? min > currentDate : min >= currentDate);
      maxValue = max && (includeEdgeDates ? max < currentDate : max <= currentDate);
    } else {
      minValue = minDate && minDate > currentDate;
      maxValue = maxDate && maxDate < currentDate;
    }
    var isOutOfMinMaxRange = minValue || maxValue;
    dateItem.events = eventsMap.get(keyDate);
    dateItem.disabled = disabledDatesMap.has(keyDate) || isOutOfMinMaxRange;
    dateItem.isOutOfMinMaxRange = isOutOfMinMaxRange;
    dateItem.disabledArrowKey = getDisabledArrowKey(currentDate, min, max, includeEdgeDates);
    dateItem.disabledDates = disabledDates;
    return dateItem;
  });
};

exports.getDatesWithModifications = getDatesWithModifications;
