"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.convertDateToStringArray = convertDateToStringArray;
exports.convertStringArrayToDate = convertStringArrayToDate;
exports.generateDatePickerColumns = generateDatePickerColumns;
var _dayjs = _interopRequireDefault(require("dayjs"));
var _isLeapYear = _interopRequireDefault(require("dayjs/plugin/isLeapYear"));
var _isoWeek = _interopRequireDefault(require("dayjs/plugin/isoWeek"));
var _isoWeeksInYear = _interopRequireDefault(require("dayjs/plugin/isoWeeksInYear"));
var _util = require("./util");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
_dayjs.default.extend(_isoWeek.default);
_dayjs.default.extend(_isoWeeksInYear.default);
_dayjs.default.extend(_isLeapYear.default);
const precisionRankRecord = {
  year: 0,
  month: 1,
  day: 2,
  hour: 3,
  minute: 4,
  second: 5
};
function generateDatePickerColumns(selected, min, max, precision, renderLabel, filter, tillNow) {
  const ret = [];
  const minYear = min.getFullYear();
  const minMonth = min.getMonth() + 1;
  const minDay = min.getDate();
  const minHour = min.getHours();
  const minMinute = min.getMinutes();
  const minSecond = min.getSeconds();
  const maxYear = max.getFullYear();
  const maxMonth = max.getMonth() + 1;
  const maxDay = max.getDate();
  const maxHour = max.getHours();
  const maxMinute = max.getMinutes();
  const maxSecond = max.getSeconds();
  const rank = precisionRankRecord[precision];
  const selectedYear = parseInt(selected[0]);
  const firstDayInSelectedMonth = (0, _dayjs.default)(convertStringArrayToDate([selected[0], selected[1], '1']));
  const selectedMonth = parseInt(selected[1]);
  const selectedDay = parseInt(selected[2]);
  const selectedHour = parseInt(selected[3]);
  const selectedMinute = parseInt(selected[4]);
  const selectedSecond = parseInt(selected[5]);
  const isInMinYear = selectedYear === minYear;
  const isInMaxYear = selectedYear === maxYear;
  const isInMinMonth = isInMinYear && selectedMonth === minMonth;
  const isInMaxMonth = isInMaxYear && selectedMonth === maxMonth;
  const isInMinDay = isInMinMonth && selectedDay === minDay;
  const isInMaxDay = isInMaxMonth && selectedDay === maxDay;
  const isInMinHour = isInMinDay && selectedHour === minHour;
  const isInMaxHour = isInMaxDay && selectedHour === maxHour;
  const isInMinMinute = isInMinHour && selectedMinute === minMinute;
  const isInMaxMinute = isInMaxHour && selectedMinute === maxMinute;
  const generateColumn = (from, to, precision) => {
    let column = [];
    for (let i = from; i <= to; i++) {
      column.push(i);
    }
    const prefix = selected.slice(0, precisionRankRecord[precision]);
    const currentFilter = filter === null || filter === void 0 ? void 0 : filter[precision];
    if (currentFilter && typeof currentFilter === 'function') {
      column = column.filter(i => currentFilter(i, {
        get date() {
          const stringArray = [...prefix, i.toString()];
          return convertStringArrayToDate(stringArray);
        }
      }));
    }
    return column;
  };
  if (rank >= precisionRankRecord.year) {
    const lower = minYear;
    const upper = maxYear;
    const years = generateColumn(lower, upper, 'year');
    ret.push(years.map(v => ({
      label: renderLabel('year', v, {
        selected: selectedYear === v
      }),
      value: v.toString()
    })));
  }
  if (rank >= precisionRankRecord.month) {
    const lower = isInMinYear ? minMonth : 1;
    const upper = isInMaxYear ? maxMonth : 12;
    const months = generateColumn(lower, upper, 'month');
    ret.push(months.map(v => ({
      label: renderLabel('month', v, {
        selected: selectedMonth === v
      }),
      value: v.toString()
    })));
  }
  if (rank >= precisionRankRecord.day) {
    const lower = isInMinMonth ? minDay : 1;
    const upper = isInMaxMonth ? maxDay : firstDayInSelectedMonth.daysInMonth();
    const days = generateColumn(lower, upper, 'day');
    ret.push(days.map(v => ({
      label: renderLabel('day', v, {
        selected: selectedDay === v
      }),
      value: v.toString()
    })));
  }
  if (rank >= precisionRankRecord.hour) {
    const lower = isInMinDay ? minHour : 0;
    const upper = isInMaxDay ? maxHour : 23;
    const hours = generateColumn(lower, upper, 'hour');
    ret.push(hours.map(v => ({
      label: renderLabel('hour', v, {
        selected: selectedHour === v
      }),
      value: v.toString()
    })));
  }
  if (rank >= precisionRankRecord.minute) {
    const lower = isInMinHour ? minMinute : 0;
    const upper = isInMaxHour ? maxMinute : 59;
    const minutes = generateColumn(lower, upper, 'minute');
    ret.push(minutes.map(v => ({
      label: renderLabel('minute', v, {
        selected: selectedMinute === v
      }),
      value: v.toString()
    })));
  }
  if (rank >= precisionRankRecord.second) {
    const lower = isInMinMinute ? minSecond : 0;
    const upper = isInMaxMinute ? maxSecond : 59;
    const seconds = generateColumn(lower, upper, 'second');
    ret.push(seconds.map(v => ({
      label: renderLabel('second', v, {
        selected: selectedSecond === v
      }),
      value: v.toString()
    })));
  }
  // Till Now
  if (tillNow) {
    ret[0].push({
      label: renderLabel('now', null, {
        selected: selected[0] === _util.TILL_NOW
      }),
      value: _util.TILL_NOW
    });
    if (_util.TILL_NOW === (selected === null || selected === void 0 ? void 0 : selected[0])) {
      for (let i = 1; i < ret.length; i += 1) {
        ret[i] = [];
      }
    }
  }
  return ret;
}
function convertDateToStringArray(date) {
  if (!date) return [];
  return [date.getFullYear().toString(), (date.getMonth() + 1).toString(), date.getDate().toString(), date.getHours().toString(), date.getMinutes().toString(), date.getSeconds().toString()];
}
function convertStringArrayToDate(value) {
  var _a, _b, _c, _d, _e, _f;
  const yearString = (_a = value[0]) !== null && _a !== void 0 ? _a : '1900';
  const monthString = (_b = value[1]) !== null && _b !== void 0 ? _b : '1';
  const dateString = (_c = value[2]) !== null && _c !== void 0 ? _c : '1';
  const hourString = (_d = value[3]) !== null && _d !== void 0 ? _d : '0';
  const minuteString = (_e = value[4]) !== null && _e !== void 0 ? _e : '0';
  const secondString = (_f = value[5]) !== null && _f !== void 0 ? _f : '0';
  return new Date(parseInt(yearString), parseInt(monthString) - 1, parseInt(dateString), parseInt(hourString), parseInt(minuteString), parseInt(secondString));
}