"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = useRangeDisabled;
var _miscUtil = require("../utils/miscUtil");
var _dateUtil = require("../utils/dateUtil");
var _vueDemi = require("vue-demi");
function useRangeDisabled(_ref, openRecordsRef) {
  let {
    picker,
    locale,
    selectedValue,
    disabledDate,
    disabled,
    generateConfig
  } = _ref;
  const startDate = (0, _vueDemi.computed)(() => (0, _miscUtil.getValue)(selectedValue.value, 0));
  const endDate = (0, _vueDemi.computed)(() => (0, _miscUtil.getValue)(selectedValue.value, 1));
  function weekFirstDate(date) {
    return generateConfig.value.locale.getWeekFirstDate(locale.value.locale, date);
  }
  function monthNumber(date) {
    const year = generateConfig.value.getYear(date);
    const month = generateConfig.value.getMonth(date);
    return year * 100 + month;
  }
  function quarterNumber(date) {
    const year = generateConfig.value.getYear(date);
    const quarter = (0, _dateUtil.getQuarter)(generateConfig.value, date);
    return year * 10 + quarter;
  }
  const disabledStartDate = date => {
    var _a;
    if (disabledDate && ((_a = disabledDate === null || disabledDate === void 0 ? void 0 : disabledDate.value) === null || _a === void 0 ? void 0 : _a.call(disabledDate, date))) {
      return true;
    }
    // Disabled range
    if (disabled[1] && endDate) {
      return !(0, _dateUtil.isSameDate)(generateConfig.value, date, endDate.value) && generateConfig.value.isAfter(date, endDate.value);
    }
    // Disabled part
    if (openRecordsRef.value[1] && endDate.value) {
      switch (picker.value) {
        case 'quarter':
          return quarterNumber(date) > quarterNumber(endDate.value);
        case 'month':
          return monthNumber(date) > monthNumber(endDate.value);
        case 'week':
          return weekFirstDate(date) > weekFirstDate(endDate.value);
        default:
          return !(0, _dateUtil.isSameDate)(generateConfig.value, date, endDate.value) && generateConfig.value.isAfter(date, endDate.value);
      }
    }
    return false;
  };
  const disabledEndDate = date => {
    var _a;
    if ((_a = disabledDate.value) === null || _a === void 0 ? void 0 : _a.call(disabledDate, date)) {
      return true;
    }
    // Disabled range
    if (disabled[0] && startDate) {
      return !(0, _dateUtil.isSameDate)(generateConfig.value, date, endDate.value) && generateConfig.value.isAfter(startDate.value, date);
    }
    // Disabled part
    if (openRecordsRef.value[0] && startDate.value) {
      switch (picker.value) {
        case 'quarter':
          return quarterNumber(date) < quarterNumber(startDate.value);
        case 'month':
          return monthNumber(date) < monthNumber(startDate.value);
        case 'week':
          return weekFirstDate(date) < weekFirstDate(startDate.value);
        default:
          return !(0, _dateUtil.isSameDate)(generateConfig.value, date, startDate.value) && generateConfig.value.isAfter(startDate.value, date);
      }
    }
    return false;
  };
  return [disabledStartDate, disabledEndDate];
}