/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { defineComponent, computed, ref, watch } from '@vue/composition-api';
import dayjs from 'dayjs';
import { CalendarIcon } from '@ldesign/icons-vue';
import { g as getDefaultFormat, f as formatDate, a as formatTime, p as parseToDayjs, i as isValidDate } from '../../_chunks/dep-c3901797.js';
import { s as subtractMonth, a as addMonth, e as extractTimeObj } from '../../_chunks/dep-68f38014.js';
import { usePrefixClass } from '../../hooks/use-config.js';
import { useGlobalIcon } from '../../hooks/use-global-icon.js';
import { RangeInputPopup } from '../range-input/index.js';
import { DateRangePickerProps } from './props.js';
import TRangePanel from './panel/range-panel.js';
import useRange from './hooks/use-range.js';
import { initYearMonthTime } from './hooks/use-range-value.js';
import '../../_chunks/dep-f38f4c93.js';
import '../../_chunks/dep-5210251b.js';
import '../../_chunks/dep-70c789e5.js';
import '../../_chunks/dep-24cf4e98.js';
import '../../_chunks/dep-eb467ddf.js';
import '../../_chunks/dep-b31eb7d9.js';
import '../../_chunks/dep-fe18d4b0.js';
import '../../_chunks/dep-22504add.js';
import '../config-provider/useConfig.js';
import '../config-provider/context.js';
import 'lodash';
import '../../_chunks/dep-def4c2ce.js';
import '../../utils/with-install.js';
import 'vue';
import '../../config.js';
import '../range-input/range-input.js';
import '../input/index.js';
import '../../utils/map-props.js';
import '../input/input.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../_chunks/dep-47d77fc8.js';
import '../../utils/helper.js';
import '../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../utils/mixins.js';
import '../../utils/event.js';
import '../../utils/render-node.js';
import '../input/props.js';
import '../input/group.js';
import '../../hooks/use-vmodel.js';
import '../../hooks/lnode.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import '../../hooks/render-lnode.js';
import '../range-input/props.js';
import '../range-input/range-input-popup.js';
import '../popup/index.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../../_chunks/dep-cf772670.js';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import '../popup/props.js';
import '../popup/container.js';
import '../select-input/hooks/use-overlay-innerstyle.js';
import 'lodash/isObject';
import './hooks/use-table-data.js';
import './hooks/use-disabled-date.js';
import '../../_chunks/dep-74554e14.js';
import './panel/extra-content.js';
import './base/Footer.js';
import '../button/index.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/loading.js';
import '../../utils/transfer-dom.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../loading/plugin.js';
import '../../utils/ripple.js';
import '../button/props.js';
import './panel/panel-content.js';
import '../../_chunks/dep-29915b8e.js';
import './base/Header.js';
import '../jumper/jumper.js';
import '../jumper/props.js';
import '../select/select.js';
import 'lodash/debounce';
import 'lodash/get';
import 'lodash/cloneDeep';
import 'lodash/isArray';
import '../../hooks/use-default-value.js';
import '../tag/index.js';
import '../tag/tag.js';
import '../tag/props.js';
import '../tag/check-tag.js';
import '../select-input/index.js';
import '../select-input/select-input.js';
import '../select-input/props.js';
import '../select-input/hooks/use-single.js';
import 'lodash/pick';
import '../select-input/hooks/use-multiple.js';
import '../tag-input/index.js';
import '../tag-input/tag-input.js';
import '../tag-input/props.js';
import '../tag-input/hooks/use-tag-scroll.js';
import '../tag-input/hooks/use-tag-list.js';
import '../tag-input/hooks/use-hover.js';
import '../tag-input/hooks/use-drag-sorter.js';
import '../common/fake-arrow.js';
import '../../hooks/use-form-disabled.js';
import '../select/option.js';
import '../checkbox/index.js';
import '../checkbox/group.js';
import 'lodash/intersection';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../../hooks/use-lazy-load.js';
import '../../_chunks/dep-356f585c.js';
import '../../hooks/use-common-classname.js';
import '../select/utils.js';
import '../select/props.js';
import '../select/select-panel.js';
import '../../hooks/use-virtual-scroll.js';
import '../select/hooks/use-select-options.js';
import './base/Table.js';
import './base/Cell.js';
import '../time-picker/panel/time-picker-panel.js';
import '../../_chunks/dep-2af9ce5e.js';
import 'lodash/range';
import 'lodash/padStart';
import '../time-picker/panel/props.js';
import '../time-picker/props.js';

var _DateRangePicker = defineComponent({
  name: "LDateRangePicker",
  props: {
    ...DateRangePickerProps
  },
  setup(props2, _ref) {
    let {
      emit
    } = _ref;
    const COMPONENT_NAME = usePrefixClass("date-range-picker");
    const {
      CalendarIcon: CalendarIcon$1
    } = useGlobalIcon({
      CalendarIcon: CalendarIcon
    });
    const {
      inputValue,
      popupVisible,
      rangeInputProps: dateRangePickerRangeInputProps,
      popupProps: dateRangePickerPopupProps,
      value,
      year,
      month,
      time,
      cacheValue,
      activeIndex,
      isHoverCell,
      isFirstValueSelected,
      onChange
    } = useRange(props2, {
      emit
    });
    const formatRef = computed(() => getDefaultFormat({
      mode: props2.mode,
      enableTimePicker: props2.enableTimePicker,
      format: props2.format,
      valueType: props2.valueType
    }));
    const isSelected = ref(false);
    watch(popupVisible, visible => {
      if (visible) {
        isSelected.value = false;
        cacheValue.value = formatDate(value.value || [], {
          format: formatRef.value.format
        });
        time.value = formatTime(value.value || [dayjs().format(formatRef.value.timeFormat), dayjs().format(formatRef.value.timeFormat)], formatRef.value.timeFormat);
        if (!value.value.length) {
          year.value = initYearMonthTime({
            value: value.value,
            mode: props2.mode,
            format: formatRef.value.format
          }).year;
          month.value = initYearMonthTime({
            value: value.value,
            mode: props2.mode,
            format: formatRef.value.format,
            enableTimePicker: props2.enableTimePicker
          }).month;
        } else if (value.value.length === 2 && !props2.enableTimePicker) {
          const nextMonth = value.value.map(v => parseToDayjs(v || new Date(), formatRef.value.format).month());
          if (year.value[0] === year.value[1] && nextMonth[0] === nextMonth[1]) nextMonth[0] === 11 ? nextMonth[0] -= 1 : nextMonth[1] += 1;
          month.value = nextMonth;
          year.value = value.value.map(v => parseToDayjs(v || new Date(), formatRef.value.format).year());
          if ((props2.mode === "month" || props2.mode === "quarter") && year.value[0] === year.value[1]) year.value = [year.value[0], year.value[0] + 1];
        } else {
          year.value = value.value.map(v => parseToDayjs(v || new Date(), formatRef.value.format).year());
          month.value = value.value.map(v => parseToDayjs(v || new Date(), formatRef.value.format).month());
        }
      } else {
        isHoverCell.value = false;
        isFirstValueSelected.value = false;
        inputValue.value = formatDate(value.value, {
          format: formatRef.value.format
        });
      }
    });
    function onCellMouseEnter(nextDate) {
      const date = nextDate;
      if (activeIndex.value && !props2.enableTimePicker) date.setHours(23, 59, 59);
      isHoverCell.value = true;
      const nextValue = [...inputValue.value];
      nextValue[activeIndex.value] = formatDate(date, {
        format: formatRef.value.format
      });
      inputValue.value = nextValue;
    }
    function onCellMouseLeave() {
      isHoverCell.value = false;
      inputValue.value = cacheValue.value;
    }
    function onCellClick(nextDate, _ref2) {
      var _props2$onPick;
      let {
        e,
        partial
      } = _ref2;
      const date = nextDate;
      if (activeIndex.value && !props2.enableTimePicker) date.setHours(23, 59, 59);
      (_props2$onPick = props2.onPick) === null || _props2$onPick === void 0 ? void 0 : _props2$onPick.call(props2, date, {
        e,
        partial: activeIndex.value ? "end" : "start"
      });
      emit("pick", date, {
        e,
        partial: activeIndex.value ? "end" : "start"
      });
      isHoverCell.value = false;
      isSelected.value = true;
      const nextValue = [...inputValue.value];
      nextValue[activeIndex.value] = formatDate(date, {
        format: formatRef.value.format
      });
      cacheValue.value = nextValue;
      inputValue.value = nextValue;
      if (props2.mode === "date") {
        const partialIndex = partial === "start" ? 0 : 1;
        const isAdditional = dayjs(date).month() !== month.value[partialIndex];
        if (isAdditional) {
          if (activeIndex.value === 0) month.value = [dayjs(date).month(), Math.min(dayjs(date).month() + 1, 11)];
          if (activeIndex.value === 1) month.value = [Math.max(dayjs(date).month() - 1, 0), dayjs(date).month()];
        }
      }
      if (props2.enableTimePicker) return;
      const notValidIndex = nextValue.findIndex(v => !v || !isValidDate(v, formatRef.value.format));
      if (notValidIndex === -1 && nextValue.length === 2) {
        if (!isFirstValueSelected.value && parseToDayjs(nextValue[0], formatRef.value.format).isAfter(parseToDayjs(nextValue[1], formatRef.value.format))) {
          nextValue[activeIndex.value ? 0 : 1] = "";
          cacheValue.value = nextValue;
          inputValue.value = nextValue;
        } else {
          onChange === null || onChange === void 0 ? void 0 : onChange(formatDate(nextValue, {
            format: formatRef.value.format,
            targetFormat: formatRef.value.valueType,
            autoSwap: true
          }), {
            dayjsValue: nextValue.map(v => parseToDayjs(v, formatRef.value.format)),
            trigger: "pick"
          });
        }
      }
      if (!isFirstValueSelected.value) {
        let nextIndex = notValidIndex;
        if (nextIndex === -1) nextIndex = activeIndex.value ? 0 : 1;
        activeIndex.value = nextIndex;
        isFirstValueSelected.value = true;
      } else {
        popupVisible.value = false;
      }
    }
    function onJumperClick(_ref3) {
      let {
        trigger,
        partial
      } = _ref3;
      const partialIndex = partial === "start" ? 0 : 1;
      const monthCountMap = {
        date: 1,
        week: 1,
        month: 12,
        quarter: 12,
        year: 120
      };
      const monthCount = monthCountMap[props2.mode] || 0;
      const current = new Date(year.value[partialIndex], month.value[partialIndex]);
      let next = null;
      if (trigger === "prev") next = subtractMonth(current, monthCount);else if (trigger === "current") next = new Date();else if (trigger === "next") next = addMonth(current, monthCount);
      const nextYear = [...year.value];
      nextYear[partialIndex] = next.getFullYear();
      const nextMonth = [...month.value];
      nextMonth[partialIndex] = next.getMonth();
      if (partialIndex === 0) {
        nextYear[1] = Math.max(nextYear[0], nextYear[1]);
        if (nextYear[0] === nextYear[1]) nextMonth[1] = Math.max(nextMonth[0], nextMonth[1]);
      }
      if (partialIndex === 1) {
        nextYear[0] = Math.min(nextYear[0], nextYear[1]);
        if (nextYear[0] === nextYear[1]) nextMonth[0] = Math.min(nextMonth[0], nextMonth[1]);
      }
      year.value = nextYear;
      month.value = nextMonth;
    }
    function onTimePickerChange(val) {
      const {
        hours,
        minutes,
        seconds,
        milliseconds,
        meridiem
      } = extractTimeObj(val);
      const nextInputValue = [...inputValue.value];
      const changedInputValue = inputValue.value[activeIndex.value];
      const currentDate = !dayjs(changedInputValue, formatRef.value.format).isValid() ? dayjs().year(year.value[activeIndex.value]).month(month.value[activeIndex.value]) : dayjs(changedInputValue, formatRef.value.format);
      let nextHours = hours;
      if (/am/i.test(meridiem) && nextHours === 12) nextHours -= 12;
      if (/pm/i.test(meridiem) && nextHours < 12) nextHours += 12;
      const nextDate = currentDate.hour(nextHours).minute(minutes).second(seconds).millisecond(milliseconds).toDate();
      nextInputValue[activeIndex.value] = nextDate;
      const nextTime = [...time.value];
      nextTime[activeIndex.value] = val;
      time.value = nextTime;
      isSelected.value = true;
      inputValue.value = formatDate(nextInputValue, {
        format: formatRef.value.format
      });
      cacheValue.value = formatDate(nextInputValue, {
        format: formatRef.value.format
      });
    }
    function onConfirmClick() {
      const nextValue = [...inputValue.value];
      const notValidIndex = nextValue.findIndex(v => !v || !isValidDate(v, formatRef.value.format));
      if (notValidIndex === -1 && nextValue.length === 2) {
        if (!isFirstValueSelected.value && parseToDayjs(nextValue[0], formatRef.value.format).isAfter(parseToDayjs(nextValue[1], formatRef.value.format))) {
          nextValue[activeIndex.value ? 0 : 1] = "";
          cacheValue.value = nextValue;
          inputValue.value = nextValue;
        } else {
          onChange === null || onChange === void 0 ? void 0 : onChange(formatDate(nextValue, {
            format: formatRef.value.format,
            targetFormat: formatRef.value.valueType,
            autoSwap: true
          }), {
            dayjsValue: nextValue.map(v => parseToDayjs(v, formatRef.value.format)),
            trigger: "confirm"
          });
        }
      }
      if (!isFirstValueSelected.value) {
        let nextIndex = notValidIndex;
        if (nextIndex === -1) nextIndex = activeIndex.value ? 0 : 1;
        activeIndex.value = nextIndex;
        isFirstValueSelected.value = true;
      } else {
        popupVisible.value = false;
      }
    }
    function onPresetClick(preset) {
      let presetValue = preset;
      if (typeof preset === "function") presetValue = preset();
      if (!Array.isArray(presetValue)) {
        console.error(`preset: ${preset} \u9884\u8BBE\u503C\u5FC5\u987B\u662F\u6570\u7EC4!`);
      } else {
        onChange === null || onChange === void 0 ? void 0 : onChange(formatDate(presetValue, {
          format: formatRef.value.format,
          targetFormat: formatRef.value.valueType,
          autoSwap: true
        }), {
          dayjsValue: presetValue.map(p => parseToDayjs(p, formatRef.value.format)),
          trigger: "preset"
        });
        popupVisible.value = false;
      }
    }
    function onYearChange(nextVal, _ref4) {
      let {
        partial
      } = _ref4;
      let partialIndex = partial === "start" ? 0 : 1;
      if (props2.enableTimePicker) partialIndex = activeIndex.value;
      const nextYear = [...year.value];
      nextYear[partialIndex] = nextVal;
      if (partialIndex === 0) nextYear[1] = Math.max(nextYear[0], nextYear[1]);
      if (partialIndex === 1) nextYear[0] = Math.min(nextYear[0], nextYear[1]);
      year.value = nextYear;
    }
    function onMonthChange(nextVal, _ref5) {
      let {
        partial
      } = _ref5;
      let partialIndex = partial === "start" ? 0 : 1;
      if (props2.enableTimePicker) partialIndex = activeIndex.value;
      const nextMonth = [...month.value];
      nextMonth[partialIndex] = nextVal;
      if (year[0] === year[1]) {
        if (partialIndex === 0) nextMonth[1] = Math.max(nextMonth[0], nextMonth[1]);
        if (partialIndex === 1) nextMonth[0] = Math.min(nextMonth[0], nextMonth[1]);
      }
      month.value = nextMonth;
    }
    const panelProps = computed(() => ({
      hoverValue: isHoverCell.value ? inputValue.value : [],
      value: isSelected.value ? cacheValue.value : value.value,
      isFirstValueSelected: isFirstValueSelected.value,
      activeIndex: activeIndex.value,
      year: year.value,
      month: month.value,
      format: formatRef.value.format,
      mode: props2.mode,
      presets: props2.presets,
      time: time.value,
      disableDate: props2.disableDate,
      firstDayOfWeek: props2.firstDayOfWeek,
      timePickerProps: props2.timePickerProps,
      enableTimePicker: props2.enableTimePicker,
      presetsPlacement: props2.presetsPlacement,
      panelPreselection: props2.panelPreselection,
      popupVisible: popupVisible.value,
      onCellClick,
      onCellMouseEnter,
      onCellMouseLeave,
      onJumperClick,
      onConfirmClick,
      onPresetClick,
      onYearChange,
      onMonthChange,
      onTimePickerChange
    }));
    return {
      COMPONENT_NAME,
      inputValue,
      dateRangePickerPopupProps,
      dateRangePickerRangeInputProps,
      popupVisible,
      panelProps,
      CalendarIcon: CalendarIcon$1
    };
  },
  render() {
    const h = arguments[0];
    const {
      COMPONENT_NAME,
      inputValue,
      dateRangePickerPopupProps,
      dateRangePickerRangeInputProps,
      popupVisible,
      panelProps,
      CalendarIcon
    } = this;
    const renderSuffixIcon = () => {
      if (this.suffixIcon) return this.suffixIcon;
      if (this.$scopedSlots.suffixIcon) return this.$scopedSlots.suffixIcon;
      if (this.$scopedSlots["suffix-icon"]) return this.$scopedSlots["suffix-icon"];
      return () => h(CalendarIcon);
    };
    return h("div", {
      "class": COMPONENT_NAME
    }, [h(RangeInputPopup, {
      "attrs": {
        "disabled": this.disabled,
        "status": this.status,
        "tips": this.tips,
        "inputValue": inputValue,
        "popupProps": dateRangePickerPopupProps,
        "rangeInputProps": {
          suffixIcon: renderSuffixIcon(),
          ...dateRangePickerRangeInputProps
        },
        "popupVisible": popupVisible,
        "panel": () => h(TRangePanel, {
          "props": {
            ...panelProps
          }
        })
      }
    })]);
  }
});

export { _DateRangePicker as default };
//# sourceMappingURL=date-range-picker.js.map
