"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const shared_esmBundler = require("../../../../../../@vue/shared/dist/shared.esm-bundler.js");
const index$1 = require("../../../../hooks/use-locale/index.js");
const index = require("../../../../hooks/use-namespace/index.js");
const constants = require("../constants.js");
const utils = require("../utils.js");
const useShortcut = require("./use-shortcut.js");
const vue = require("vue");
const dayjs_min = require("../../../../../../../../_virtual/dayjs.min.js");
const isEqual = require("../../../../../../lodash-es/isEqual.js");
const useRangePicker = (props, { defaultValue, defaultTime, leftDate, rightDate, step, unit, sortDates }) => {
  const { emit } = vue.getCurrentInstance();
  const { pickerNs } = vue.inject(constants.ROOT_PICKER_INJECTION_KEY);
  const drpNs = index.useNamespace("date-range-picker");
  const { t, lang } = index$1.useLocale();
  const handleShortcutClick = useShortcut.useShortcut(lang);
  const minDate = vue.ref();
  const maxDate = vue.ref();
  const rangeState = vue.ref({
    endDate: null,
    selecting: false
  });
  const handleChangeRange = (val) => {
    rangeState.value = val;
  };
  const handleRangeConfirm = (visible = false) => {
    const _minDate = vue.unref(minDate);
    const _maxDate = vue.unref(maxDate);
    if (utils.isValidRange([_minDate, _maxDate])) emit("pick", [_minDate, _maxDate], visible);
  };
  const onSelect = (selecting) => {
    rangeState.value.selecting = selecting;
    if (!selecting) rangeState.value.endDate = null;
  };
  const parseValue = (parsedValue) => {
    if (shared_esmBundler.isArray(parsedValue) && parsedValue.length === 2) {
      const [start, end] = parsedValue;
      minDate.value = start;
      leftDate.value = start;
      maxDate.value = end;
      sortDates(vue.unref(minDate), vue.unref(maxDate));
    } else restoreDefault();
  };
  const restoreDefault = () => {
    let [start, end] = utils.getDefaultValue(vue.unref(defaultValue), {
      lang: vue.unref(lang),
      step,
      unit,
      unlinkPanels: props.unlinkPanels
    });
    const getShift = (day) => {
      return day.diff(day.startOf("d"), "ms");
    };
    const maybeTimes = vue.unref(defaultTime);
    if (maybeTimes) {
      let leftShift = 0;
      let rightShift = 0;
      if (shared_esmBundler.isArray(maybeTimes)) {
        const [timeStart, timeEnd] = maybeTimes.map(dayjs_min.default);
        leftShift = getShift(timeStart);
        rightShift = getShift(timeEnd);
      } else {
        const shift = getShift(dayjs_min.default(maybeTimes));
        leftShift = shift;
        rightShift = shift;
      }
      start = start.startOf("d").add(leftShift, "ms");
      end = end.startOf("d").add(rightShift, "ms");
    }
    minDate.value = void 0;
    maxDate.value = void 0;
    leftDate.value = start;
    rightDate.value = end;
  };
  vue.watch(defaultValue, (val) => {
    if (val) restoreDefault();
  }, { immediate: true });
  vue.watch(() => props.parsedValue, (parsedValue) => {
    if (!(parsedValue == null ? void 0 : parsedValue.length) || !isEqual.default(parsedValue, [minDate.value, maxDate.value])) parseValue(parsedValue);
  }, { immediate: true });
  vue.watch(() => props.visible, () => {
    if (props.visible) parseValue(props.parsedValue);
  }, { immediate: true });
  return {
    minDate,
    maxDate,
    rangeState,
    lang,
    ppNs: pickerNs,
    drpNs,
    handleChangeRange,
    handleRangeConfirm,
    handleShortcutClick,
    onSelect,
    parseValue,
    t
  };
};
exports.useRangePicker = useRangePicker;
