"use strict";

exports.__esModule = true;
exports.NOW = exports.MIN_MONTH = exports.MIN_DAY = exports.MAX_MONTH = exports.MAX_DAY = exports.DEFAULT_MIN_DATE = exports.DEFAULT_MAX_DATE = void 0;
exports.cloneAsDate = cloneAsDate;
exports.cloneDate = cloneDate;
exports.getAMPM = exports.genTotalPageDates = exports.genContentHeight = exports.formatNumber = void 0;
exports.getCellStatus = getCellStatus;
exports.getCurrentMonthDates = getCurrentMonthDates;
exports.getCurrentMonthList = getCurrentMonthList;
exports.getDateByValues = exports.getDate = exports.getDataSource = void 0;
exports.getDayList = getDayList;
exports.getDefaultFormat = exports.getDaysInMonth = exports.getDays = void 0;
exports.getFirstDayOfMonth = getFirstDayOfMonth;
exports.getMinutes = exports.getMaxMinDate = exports.getHours = void 0;
exports.getMonthArray = getMonthArray;
exports.getMonthList = getMonthList;
exports.getMonths = void 0;
exports.getNextMonthDates = getNextMonthDates;
exports.getPreMonthDates = getPreMonthDates;
exports.getValues = exports.getTargetDate = exports.getSelectedIndex = exports.getSeconds = void 0;
exports.getWeekArray = getWeekArray;
exports.getWeekDateArray = getWeekDateArray;
exports.getWeekDayArray = getWeekDayArray;
exports.getYears = void 0;
exports.isAfterDate = isAfterDate;
exports.isBeforeDate = isBeforeDate;
exports.isBetweenDates = isBetweenDates;
exports.isDateDisabled = isDateDisabled;
exports.isDateInRange = isDateInRange;
exports.isDateSelected = isDateSelected;
exports.isEqualDate = isEqualDate;
exports.isHaveDate = isHaveDate;
exports.isTheSameDay = isTheSameDay;
exports.isTheSameMonth = isTheSameMonth;
exports.isTheSameYear = isTheSameYear;
exports.isToday = isToday;
exports.normalizeValue = normalizeValue;
exports.setWeek = setWeek;
exports.updateMonth = updateMonth;
exports.updateWeek = updateWeek;
exports.updateYear = updateYear;

var _dayjs = _interopRequireDefault(require("dayjs"));

var _types = require("./types");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var MAX_MONTH = 11;
exports.MAX_MONTH = MAX_MONTH;
var MIN_MONTH = 0;
exports.MIN_MONTH = MIN_MONTH;
var MAX_DAY = 6;
exports.MAX_DAY = MAX_DAY;
var MIN_DAY = 0;
exports.MIN_DAY = MIN_DAY;
var NOW = new Date();
exports.NOW = NOW;
var DEFAULT_MAX_DATE = new Date(NOW.getFullYear() + 20, 11, 31, 23, 59, 59, 999);
exports.DEFAULT_MAX_DATE = DEFAULT_MAX_DATE;
var DEFAULT_MIN_DATE = new Date(2000, 0, 1, 0, 0, 0, 0);
exports.DEFAULT_MIN_DATE = DEFAULT_MIN_DATE;

var getMaxMinDate = function (validRange) {
  var minDate = DEFAULT_MIN_DATE;
  var maxDate = DEFAULT_MAX_DATE;

  if (validRange && validRange.length > 0) {
    if (Object.prototype.toString.call(validRange[0]) === '[object Date]') {
      minDate = validRange[0];
    }

    if (Object.prototype.toString.call(validRange[1]) === '[object Date]') {
      maxDate = validRange[1];
    }
  }

  return [minDate, maxDate];
};

exports.getMaxMinDate = getMaxMinDate;

var formatNumber = function (value) {
  return value > 9 ? value : "0" + value;
};

exports.formatNumber = formatNumber;

var getDate = function (value, maxDate, minDate) {
  var v = value || new Date();

  if (v.getTime() > maxDate.getTime()) {
    v = cloneDate(maxDate);
  }

  if (v.getTime() < minDate.getTime()) {
    v = cloneDate(minDate);
  }

  return v;
};

exports.getDate = getDate;

function cloneDate(d) {
  return new Date(d.getTime());
}

var getDaysInMonth = function (date) {
  var resultDate = new Date(date.getFullYear(), date.getMonth(), 1);
  resultDate.setDate(1);
  resultDate.setMonth(resultDate.getMonth() + 1);
  resultDate.setDate(resultDate.getDate() - 1);
  return resultDate.getDate();
};

exports.getDaysInMonth = getDaysInMonth;

function updateYear(d, isAdd, maxDate, minDate) {
  if (isAdd === void 0) {
    isAdd = true;
  }

  var year = d.getFullYear();

  if (isAdd === true) {
    year = year + 1;
  } else {
    year = year - 1;
  }

  var newDate = new Date(year, d.getMonth(), 1);
  return getDate(newDate, maxDate, minDate);
}

function updateWeek(d, isAdd, maxDate, minDate) {
  if (isAdd === void 0) {
    isAdd = true;
  }

  var newD = (0, _dayjs.default)(d)[isAdd ? 'add' : 'subtract'](1, 'week');
  return getDate(new Date(newD.valueOf()), maxDate, minDate);
}

function updateMonth(d, isAdd, maxDate, minDate) {
  if (isAdd === void 0) {
    isAdd = true;
  }

  var month = d.getMonth();
  var year = d.getFullYear();

  if (isAdd === true) {
    month = month + 1;

    if (month > MAX_MONTH) {
      month = MIN_MONTH;
      year = year + 1;
    }
  } else {
    month = month - 1;

    if (month < MIN_MONTH) {
      month = MAX_MONTH;
      year = year - 1;
    }
  }

  var newDate = new Date(year, month, 1);
  var resultDate = getDate(new Date(newDate), maxDate, minDate);
  return resultDate;
}

function getMonthList(visibleDate, locale) {
  var months = [];

  for (var month = MIN_MONTH; month <= MAX_MONTH; month += 1) {
    months.push({
      label: "" + formatNumber(month + 1) + (locale.month || ''),
      value: new Date(visibleDate.getFullYear(), month)
    });
  }

  return months;
}

function getDayList(local) {
  var days = [];

  for (var day = MIN_DAY; day <= MAX_DAY; day += 1) {
    days.push({
      label: local.weekdays && local.weekdays[day],
      value: day
    });
  }

  return days;
}

function getPreMonthDates(visibleDate) {
  var days = [];
  var firstDay = new Date(visibleDate.getFullYear(), visibleDate.getMonth(), 1);
  var visibleDay = firstDay.getDay();

  if (visibleDay === 0) {
    return days;
  }

  var lastMonthDays = 0;
  var year = visibleDate.getFullYear();
  var month = visibleDate.getMonth();

  if (visibleDate.getMonth() === 0) {
    year = year - 1;
    month = MAX_MONTH;
  } else {
    month = month - 1;
  }

  lastMonthDays = getDaysInMonth(new Date(year, month, 1));
  var beginDate = lastMonthDays - (visibleDay - 1);

  for (var i = 0; i < visibleDay; i++) {
    days.push({
      label: "" + (beginDate + i),
      value: new Date(year, month, beginDate + i)
    });
  }

  return days;
}

function getCurrentMonthDates(visibleDate) {
  var days = [];
  var monthDays = getDaysInMonth(visibleDate);

  for (var i = 1; i <= monthDays; i++) {
    days.push({
      label: i.toString(),
      value: new Date(visibleDate.getFullYear(), visibleDate.getMonth(), i)
    });
  }

  return days;
}

function getNextMonthDates(visibleDate) {
  var days = [];
  var monthDays = getDaysInMonth(visibleDate);
  var lastDate = new Date(visibleDate.getFullYear(), visibleDate.getMonth(), monthDays);
  var lastDay = lastDate.getDay();
  var year = visibleDate.getFullYear();
  var month = visibleDate.getMonth();

  if (visibleDate.getMonth() === MAX_MONTH) {
    year = year + 1;
    month = MIN_MONTH;
  } else {
    month = month + 1;
  }

  for (var i = lastDay + 1; i <= MAX_DAY; i++) {
    days.push({
      label: "" + (i - lastDay),
      value: new Date(year, month, i - lastDay)
    });
  }

  return days;
}

function getCurrentMonthList(visibleDate, local) {
  var preDates = getPreMonthDates(visibleDate, local);
  var currentDates = getCurrentMonthDates(visibleDate, local);
  var nextDates = getNextMonthDates(visibleDate, local);
  return preDates.concat(currentDates).concat(nextDates);
}

function isTheSameDay(date1, date2) {
  if (!date1 || !date2) {
    return false;
  }

  return (0, _dayjs.default)(date1).isSame(date2, 'day');
}

function isTheSameMonth(date1, date2) {
  if (!date1 || !date2) {
    return false;
  }

  return (0, _dayjs.default)(date1).isSame(date2, 'month');
}

function isTheSameYear(date1, date2) {
  if (!date1 || !date2) {
    return false;
  }

  return date1.getFullYear() === date2.getFullYear();
}

function isDateDisabled(cellDate, disableFunc, maxDate, minDate, view) {
  if (cellDate.getTime() > maxDate.getTime()) {
    return true;
  }

  if (cellDate.getTime() < minDate.getTime()) {
    return true;
  }

  if (disableFunc) {
    return disableFunc(cellDate, view);
  }

  return false;
}

function isDateInRange(cellDate, selectedData) {
  if (!cellDate || !selectedData) {
    return false;
  }

  if (!Array.isArray(selectedData)) {
    return false;
  }

  if (selectedData.length < 2) {
    return false;
  }

  if (cellDate.getTime() > selectedData[selectedData.length - 1].getTime()) {
    return false;
  }

  if (cellDate.getTime() < selectedData[0].getTime()) {
    return false;
  }

  return true;
}

function isDateSelected(cellDate, selectedData, mode, type) {
  var isSelected = null;

  if (type === 'range') {
    if (!Array.isArray(selectedData)) {
      throw new Error('Type is range, but selected is not an array');
    }

    if (isTheSameDay(cellDate, selectedData[0])) {
      return _types.CellStatusType.begin;
    }

    if (isTheSameDay(cellDate, selectedData[selectedData.length - 1])) {
      return _types.CellStatusType.end;
    }

    return isSelected;
  }

  if (type === 'multi') {
    if (!Array.isArray(selectedData)) {
      throw new Error('Type is multi, but selected is not an array');
    }

    for (var i = 0; i < selectedData.length; i++) {
      if (isTheSameDay(cellDate, selectedData[i])) {
        return _types.CellStatusType.selected;
      }
    }

    return isSelected;
  }

  if (Array.isArray(selectedData)) {
    throw new Error("Type is " + mode + ", but selected is an array");
  }

  if (mode === 'month' || mode === 'week') {
    if (isTheSameDay(cellDate, selectedData)) {
      isSelected = _types.CellStatusType.selected;
    }
  }

  if (mode === 'year') {
    if (isTheSameMonth(cellDate, selectedData)) {
      isSelected = _types.CellStatusType.selected;
    }
  }

  return isSelected;
}

function getCellStatus(cellDate, selectedData, mode, type, disableFunc, maxDate, minDate, view) {
  var cellType = _types.CellStatusType.normal;
  var today = isToday(cellDate);

  if (today) {
    cellType = _types.CellStatusType.today;
  }

  var isInRange = type == 'range' && isDateInRange(cellDate, selectedData);

  if (isInRange) {
    cellType = _types.CellStatusType.range;
  }

  var isSelected = isDateSelected(cellDate, selectedData, mode, type);

  if (isSelected) {
    cellType = isSelected;
  }

  var isDisable = isDateDisabled(cellDate, disableFunc, maxDate, minDate, view);

  if (isDisable) {
    cellType = _types.CellStatusType.disabled;
  }

  return cellType;
}

function isToday(cellDate) {
  if (!cellDate) {
    return false;
  }

  var cellYear = cellDate.getFullYear();
  var cellMonth = cellDate.getMonth();
  var cellDay = cellDate.getDate();
  var today = new Date();
  var todayYear = today.getFullYear();
  var todayMonth = today.getMonth();
  var todayDay = today.getDate();

  if (cellYear == todayYear && cellMonth == todayMonth && cellDay == todayDay) {
    return true;
  }

  return false;
}

function isHaveDate(cellDate, selected) {
  if (!cellDate) {
    return -1;
  }

  if (!selected || selected.length < 1) {
    return -1;
  }

  for (var i = 0; i < selected.length; i++) {
    if (isTheSameDay(cellDate, selected[i])) {
      return i;
    }
  }

  return -1;
}

function getFirstDayOfMonth(d) {
  return new Date(d.getFullYear(), d.getMonth(), 1);
}

function getWeekArray(d, firstDayOfWeek) {
  var dayArray = [];
  var daysInMonth = getDaysInMonth(d);
  var weekArray = [];
  var week = [];

  for (var i = 1; i <= daysInMonth; i++) {
    dayArray.push(new Date(d.getFullYear(), d.getMonth(), i, d.getHours(), d.getMinutes()));
  }

  var addWeek = function (w) {
    var emptyDays = 7 - w.length;

    for (var _i = 0; _i < emptyDays; ++_i) {
      w[weekArray.length ? 'push' : 'unshift'](null);
    }

    weekArray.push(w);
  };

  dayArray.forEach(function (day) {
    if (week.length > 0 && day.getDay() === firstDayOfWeek) {
      addWeek(week);
      week = [];
    }

    week.push(day);

    if (dayArray.indexOf(day) === dayArray.length - 1) {
      addWeek(week);
    }
  });
  return weekArray;
}

function getWeekDayArray(weekdays, firstDayOfWeek) {
  var beforeArray = [];
  var afterArray = [];

  for (var i = 0; i < weekdays.length; i++) {
    if (i < firstDayOfWeek) {
      afterArray.push(weekdays[i]);
    } else {
      beforeArray.push(weekdays[i]);
    }
  }

  return [].concat(beforeArray, afterArray);
}

function getWeekDateArray(d, firstDayOfWeek) {
  var arr = [];

  for (var i = 0; i < 7; i++) {
    var weekDate = cloneDate(d);
    setWeek(weekDate, i, firstDayOfWeek);
    arr.push(weekDate);
  }

  return arr;
}

function getMonthArray(d) {
  var months = [];
  var month = [];

  for (var i = 0; i < 12; i++) {
    month.push(new Date(d.getFullYear(), i, 1, d.getHours(), d.getMinutes()));

    if (month.length === 3) {
      months.push(month);
      month = [];
    }
  }

  return months;
}

function setWeek(d, day, firstDayOfWeek) {
  var week = d.getDay();
  d.setDate(d.getDate() + (day - week + firstDayOfWeek));
  return d;
}

function cloneAsDate(d) {
  var clonedDate = cloneDate(d);
  clonedDate.setHours(0, 0, 0, 0);
  return clonedDate;
}

function isBeforeDate(d1, d2) {
  var date1 = cloneAsDate(d1);
  var date2 = cloneAsDate(d2);
  return date1.getTime() < date2.getTime();
}

function isAfterDate(d1, d2) {
  var date1 = cloneAsDate(d1);
  var date2 = cloneAsDate(d2);
  return date1.getTime() > date2.getTime();
}

function isBetweenDates(dateToCheck, startDate, endDate) {
  return !isBeforeDate(dateToCheck, startDate) && !isAfterDate(dateToCheck, endDate);
}

function isEqualDate(d1, d2) {
  return d1 && d2 && d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate();
}

function normalizeValue(value, isSelectWeek, firstDayOfWeek) {
  if (!value) {
    return [];
  }

  if (!Array.isArray(value)) {
    if (isSelectWeek) {
      return [setWeek(cloneDate(value), 0, firstDayOfWeek), setWeek(cloneDate(value), 6, firstDayOfWeek)];
    }

    return [value];
  }

  return value;
}

var getDefaultFormat = function (type, second, use12Hours) {
  var timeFormat = 'HH:mm';

  if (second) {
    timeFormat += ':ss';
  }

  if (use12Hours) {
    timeFormat = timeFormat.replace('HH', 'hh') + " A";
  }

  return {
    date: 'YYYY-MM-DD',
    year: 'YYYY',
    month: 'YYYY-MM',
    datetime: ['YYYY-MM-DD', timeFormat].join(' '),
    time: timeFormat
  }[type];
};

exports.getDefaultFormat = getDefaultFormat;

var getDateByValues = function (date, values, type, second, use12Hours) {
  var units = {
    date: 'year_month_date',
    year: 'year',
    month: 'year_month',
    datetime: "year_month_date_hour_minute" + (second ? '_second' : ''),
    time: "hour_minute" + (second ? '_second' : '')
  }[type].split('_');
  var d = cloneDate(date);

  if (units.includes('month')) {
    d.setDate(1);
  }

  units.forEach(function (unit, index) {
    var value = values[index];

    switch (unit) {
      case 'year':
        d.setFullYear(value);
        break;

      case 'month':
        d.setMonth(value);
        break;

      case 'date':
        var daysInMonth = getDaysInMonth(d);
        d.setDate(value > daysInMonth ? daysInMonth : value);
        break;

      case 'hour':
        if (use12Hours && values[values.length - 1] === 'am' && value > 11) {
          d.setHours(value - 12);
          break;
        } else if (use12Hours && values[values.length - 1] === 'pm' && value < 12) {
          d.setHours(value + 12);
          break;
        }

        d.setHours(value);
        break;

      case 'minute':
        d.setMinutes(value);
        break;

      case 'second':
        d.setSeconds(value);
        break;

      default:
        break;
    }
  });
  return d;
};

exports.getDateByValues = getDateByValues;

var getValues = function (value, type, second, use12Hours, maxDate, minDate) {
  value = getDate(value, maxDate, minDate);
  var date = (0, _dayjs.default)(value);
  var values = [];
  var format = getDefaultFormat(type, second, use12Hours);
  var hour = date.hour();

  if (format.indexOf('YYYY') !== -1) {
    values.push(date.year());
  }

  if (format.indexOf('MM') !== -1) {
    values.push(date.month());
  }

  if (format.indexOf('DD') !== -1) {
    values.push(date.date());
  }

  if (format.indexOf('HH') !== -1 || format.indexOf('hh') !== -1) {
    values.push(hour);
  }

  if (format.indexOf('mm') !== -1) {
    values.push(date.minute());
  }

  if (format.indexOf('ss') !== -1) {
    values.push(date.second());
  }

  if (use12Hours) {
    values.push(hour > 11 ? 'pm' : 'am');
  }

  return values;
};

exports.getValues = getValues;

var getYears = function (maxDate, minDate, locale) {
  if (locale === void 0) {
    locale = {};
  }

  var years = [];
  var maxYears = maxDate.getFullYear();
  var minYears = minDate.getFullYear();

  for (var year = minYears; year <= maxYears; year += 1) {
    years.push({
      label: "" + year + (locale.year || ''),
      value: year
    });
  }

  return years;
};

exports.getYears = getYears;

var getMonths = function (value, maxDate, minDate, locale) {
  if (locale === void 0) {
    locale = {};
  }

  var months = [];
  var minMonth = 0;
  var maxMonth = 11;

  if (value.getFullYear() === minDate.getFullYear()) {
    minMonth = minDate.getMonth();
  }

  if (value.getFullYear() === maxDate.getFullYear()) {
    maxMonth = maxDate.getMonth();
  }

  for (var month = minMonth; month <= maxMonth; month += 1) {
    months.push({
      label: "" + formatNumber(month + 1) + (locale.month || ''),
      value: month
    });
  }

  return months;
};

exports.getMonths = getMonths;

var getDays = function (value, maxDate, minDate, locale) {
  if (locale === void 0) {
    locale = {};
  }

  var days = [];
  var maxDay = getDaysInMonth(value);
  var minDay = 1;

  if (value.getFullYear() === maxDate.getFullYear() && value.getMonth() === maxDate.getMonth()) {
    maxDay = maxDate.getDate();
  }

  if (value.getFullYear() === minDate.getFullYear() && value.getMonth() === minDate.getMonth()) {
    minDay = minDate.getDate();
  }

  for (var day = minDay; day <= maxDay; day += 1) {
    days.push({
      label: "" + formatNumber(day) + (locale.day || ''),
      value: day
    });
  }

  return days;
};

exports.getDays = getDays;

var getHours = function (value, maxDate, minDate, locale, use12Hours) {
  if (locale === void 0) {
    locale = {};
  }

  var hours = [];
  var maxHour = 23;
  var minHour = 0;

  if (value.getFullYear() === maxDate.getFullYear() && value.getMonth() === maxDate.getMonth() && value.getDate() === maxDate.getDate()) {
    maxHour = maxDate.getHours();
  }

  if (value.getFullYear() === minDate.getFullYear() && value.getMonth() === minDate.getMonth() && value.getDate() === minDate.getDate()) {
    minHour = minDate.getHours();
  }

  for (var hour = minHour; hour <= maxHour; hour += 1) {
    hours.push({
      label: "" + formatNumber(hour) + (locale.hour || ''),
      value: hour
    });
  }

  if (use12Hours) {
    var isPM = value.getHours() > 11;
    hours = hours.filter(function (item) {
      return isPM && item.value > 11 || !isPM && item.value < 12;
    }).map(function (item) {
      if (isPM) {
        var val = item.value - 12;
        return {
          label: "" + formatNumber(val) + (locale.hour || ''),
          value: item.value
        };
      }

      return item;
    });

    if (hours.length > 0 && (hours[0].value === 0 || hours[0].value === 12)) {
      var item = hours[0];
      hours.splice(0, 1);
      hours.push(Object.assign(Object.assign({}, item), {
        label: "12" + (locale.hour || '')
      }));
    }
  }

  return hours;
};

exports.getHours = getHours;

var getMinutes = function (value, maxDate, minDate, locale) {
  if (locale === void 0) {
    locale = {};
  }

  var minutes = [];
  var maxMinute = 59;
  var minMinute = 0;

  if (value.getFullYear() === maxDate.getFullYear() && value.getMonth() === maxDate.getMonth() && value.getDate() === maxDate.getDate() && value.getHours() === maxDate.getHours()) {
    maxMinute = maxDate.getMinutes();
  }

  if (value.getFullYear() === minDate.getFullYear() && value.getMonth() === minDate.getMonth() && value.getDate() === minDate.getDate() && value.getHours() === minDate.getHours()) {
    minMinute = minDate.getMinutes();
  }

  for (var minute = minMinute; minute <= maxMinute; minute += 1) {
    minutes.push({
      label: "" + formatNumber(minute) + (locale.minute || ''),
      value: minute
    });
  }

  return minutes;
};

exports.getMinutes = getMinutes;

var getSeconds = function (value, maxDate, minDate, locale) {
  if (locale === void 0) {
    locale = {};
  }

  var seconds = [];
  var maxSecond = 59;
  var minSecond = 0;

  if (value.getFullYear() === maxDate.getFullYear() && value.getMonth() === maxDate.getMonth() && value.getDate() === maxDate.getDate() && value.getHours() === maxDate.getHours() && value.getMinutes() === maxDate.getMinutes()) {
    maxSecond = maxDate.getSeconds();
  }

  if (value.getFullYear() === minDate.getFullYear() && value.getMonth() === minDate.getMonth() && value.getDate() === minDate.getDate() && value.getHours() === minDate.getHours() && value.getMinutes() === minDate.getMinutes()) {
    minSecond = minDate.getSeconds();
  }

  for (var second = minSecond; second <= maxSecond; second += 1) {
    seconds.push({
      label: "" + formatNumber(second) + (locale.second || ''),
      value: second
    });
  }

  return seconds;
};

exports.getSeconds = getSeconds;

var getAMPM = function (locale) {
  return ['am', 'pm'].map(function (key) {
    return {
      label: locale[key],
      value: key
    };
  });
};

exports.getAMPM = getAMPM;

var getDataSource = function (value, type, second, use12Hours, maxDate, minDate, locale) {
  value = getDate(value, maxDate, minDate);
  var format = getDefaultFormat(type, second, use12Hours);
  var data = [];

  if (format.indexOf('YYYY') !== -1) {
    data.push(getYears(maxDate, minDate, locale));
  }

  if (format.indexOf('MM') !== -1) {
    data.push(getMonths(value, maxDate, minDate, locale));
  }

  if (format.indexOf('DD') !== -1) {
    data.push(getDays(value, maxDate, minDate, locale));
  }

  if (format.indexOf('HH') !== -1 || format.indexOf('hh') !== -1) {
    data.push(getHours(value, maxDate, minDate, locale, use12Hours));
  }

  if (format.indexOf('mm') !== -1) {
    data.push(getMinutes(value, maxDate, minDate, locale));
  }

  if (format.indexOf('ss') !== -1) {
    data.push(getSeconds(value, maxDate, minDate, locale));
  }

  if (use12Hours) {
    data.push(getAMPM(locale));
  }

  return data;
};

exports.getDataSource = getDataSource;

var getTargetDate = function (modeType, visibleDate, type, maxDate, minDate) {
  var newTime = visibleDate;

  if (type == 'pre') {
    if (modeType == 'week') {
      newTime = updateWeek(visibleDate, false, maxDate, minDate);
    }

    if (modeType == 'month') {
      newTime = updateMonth(visibleDate, false, maxDate, minDate);
    }

    if (modeType == 'year') {
      newTime = updateYear(visibleDate, false, maxDate, minDate);
    }
  }

  if (type == 'next') {
    if (modeType == 'week') {
      newTime = updateWeek(visibleDate, true, maxDate, minDate);
    }

    if (modeType == 'month') {
      newTime = updateMonth(visibleDate, true, maxDate, minDate);
    }

    if (modeType == 'year') {
      newTime = updateYear(visibleDate, true, maxDate, minDate);
    }
  }

  return newTime;
};

exports.getTargetDate = getTargetDate;

var genContentHeight = function (modeType) {
  if (modeType == 'week') {
    return '220';
  }

  if (modeType == 'month') {
    return '580';
  }

  if (modeType == 'year') {
    return '700';
  }

  return '700';
};

exports.genContentHeight = genContentHeight;

var genTotalPageDates = function (minDate, maxDate, mode, visibleDate) {
  var dates = [];
  var lastPre = visibleDate;

  while (true) {
    lastPre = dates.length > 0 ? dates[dates.length - 1] : lastPre;
    var pre = getTargetDate(mode, lastPre, 'pre', maxDate, minDate);

    if (isTheSameDay(pre, lastPre)) {
      break;
    }

    if (mode == 'week') {
      var tempNext = getTargetDate(mode, pre, 'next', maxDate, minDate);

      if (!isTheSameDay(tempNext, lastPre)) {
        break;
      }
    }

    if (mode == 'month') {
      if (isTheSameMonth(pre, lastPre)) {
        break;
      }
    }

    if (mode == 'year') {
      if (isTheSameYear(pre, lastPre)) {
        break;
      }
    }

    dates.push(pre);
  }

  dates = dates.reverse();
  dates.push(visibleDate);
  var lastNext = visibleDate;

  while (true) {
    lastNext = dates.length > 0 ? dates[dates.length - 1] : lastNext;
    var next = getTargetDate(mode, lastNext, 'next', maxDate, minDate);

    if (isTheSameDay(next, lastNext)) {
      break;
    }

    if (mode == 'week') {
      var tempPre = getTargetDate(mode, next, 'pre', maxDate, minDate);

      if (!isTheSameDay(tempPre, lastNext)) {
        break;
      }
    }

    if (mode == 'month') {
      if (isTheSameMonth(next, lastNext)) {
        break;
      }
    }

    if (mode == 'year') {
      if (isTheSameYear(next, lastNext)) {
        break;
      }
    }

    dates.push(next);
  }

  return dates;
};

exports.genTotalPageDates = genTotalPageDates;

var getSelectedIndex = function (dates, selectedDate) {
  if (!dates || dates.length < 1) {
    return 1;
  }

  for (var i = 0; i < dates.length; i++) {
    if (!dates[i]) {
      continue;
    }

    if (isTheSameDay(dates[i], selectedDate)) {
      return i;
    }
  }

  return 1;
};

exports.getSelectedIndex = getSelectedIndex;