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

'use strict';

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

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var dayjs = require('dayjs');
var isFunction = require('lodash/isFunction');
var isArray = require('lodash/isArray');
var form_hooks = require('../form/hooks.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var rangeInput_index = require('../range-input/index.js');
var _common_js_datePicker_format = require('../_common/js/date-picker/format.js');
var _common_js_datePicker_utils = require('../_common/js/date-picker/utils.js');
var datePicker_dateRangePickerProps = require('./date-range-picker-props.js');
var datePicker_panel_RangePanel = require('./panel/RangePanel.js');
var datePicker_hooks_useRange = require('./hooks/useRange.js');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('lodash/isString');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-7f0559c3.js');
require('../utils/withInstall.js');
require('../range-input/range-input.js');
require('@babel/runtime/helpers/slicedToArray');
require('@ldesign/icons-vue-next');
require('../input/index.js');
require('../input/input.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/isUndefined');
require('../hooks/useGlobalIcon.js');
require('../hooks/tnode.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isObject');
require('../input/props.js');
require('../input/useInput.js');
require('../form/const.js');
require('../hooks/useVModel.js');
require('../input/useLengthLimit.js');
require('lodash/isNumber');
require('../_common/js/log/log.js');
require('../_common/js/utils/helper.js');
require('lodash/isNull');
require('../input/useInputEventHandler.js');
require('../input/useInputWidth.js');
require('../input/input-group.js');
require('../input/input-group-props.js');
require('../range-input/props.js');
require('../range-input/range-input-popup.js');
require('../popup/index.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../utils/dom.js');
require('../utils/easing.js');
require('../_common/js/utils/set-style.js');
require('../popup/container.js');
require('../hooks/useResizeObserver.js');
require('../popup/props.js');
require('../select-input/useOverlayInnerStyle.js');
require('../range-input/range-input-popup-props.js');
require('@babel/runtime/helpers/objectDestructuringEmpty');
require('@babel/runtime/helpers/extends');
require('../_chunks/dep-69993bc1.js');
require('lodash/chunk');
require('./hooks/useTableData.js');
require('./hooks/useDisableDate.js');
require('./panel/ExtraContent.js');
require('./base/Footer.js');
require('../button/index.js');
require('../button/button.js');
require('../loading/index.js');
require('../loading/directive.js');
require('../loading/plugin.js');
require('../loading/loading.js');
require('../hooks/useTeleport.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../hooks/useRipple.js');
require('../utils/set-style.js');
require('../hooks/useKeepAnimation.js');
require('../button/props.js');
require('./panel/PanelContent.js');
require('./base/Header.js');
require('../pagination/index.js');
require('../pagination/pagination.js');
require('lodash/isNaN');
require('../input-number/index.js');
require('../input-number/input-number.js');
require('../input-number/props.js');
require('../input-number/useInputNumber.js');
require('../hooks/useCommonClassName.js');
require('../_common/js/input-number/number.js');
require('lodash/isNil');
require('../_common/js/input-number/large-number.js');
require('../select/index.js');
require('../select/select.js');
require('lodash/pick');
require('lodash/debounce');
require('lodash/get');
require('lodash/intersection');
require('../common-components/fake-arrow.js');
require('../select-input/index.js');
require('../select-input/select-input.js');
require('../select-input/props.js');
require('../select-input/useSingle.js');
require('../hooks/useDefaultValue.js');
require('../select-input/useMultiple.js');
require('../tag-input/index.js');
require('../tag-input/tag-input.js');
require('../tag-input/props.js');
require('../tag-input/hooks/useTagScroll.js');
require('../tag-input/useTagList.js');
require('../tag/index.js');
require('../tag/tag.js');
require('../tag/props.js');
require('../tag/check-tag.js');
require('../tag/check-tag-props.js');
require('../tag-input/hooks/useHover.js');
require('../tag-input/hooks/useDragSorter.js');
require('../select/props.js');
require('../select/select-panel.js');
require('lodash/omit');
require('../select/option.js');
require('../checkbox/index.js');
require('../checkbox/checkbox.js');
require('../checkbox/props.js');
require('../checkbox/constants.js');
require('../checkbox/group.js');
require('../hooks/slot.js');
require('../checkbox/checkbox-group-props.js');
require('../select/option-props.js');
require('../select/helper.js');
require('../select/optionGroup.js');
require('../select/option-group-props.js');
require('../select/hooks/usePanelVirtualScroll.js');
require('../hooks/useVirtualScroll.js');
require('../select/hooks/useSelectOptions.js');
require('../input-adornment/index.js');
require('../input-adornment/input-adornment.js');
require('../input-adornment/props.js');
require('../pagination/props.js');
require('../pagination/usePaginationClasses.js');
require('../pagination/useMoreAction.js');
require('../pagination/pagination-mini.js');
require('../pagination/pagination-mini-props.js');
require('./base/Table.js');
require('./base/Cell.js');
require('../time-picker/panel/time-picker-panel.js');
require('../_common/js/time-picker/const.js');
require('../time-picker/panel/props.js');
require('lodash/isBoolean');
require('../time-picker/props.js');
require('../time-picker/panel/single-panel.js');
require('lodash/range');
require('lodash/padStart');
require('../_common/js/time-picker/utils.js');
require('./hooks/useRangeValue.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);

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__default["default"](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 _DateRangePicker = vue.defineComponent({
  name: "TDateRangePicker",
  props: _objectSpread({}, datePicker_dateRangePickerProps["default"]),
  setup: function setup(props2, _ref) {
    var slots = _ref.slots;
    var COMPONENT_NAME = hooks_useConfig.usePrefixClass("date-range-picker");
    var _useRange = datePicker_hooks_useRange["default"](props2),
      inputValue = _useRange.inputValue,
      popupVisible = _useRange.popupVisible,
      rangeInputProps = _useRange.rangeInputProps,
      popupProps = _useRange.popupProps,
      value = _useRange.value,
      year = _useRange.year,
      month = _useRange.month,
      time = _useRange.time,
      cacheValue = _useRange.cacheValue,
      activeIndex = _useRange.activeIndex,
      isHoverCell = _useRange.isHoverCell,
      isFirstValueSelected = _useRange.isFirstValueSelected,
      onChange = _useRange.onChange;
    var disabled = form_hooks.useFormDisabled();
    var formatRef = vue.computed(function () {
      return _common_js_datePicker_format.getDefaultFormat({
        mode: props2.mode,
        enableTimePicker: props2.enableTimePicker,
        format: props2.format,
        valueType: props2.valueType
      });
    });
    var isSelected = vue.ref(false);
    vue.watch(popupVisible, function (visible) {
      if (visible) {
        isSelected.value = false;
        cacheValue.value = _common_js_datePicker_format.formatDate(value.value || [], {
          format: formatRef.value.format
        });
        time.value = _common_js_datePicker_format.formatTime(value.value || [dayjs__default["default"]().format(formatRef.value.timeFormat), dayjs__default["default"]().format(formatRef.value.timeFormat)], formatRef.value.format, formatRef.value.timeFormat, props2.defaultTime);
        if (!value.value.length) {
          var _initYearMonthTime = _common_js_datePicker_format.initYearMonthTime({
              value: value.value,
              mode: props2.mode,
              format: formatRef.value.format,
              enableTimePicker: props2.enableTimePicker
            }),
            defaultYear = _initYearMonthTime.year,
            defaultMonth = _initYearMonthTime.month;
          year.value = defaultYear;
          month.value = defaultMonth;
        } else if (value.value.length === 2 && !props2.enableTimePicker) {
          var nextMonth = value.value.map(function (v) {
            return _common_js_datePicker_format.parseToDayjs(v, 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(function (v) {
            return _common_js_datePicker_format.parseToDayjs(v, 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(function (v) {
            return _common_js_datePicker_format.parseToDayjs(v, formatRef.value.format).year();
          });
          if (year.value.length === 1) year.value = [year.value[0], year.value[0]];
          month.value = value.value.map(function (v) {
            return _common_js_datePicker_format.parseToDayjs(v, formatRef.value.format).month();
          });
          if (month.value.length === 1) month.value = [month.value[0], Math.min(month.value[0] + 1, 11)];
        }
      } else {
        isHoverCell.value = false;
        isFirstValueSelected.value = false;
        inputValue.value = _common_js_datePicker_format.formatDate(value.value, {
          format: formatRef.value.format
        });
      }
    });
    function onCellMouseEnter(date) {
      isHoverCell.value = true;
      var nextValue = _toConsumableArray__default["default"](inputValue.value);
      nextValue[activeIndex.value] = _common_js_datePicker_format.formatDate(date, {
        format: formatRef.value.format
      });
      inputValue.value = nextValue;
    }
    function onCellMouseLeave() {
      isHoverCell.value = false;
      inputValue.value = cacheValue.value;
    }
    function onCellClick(date, _ref2) {
      var _props2$onPick;
      var e = _ref2.e,
        partial = _ref2.partial;
      (_props2$onPick = props2.onPick) === null || _props2$onPick === void 0 ? void 0 : _props2$onPick.call(props2, date, {
        e: e,
        partial: activeIndex.value ? "end" : "start"
      });
      isHoverCell.value = false;
      isSelected.value = true;
      var nextValue = _toConsumableArray__default["default"](inputValue.value);
      nextValue[activeIndex.value] = _common_js_datePicker_format.formatDate(date, {
        format: formatRef.value.format
      });
      cacheValue.value = nextValue;
      inputValue.value = nextValue;
      if (props2.mode === "date") {
        var partialIndex = partial === "start" ? 0 : 1;
        var isAdditional = dayjs__default["default"](date).month() !== month.value[partialIndex];
        if (isAdditional) {
          if (activeIndex.value === 0) month.value = [dayjs__default["default"](date).month(), Math.min(dayjs__default["default"](date).month() + 1, 11)];
          if (activeIndex.value === 1) month.value = [Math.max(dayjs__default["default"](date).month() - 1, 0), dayjs__default["default"](date).month()];
        }
      }
      if (props2.enableTimePicker) return;
      var notValidIndex = nextValue.findIndex(function (v) {
        return !v || !_common_js_datePicker_format.isValidDate(v, formatRef.value.format);
      });
      if (notValidIndex === -1 && nextValue.length === 2) {
        if (!isFirstValueSelected.value && _common_js_datePicker_format.parseToDayjs(nextValue[0], formatRef.value.format).isAfter(_common_js_datePicker_format.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(_common_js_datePicker_format.formatDate(nextValue, {
            format: formatRef.value.format,
            targetFormat: formatRef.value.valueType,
            autoSwap: true
          }), {
            dayjsValue: nextValue.map(function (v) {
              return _common_js_datePicker_format.parseToDayjs(v, formatRef.value.format);
            }),
            trigger: "pick"
          });
        }
      }
      if (!isFirstValueSelected.value) {
        var nextIndex = notValidIndex;
        if (nextIndex === -1) nextIndex = activeIndex.value ? 0 : 1;
        activeIndex.value = nextIndex;
        isFirstValueSelected.value = true;
      } else {
        popupVisible.value = false;
      }
    }
    function onJumperClick(_ref3) {
      var trigger = _ref3.trigger,
        partial = _ref3.partial;
      var partialIndex = partial === "start" ? 0 : 1;
      var monthCountMap = {
        date: 1,
        week: 1,
        month: 12,
        quarter: 12,
        year: 120
      };
      var monthCount = monthCountMap[props2.mode] || 0;
      var current = new Date(year.value[partialIndex], month.value[partialIndex]);
      var next = null;
      if (trigger === "prev") next = _common_js_datePicker_utils.subtractMonth(current, monthCount);else if (trigger === "current") next = new Date();else if (trigger === "next") next = _common_js_datePicker_utils.addMonth(current, monthCount);
      var nextYear = _toConsumableArray__default["default"](year.value);
      nextYear[partialIndex] = next.getFullYear();
      var nextMonth = _toConsumableArray__default["default"](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) {
      var _extractTimeObj = _common_js_datePicker_utils.extractTimeObj(val),
        hours = _extractTimeObj.hours,
        minutes = _extractTimeObj.minutes,
        seconds = _extractTimeObj.seconds,
        milliseconds = _extractTimeObj.milliseconds,
        meridiem = _extractTimeObj.meridiem;
      var nextInputValue = _toConsumableArray__default["default"](inputValue.value);
      var changedInputValue = inputValue.value[activeIndex.value];
      var currentDate = !dayjs__default["default"](changedInputValue, formatRef.value.format).isValid() ? dayjs__default["default"]().year(year.value[activeIndex.value]).month(month.value[activeIndex.value]) : dayjs__default["default"](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__default["default"](time.value);
      nextTime[activeIndex.value] = val;
      time.value = nextTime;
      isSelected.value = true;
      inputValue.value = _common_js_datePicker_format.formatDate(nextInputValue, {
        format: formatRef.value.format
      });
      cacheValue.value = _common_js_datePicker_format.formatDate(nextInputValue, {
        format: formatRef.value.format
      });
    }
    function onConfirmClick() {
      var nextValue = _toConsumableArray__default["default"](inputValue.value);
      var notValidIndex = nextValue.findIndex(function (v) {
        return !v || !_common_js_datePicker_format.isValidDate(v, formatRef.value.format);
      });
      if (notValidIndex === -1 && nextValue.length === 2) {
        if (!isFirstValueSelected.value && _common_js_datePicker_format.parseToDayjs(nextValue[0], formatRef.value.format).isAfter(_common_js_datePicker_format.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(_common_js_datePicker_format.formatDate(nextValue, {
            format: formatRef.value.format,
            targetFormat: formatRef.value.valueType,
            autoSwap: true
          }), {
            dayjsValue: nextValue.map(function (v) {
              return _common_js_datePicker_format.parseToDayjs(v, formatRef.value.format);
            }),
            trigger: "confirm"
          });
        }
      }
      if (!isFirstValueSelected.value) {
        var nextIndex = notValidIndex;
        if (nextIndex === -1) nextIndex = activeIndex.value ? 0 : 1;
        activeIndex.value = nextIndex;
        isFirstValueSelected.value = true;
      } else {
        popupVisible.value = false;
      }
    }
    function onPresetClick(preset) {
      var presetValue = preset;
      if (isFunction__default["default"](preset)) presetValue = preset();
      if (!isArray__default["default"](presetValue)) {
        console.error("preset: ".concat(preset, " \u9884\u8BBE\u503C\u5FC5\u987B\u662F\u6570\u7EC4!"));
      } else {
        onChange === null || onChange === void 0 ? void 0 : onChange(_common_js_datePicker_format.formatDate(presetValue, {
          format: formatRef.value.format,
          targetFormat: formatRef.value.valueType,
          autoSwap: true
        }), {
          dayjsValue: presetValue.map(function (p) {
            return _common_js_datePicker_format.parseToDayjs(p, formatRef.value.format);
          }),
          trigger: "preset"
        });
        popupVisible.value = false;
      }
    }
    function onYearChange(nextVal, _ref4) {
      var partial = _ref4.partial;
      var partialIndex = partial === "start" ? 0 : 1;
      if (props2.enableTimePicker) partialIndex = activeIndex.value;
      var nextYear = _toConsumableArray__default["default"](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) {
      var partial = _ref5.partial;
      var partialIndex = partial === "start" ? 0 : 1;
      if (props2.enableTimePicker) partialIndex = activeIndex.value;
      var nextMonth = _toConsumableArray__default["default"](month.value);
      nextMonth[partialIndex] = nextVal;
      if (year.value[0] === year.value[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;
    }
    var panelProps = vue.computed(function () {
      return {
        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,
        popupVisible: popupVisible.value,
        panelPreselection: props2.panelPreselection,
        onCellClick: onCellClick,
        onCellMouseEnter: onCellMouseEnter,
        onCellMouseLeave: onCellMouseLeave,
        onJumperClick: onJumperClick,
        onConfirmClick: onConfirmClick,
        onPresetClick: onPresetClick,
        onYearChange: onYearChange,
        onMonthChange: onMonthChange,
        onTimePickerChange: onTimePickerChange
      };
    });
    return function () {
      return vue.createVNode("div", {
        "class": COMPONENT_NAME.value
      }, [vue.createVNode(rangeInput_index.RangeInputPopup, {
        "disabled": disabled.value,
        "status": props2.status,
        "tips": props2.tips || slots.tips,
        "inputValue": inputValue.value,
        "popupProps": popupProps.value,
        "rangeInputProps": rangeInputProps.value,
        "popupVisible": popupVisible.value,
        "panel": function panel() {
          return vue.createVNode(datePicker_panel_RangePanel["default"], panelProps.value, null);
        }
      }, null)]);
    };
  }
});

exports["default"] = _DateRangePicker;
//# sourceMappingURL=DateRangePicker.js.map
