'use strict';

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

var vue = require('vue');
var miscUtil = require('../utils/miscUtil.cjs');
var dateUtil = require('../utils/dateUtil.cjs');

function getStartEndDistance(startDate, endDate, picker, generateConfig) {
  const startNext = dateUtil.getClosingViewDate(startDate, picker, generateConfig, 1);
  function getDistance(compareFunc) {
    if (compareFunc(startDate, endDate))
      return "same";
    if (compareFunc(startNext, endDate))
      return "closing";
    return "far";
  }
  switch (picker) {
    case "year":
      return getDistance((start, end) => dateUtil.isSameDecade(generateConfig, start, end));
    case "quarter":
    case "month":
      return getDistance((start, end) => dateUtil.isSameYear(generateConfig, start, end));
    default:
      return getDistance((start, end) => dateUtil.isSameMonth(generateConfig, start, end));
  }
}
function getRangeViewDate(values, index, picker, generateConfig) {
  const startDate = miscUtil.getValue(values, 0);
  const endDate = miscUtil.getValue(values, 1);
  if (index === 0)
    return startDate;
  if (startDate && endDate) {
    const distance = getStartEndDistance(startDate, endDate, picker, generateConfig);
    switch (distance) {
      case "same":
        return startDate;
      case "closing":
        return startDate;
      default:
        return dateUtil.getClosingViewDate(endDate, picker, generateConfig, -1);
    }
  }
  return startDate;
}
function useRangeViewDates({
  values,
  picker,
  defaultDates,
  generateConfig
}) {
  const defaultViewDates = vue.ref([
    miscUtil.getValue(defaultDates, 0),
    miscUtil.getValue(defaultDates, 1)
  ]);
  const viewDates = vue.ref(null);
  const startDate = vue.computed(() => miscUtil.getValue(values.value, 0));
  const endDate = vue.computed(() => miscUtil.getValue(values.value, 1));
  const getViewDate = (index) => {
    if (defaultViewDates.value[index])
      return defaultViewDates.value[index];
    return miscUtil.getValue(viewDates.value, index) || getRangeViewDate(values.value, index, picker.value, generateConfig.value) || startDate.value || endDate.value || generateConfig.value.getNow();
  };
  const startViewDate = vue.ref(null);
  const endViewDate = vue.ref(null);
  vue.watchEffect(() => {
    startViewDate.value = getViewDate(0);
    endViewDate.value = getViewDate(1);
  });
  function setViewDate(viewDate, index) {
    if (viewDate) {
      let newViewDates = miscUtil.updateValues(viewDates.value, viewDate, index);
      defaultViewDates.value = miscUtil.updateValues(defaultViewDates.value, null, index) || [null, null];
      const anotherIndex = (index + 1) % 2;
      if (!miscUtil.getValue(values.value, anotherIndex))
        newViewDates = miscUtil.updateValues(newViewDates, viewDate, anotherIndex);
      viewDates.value = newViewDates;
    } else if (startDate.value || endDate.value) {
      viewDates.value = null;
    }
  }
  return [startViewDate, endViewDate, setViewDate];
}

exports.default = useRangeViewDates;
//# sourceMappingURL=useRangeViewDates.cjs.map
