"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.convertDateToStringArray = convertDateToStringArray;
exports.convertStringArrayToDate = convertStringArrayToDate;
exports.generateDatePickerColumns = generateDatePickerColumns;
var _dayjs = _interopRequireDefault(require("dayjs"));
var _quarterOfYear = _interopRequireDefault(require("dayjs/plugin/quarterOfYear"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
_dayjs.default.extend(_quarterOfYear.default);
const precisionRankRecord = {
  year: 0,
  quarter: 1
};
function generateDatePickerColumns(selected, min, max, precision, renderLabel, filter) {
  const ret = [];
  const minYear = min.getFullYear();
  const maxYear = max.getFullYear();
  const rank = precisionRankRecord[precision];
  const selectedYear = parseInt(selected[0]);
  const isInMinYear = selectedYear === minYear;
  const isInMaxYear = selectedYear === maxYear;
  const minDay = (0, _dayjs.default)(min);
  const maxDay = (0, _dayjs.default)(max);
  const minQuarter = minDay.quarter();
  const maxQuarter = maxDay.quarter();
  const selectedQuarter = parseInt(selected[1]);
  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.quarter) {
    const lower = isInMinYear ? minQuarter : 1;
    const upper = isInMaxYear ? maxQuarter : 4;
    const quarters = generateColumn(lower, upper, 'quarter');
    ret.push(quarters.map(v => ({
      label: renderLabel('quarter', v, {
        selected: selectedQuarter === v
      }),
      value: v.toString()
    })));
  }
  return ret;
}
function convertDateToStringArray(date) {
  if (!date) return [];
  const day = (0, _dayjs.default)(date);
  return [day.year().toString(), day.quarter().toString()];
}
function convertStringArrayToDate(value) {
  var _a, _b;
  const yearString = (_a = value[0]) !== null && _a !== void 0 ? _a : '1900';
  const quarterString = (_b = value[1]) !== null && _b !== void 0 ? _b : '1';
  const day = (0, _dayjs.default)().year(parseInt(yearString)).quarter(parseInt(quarterString)).hour(0).minute(0).second(0);
  return day.toDate();
}