/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, computed, ref, createVNode } from 'vue';
import dayjs from 'dayjs';
import isFunction from 'lodash/isFunction';
import isArray from 'lodash/isArray';
import { getDefaultFormat, formatDate, parseToDayjs } from '../_common/js/date-picker/format.js';
import { subtractMonth, addMonth, extractTimeObj } from '../_common/js/date-picker/utils.js';
import dateRangePickerPanelProps from './date-range-picker-panel-props.js';
import dateRangePickerProps from './date-range-picker-props.js';
import TRangePanel from './panel/RangePanel.js';
import useRangeValue from './hooks/useRangeValue.js';
import 'lodash/isString';
import '../_common/js/log/index.js';
import '../_common/js/log/log.js';
import '@babel/runtime/helpers/slicedToArray';
import '@babel/runtime/helpers/objectDestructuringEmpty';
import '@babel/runtime/helpers/extends';
import '../_chunks/dep-512eb181.js';
import '../_chunks/dep-ccfdbba1.js';
import 'lodash/chunk';
import '../hooks/useConfig.js';
import '../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-ac28b67c.js';
import '../config-provider/type.js';
import './hooks/useTableData.js';
import './hooks/useDisableDate.js';
import 'lodash/isObject';
import './panel/ExtraContent.js';
import './base/Footer.js';
import '../button/index.js';
import '../utils/withInstall.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/directive.js';
import '../loading/plugin.js';
import '../utils/dom.js';
import '../utils/easing.js';
import '../loading/loading.js';
import '../utils/render-tnode.js';
import 'lodash/isEmpty';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../hooks/useTeleport.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../_common/js/utils/set-style.js';
import '../_common/js/utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'lodash/isNull';
import 'lodash/isUndefined';
import 'lodash/isNumber';
import '../loading/style';
import '../loading/type.js';
import '../hooks/useRipple.js';
import '../utils/set-style.js';
import '../hooks/useKeepAnimation.js';
import '../form/hooks.js';
import '../hooks/tnode.js';
import '../button/props.js';
import '../button/style';
import '../button/type.js';
import './panel/PanelContent.js';
import './base/Header.js';
import '../pagination/index.js';
import '../pagination/pagination.js';
import 'lodash/isNaN';
import '@ldesign/icons-vue-next';
import '../hooks/useGlobalIcon.js';
import '../input-number/index.js';
import '../input-number/input-number.js';
import '../input/index.js';
import '../input/input.js';
import '../input/props.js';
import '../input/useInput.js';
import '../form/const.js';
import '../hooks/index.js';
import '../hooks/icon.js';
import '../hooks/slot.js';
import '../hooks/useCommonClassName.js';
import '../hooks/useDefaultValue.js';
import '../hooks/useDestroyOnClose.js';
import '../hooks/useVirtualScroll.js';
import '../hooks/useVModel.js';
import '../input/useLengthLimit.js';
import '../input/useInputEventHandler.js';
import '../input/useInputWidth.js';
import '../input/input-group.js';
import '../input/input-group-props.js';
import '../input/style';
import '../input/type.js';
import '../input-number/props.js';
import '../input-number/useInputNumber.js';
import '../_common/js/input-number/number.js';
import 'lodash/isNil';
import '../_common/js/input-number/large-number.js';
import '../input-number/style';
import '../input-number/type.js';
import '../select/index.js';
import '../select/select.js';
import 'lodash/pick';
import 'lodash/debounce';
import 'lodash/get';
import 'lodash/intersection';
import '../common-components/fake-arrow.js';
import '../select-input/index.js';
import '../select-input/select-input.js';
import '../popup/index.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../popup/props.js';
import '../popup/container.js';
import '../hooks/useResizeObserver.js';
import '../popup/style';
import '../popup/type.js';
import '../select-input/props.js';
import '../select-input/useSingle.js';
import '../select-input/useMultiple.js';
import '../tag-input/index.js';
import '../tag-input/tag-input.js';
import '../tag-input/props.js';
import '../tag-input/hooks/useTagScroll.js';
import '../tag-input/useTagList.js';
import '../tag/index.js';
import '../tag/tag.js';
import '../tag/props.js';
import '../tag/check-tag.js';
import '../tag/check-tag-props.js';
import '../tag/style';
import '../tag/type.js';
import '../tag-input/hooks/useHover.js';
import '../tag-input/hooks/useDragSorter.js';
import '../tag-input/style';
import '../tag-input/type.js';
import '../select-input/useOverlayInnerStyle.js';
import '../select-input/style';
import '../select-input/type.js';
import '../select/props.js';
import '../select/select-panel.js';
import 'lodash/omit';
import '../select/option.js';
import '../checkbox/index.js';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../checkbox/constants.js';
import '../checkbox/group.js';
import '../checkbox/checkbox-group-props.js';
import '../checkbox/style';
import '../checkbox/type.js';
import '../select/option-props.js';
import '../select/helper.js';
import '../select/optionGroup.js';
import '../select/option-group-props.js';
import '../select/hooks/usePanelVirtualScroll.js';
import '../select/hooks/useSelectOptions.js';
import '../select/style';
import '../select/type.js';
import '../input-adornment/index.js';
import '../input-adornment/input-adornment.js';
import '../input-adornment/props.js';
import '../input-adornment/style';
import '../input-adornment/type.js';
import '../pagination/props.js';
import '../pagination/usePaginationClasses.js';
import '../pagination/useMoreAction.js';
import '../pagination/pagination-mini.js';
import '../pagination/pagination-mini-props.js';
import '../pagination/style';
import '../pagination/type.js';
import './base/Table.js';
import './base/Cell.js';
import '../time-picker/panel/time-picker-panel.js';
import '../_common/js/time-picker/const.js';
import '../time-picker/panel/props.js';
import 'lodash/isBoolean';
import '../time-picker/props.js';
import '../time-picker/panel/single-panel.js';
import 'lodash/range';
import 'lodash/padStart';
import '../_common/js/time-picker/utils.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var _DateRangePickerPanel = defineComponent({
  name: "TDateRangePickerPanel",
  props: _objectSpread({
    value: dateRangePickerProps.value,
    defaultValue: dateRangePickerProps.defaultValue,
    modelValue: dateRangePickerProps.modelValue,
    disabled: dateRangePickerProps.disabled,
    disableDate: dateRangePickerProps.disableDate,
    enableTimePicker: dateRangePickerProps.enableTimePicker,
    firstDayOfWeek: dateRangePickerProps.firstDayOfWeek,
    format: dateRangePickerProps.format,
    mode: dateRangePickerProps.mode,
    presets: dateRangePickerProps.presets,
    presetsPlacement: dateRangePickerProps.presetsPlacement,
    timePickerProps: dateRangePickerProps.timePickerProps,
    panelPreselection: dateRangePickerProps.panelPreselection
  }, dateRangePickerPanelProps),
  setup: function setup(props) {
    var _useRangeValue = useRangeValue(props),
      value = _useRangeValue.value,
      year = _useRangeValue.year,
      month = _useRangeValue.month,
      time = _useRangeValue.time,
      cacheValue = _useRangeValue.cacheValue,
      isFirstValueSelected = _useRangeValue.isFirstValueSelected,
      onChange = _useRangeValue.onChange;
    var formatRef = computed(function () {
      return getDefaultFormat({
        mode: props.mode,
        enableTimePicker: props.enableTimePicker,
        format: props.format
      });
    });
    var isSelected = ref(false);
    var isHoverCell = ref(false);
    var hoverValue = ref([]);
    var activeIndex = computed(function () {
      return isFirstValueSelected.value ? 1 : 0;
    });
    function onCellMouseEnter(date) {
      isHoverCell.value = true;
      var nextValue = _toConsumableArray(hoverValue.value);
      nextValue[activeIndex.value] = formatDate(date, {
        format: formatRef.value.format
      });
      hoverValue.value = nextValue;
    }
    function onCellMouseLeave() {
      isHoverCell.value = false;
      hoverValue.value = cacheValue.value;
    }
    function onCellClick(date, _ref) {
      var _props$onCellClick;
      var e = _ref.e;
      (_props$onCellClick = props.onCellClick) === null || _props$onCellClick === void 0 ? void 0 : _props$onCellClick.call(props, {
        e: e,
        partial: activeIndex.value ? "end" : "start",
        date: value.value.map(function (v) {
          return dayjs(v).toDate();
        })
      });
      isHoverCell.value = false;
      isSelected.value = true;
      var nextValue = _toConsumableArray(cacheValue.value);
      nextValue[activeIndex.value] = formatDate(date, {
        format: formatRef.value.format
      });
      cacheValue.value = nextValue;
      if (props.enableTimePicker) return;
      if (nextValue.length === 2 && isFirstValueSelected.value) {
        onChange === null || onChange === void 0 ? void 0 : onChange(formatDate(nextValue, {
          format: formatRef.value.format,
          autoSwap: true
        }), {
          dayjsValue: nextValue.map(function (v) {
            return parseToDayjs(v, formatRef.value.format);
          }),
          trigger: "pick"
        });
        isFirstValueSelected.value = false;
      } else {
        isFirstValueSelected.value = true;
      }
    }
    function onJumperClick(_ref2) {
      var trigger = _ref2.trigger,
        partial = _ref2.partial;
      var partialIndex = partial === "start" ? 0 : 1;
      var triggerMap = {
        prev: "arrow-previous",
        next: "arrow-next"
      };
      var monthCountMap = {
        date: 1,
        month: 12,
        year: 120
      };
      var monthCount = monthCountMap[props.mode] || 0;
      var current = new Date(year.value[partialIndex], month.value[partialIndex]);
      var next = null;
      if (trigger === "prev") next = subtractMonth(current, monthCount);else if (trigger === "current") next = new Date();else if (trigger === "next") next = addMonth(current, monthCount);
      var nextYear = _toConsumableArray(year.value);
      nextYear[partialIndex] = next.getFullYear();
      var nextMonth = _toConsumableArray(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]);
      }
      if (year.value.some(function (y) {
        return !nextYear.includes(y);
      })) {
        var _props$onYearChange;
        (_props$onYearChange = props.onYearChange) === null || _props$onYearChange === void 0 ? void 0 : _props$onYearChange.call(props, {
          partial: partial,
          year: nextYear[partialIndex],
          date: value.value.map(function (v) {
            return dayjs(v).toDate();
          }),
          trigger: trigger === "current" ? "today" : "year-".concat(triggerMap[trigger])
        });
      }
      if (month.value.some(function (m) {
        return !nextMonth.includes(m);
      })) {
        var _props$onMonthChange;
        (_props$onMonthChange = props.onMonthChange) === null || _props$onMonthChange === void 0 ? void 0 : _props$onMonthChange.call(props, {
          partial: partial,
          month: nextMonth[partialIndex],
          date: value.value.map(function (v) {
            return dayjs(v).toDate();
          }),
          trigger: trigger === "current" ? "today" : "month-".concat(triggerMap[trigger])
        });
      }
      year.value = nextYear;
      month.value = nextMonth;
    }
    function onTimePickerChange(val) {
      var _props$onTimeChange;
      var _extractTimeObj = extractTimeObj(val),
        hours = _extractTimeObj.hours,
        minutes = _extractTimeObj.minutes,
        seconds = _extractTimeObj.seconds,
        milliseconds = _extractTimeObj.milliseconds,
        meridiem = _extractTimeObj.meridiem;
      var nextInputValue = _toConsumableArray(cacheValue.value);
      var changedInputValue = cacheValue.value[activeIndex.value];
      var currentDate = !dayjs(changedInputValue, formatRef.value.format).isValid() ? dayjs().year(year.value[activeIndex.value]).month(month.value[activeIndex.value]) : dayjs(changedInputValue, formatRef.value.format);
      var nextHours = hours;
      if (/am/i.test(meridiem) && nextHours === 12) nextHours -= 12;
      if (/pm/i.test(meridiem) && nextHours < 12) nextHours += 12;
      var nextDate = currentDate.hour(nextHours).minute(minutes).second(seconds).millisecond(milliseconds).toDate();
      nextInputValue[activeIndex.value] = nextDate;
      var nextTime = _toConsumableArray(time.value);
      nextTime[activeIndex.value] = val;
      time.value = nextTime;
      isSelected.value = true;
      cacheValue.value = formatDate(nextInputValue, {
        format: formatRef.value.format
      });
      (_props$onTimeChange = props.onTimeChange) === null || _props$onTimeChange === void 0 ? void 0 : _props$onTimeChange.call(props, {
        time: val,
        date: value.value.map(function (v) {
          return parseToDayjs(v, formatRef.value.format).toDate();
        }),
        partial: activeIndex.value ? "end" : "start",
        trigger: "time-hour"
      });
    }
    function onConfirmClick(_ref3) {
      var _props$onConfirm;
      var e = _ref3.e;
      var nextValue = _toConsumableArray(cacheValue.value);
      if (nextValue.length === 2 && isFirstValueSelected.value) {
        onChange === null || onChange === void 0 ? void 0 : onChange(formatDate(nextValue, {
          format: formatRef.value.format,
          autoSwap: true
        }), {
          dayjsValue: nextValue.map(function (v) {
            return parseToDayjs(v, formatRef.value.format);
          }),
          trigger: "confirm"
        });
        year.value = nextValue.map(function (v) {
          return dayjs(v, formatRef.value.format).year();
        });
        month.value = nextValue.map(function (v) {
          return dayjs(v, formatRef.value.format).month();
        });
        isFirstValueSelected.value = false;
      } else {
        isFirstValueSelected.value = true;
      }
      (_props$onConfirm = props.onConfirm) === null || _props$onConfirm === void 0 ? void 0 : _props$onConfirm.call(props, {
        date: value.value.map(function (v) {
          return dayjs(v).toDate();
        }),
        e: e
      });
    }
    function onPresetClick(preset) {
      var presetValue = preset;
      if (isFunction(preset)) presetValue = preset();
      if (!isArray(presetValue)) {
        console.error("preset: ".concat(preset, " \u9884\u8BBE\u503C\u5FC5\u987B\u662F\u6570\u7EC4!"));
      } else {
        onChange === null || onChange === void 0 ? void 0 : onChange(formatDate(presetValue, {
          format: formatRef.value.format,
          autoSwap: true
        }), {
          dayjsValue: presetValue.map(function (p) {
            return parseToDayjs(p, formatRef.value.format);
          }),
          trigger: "preset"
        });
      }
    }
    function onYearChange(nextVal, _ref4) {
      var _props$onYearChange2;
      var partial = _ref4.partial;
      var partialIndex = partial === "start" ? 0 : 1;
      if (props.enableTimePicker) partialIndex = activeIndex.value;
      var nextYear = _toConsumableArray(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;
      (_props$onYearChange2 = props.onYearChange) === null || _props$onYearChange2 === void 0 ? void 0 : _props$onYearChange2.call(props, {
        partial: partial,
        year: nextYear[partialIndex],
        date: value.value.map(function (v) {
          return dayjs(v).toDate();
        }),
        trigger: "year-select"
      });
    }
    function onMonthChange(nextVal, _ref5) {
      var _props$onMonthChange2;
      var partial = _ref5.partial;
      var partialIndex = partial === "start" ? 0 : 1;
      if (props.enableTimePicker) partialIndex = activeIndex.value;
      var nextMonth = _toConsumableArray(month.value);
      nextMonth[partialIndex] = nextVal;
      if (year.value[0] === year.value[1]) {
        if (partialIndex === 0) nextMonth[1] = Math.max.apply(Math, _toConsumableArray(nextMonth));
        if (partialIndex === 1) nextMonth[0] = Math.min.apply(Math, _toConsumableArray(nextMonth));
      }
      month.value = nextMonth;
      (_props$onMonthChange2 = props.onMonthChange) === null || _props$onMonthChange2 === void 0 ? void 0 : _props$onMonthChange2.call(props, {
        partial: partial,
        month: nextMonth[partialIndex],
        date: value.value.map(function (v) {
          return dayjs(v).toDate();
        }),
        trigger: "month-select"
      });
    }
    var panelProps = computed(function () {
      return {
        hoverValue: isHoverCell.value ? hoverValue.value : [],
        value: isSelected.value ? cacheValue.value : value.value,
        activeIndex: activeIndex.value,
        year: year.value,
        month: month.value,
        mode: props.mode,
        format: formatRef.value.format,
        presets: props.presets,
        time: time.value,
        disableDate: props.disableDate,
        firstDayOfWeek: props.firstDayOfWeek,
        timePickerProps: props.timePickerProps,
        enableTimePicker: props.enableTimePicker,
        presetsPlacement: props.presetsPlacement,
        panelPreselection: props.panelPreselection,
        panelClick: props.onPanelClick,
        onCellClick: onCellClick,
        onCellMouseEnter: onCellMouseEnter,
        onCellMouseLeave: onCellMouseLeave,
        onJumperClick: onJumperClick,
        onConfirmClick: onConfirmClick,
        onPresetClick: onPresetClick,
        onYearChange: onYearChange,
        onMonthChange: onMonthChange,
        onTimePickerChange: onTimePickerChange
      };
    });
    return function () {
      return createVNode(TRangePanel, panelProps.value, null);
    };
  }
});

export { _DateRangePickerPanel as default };
//# sourceMappingURL=DateRangePickerPanel.js.map
