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

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { ref, computed, createVNode, watch } from 'vue';
import { CalendarIcon } from '@ldesign/icons-vue-next';
import { useTNodeJSX } from '../../hooks/tnode.js';
import { useGlobalIcon } from '../../hooks/useGlobalIcon.js';
import { usePrefixClass } from '../../hooks/useConfig.js';
import { getDefaultFormat, formatDate, isValidDate, parseToDayjs } from '../../_common/js/date-picker/format.js';
import useRangeValue from './useRangeValue.js';
import { useConfig } from '../../config-provider/useConfig.js';
import 'lodash/isFunction';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../utils/render-tnode.js';
import 'lodash/isEmpty';
import 'lodash/isString';
import 'lodash/isObject';
import 'lodash/cloneDeep';
import '../../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import 'lodash/isArray';
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-e041f5b0.js';
import 'dayjs';
import '../../_common/js/date-picker/utils.js';
import '@babel/runtime/helpers/slicedToArray';
import '@babel/runtime/helpers/objectDestructuringEmpty';
import '@babel/runtime/helpers/extends';
import '../../_chunks/dep-e86e320e.js';
import 'lodash/chunk';
import '../../_common/js/log/log.js';
import '../../hooks/useVModel.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 PARTIAL_MAP = {
  first: "start",
  second: "end"
};
function useRange(props) {
  var COMPONENT_NAME = usePrefixClass("date-range-picker");
  var _useConfig = useConfig("datePicker"),
    globalConfig = _useConfig.globalConfig;
  var _useGlobalIcon = useGlobalIcon({
      CalendarIcon: CalendarIcon
    }),
    CalendarIcon$1 = _useGlobalIcon.CalendarIcon;
  var renderTNodeJSX = useTNodeJSX();
  var isMountedRef = ref(false);
  var inputRef = ref();
  var _useRangeValue = useRangeValue(props),
    value = _useRangeValue.value,
    onChange = _useRangeValue.onChange,
    time = _useRangeValue.time,
    month = _useRangeValue.month,
    year = _useRangeValue.year,
    cacheValue = _useRangeValue.cacheValue,
    isFirstValueSelected = _useRangeValue.isFirstValueSelected;
  var formatRef = computed(function () {
    return getDefaultFormat({
      mode: props.mode,
      format: props.format,
      valueType: props.valueType,
      enableTimePicker: props.enableTimePicker
    });
  });
  var popupVisible = ref(false);
  var isHoverCell = ref(false);
  var activeIndex = ref(0);
  var inputValue = ref(formatDate(props.value, {
    format: formatRef.value.format
  }));
  var rangeInputProps = computed(function () {
    return _objectSpread(_objectSpread({}, props.rangeInputProps), {}, {
      size: props.size,
      ref: inputRef,
      clearable: props.clearable,
      prefixIcon: function prefixIcon() {
        return renderTNodeJSX("prefixIcon");
      },
      readonly: !props.allowInput,
      separator: props.separator || globalConfig.value.rangeSeparator,
      placeholder: props.placeholder || globalConfig.value.placeholder[props.mode],
      activeIndex: popupVisible.value ? activeIndex.value : void 0,
      suffixIcon: function suffixIcon() {
        return renderTNodeJSX("suffixIcon") || createVNode(CalendarIcon$1, null, null);
      },
      "class": _defineProperty({}, "".concat(COMPONENT_NAME.value, "__input--placeholder"), isHoverCell.value),
      onClick: function onClick(_ref) {
        var position = _ref.position;
        activeIndex.value = position === "first" ? 0 : 1;
      },
      onClear: function onClear(_ref2) {
        var e = _ref2.e;
        e.stopPropagation();
        popupVisible.value = false;
        onChange === null || onChange === void 0 ? void 0 : onChange([], {
          dayjsValue: [],
          trigger: "clear"
        });
      },
      onBlur: function onBlur(newVal, _ref3) {
        var _props$onBlur;
        var e = _ref3.e,
          position = _ref3.position;
        (_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 ? void 0 : _props$onBlur.call(props, {
          value: newVal,
          partial: PARTIAL_MAP[position],
          e: e
        });
      },
      onFocus: function onFocus(newVal, _ref4) {
        var _props$onFocus;
        var e = _ref4.e,
          position = _ref4.position;
        (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 ? void 0 : _props$onFocus.call(props, {
          value: newVal,
          partial: PARTIAL_MAP[position],
          e: e
        });
        activeIndex.value = position === "first" ? 0 : 1;
      },
      onChange: function onChange(newVal, _ref5) {
        _ref5.e;
          _ref5.position;
        inputValue.value = newVal;
        if (!isValidDate(newVal, formatRef.value.format)) return;
        cacheValue.value = newVal;
        var newYear = [];
        var newMonth = [];
        var newTime = [];
        newVal.forEach(function (v) {
          newYear.push(parseToDayjs(v, formatRef.value.format).year());
          newMonth.push(parseToDayjs(v, formatRef.value.format).month());
          newTime.push(parseToDayjs(v, formatRef.value.format).format(formatRef.value.timeFormat));
        });
        year.value = newYear;
        month.value = newMonth;
        time.value = newTime;
      },
      onEnter: function onEnter(newVal) {
        if (!isValidDate(newVal, formatRef.value.format) && !isValidDate(value.value, formatRef.value.format)) return;
        popupVisible.value = false;
        if (isValidDate(newVal, formatRef.value.format)) {
          onChange === null || onChange === void 0 ? void 0 : onChange(formatDate(newVal, {
            format: formatRef.value.format,
            targetFormat: formatRef.value.valueType,
            autoSwap: true
          }), {
            dayjsValue: newVal.map(function (v) {
              return parseToDayjs(v, formatRef.value.format);
            }),
            trigger: "enter"
          });
        } else if (isValidDate(value.value, formatRef.value.format)) {
          inputValue.value = formatDate(value.value, {
            format: formatRef.value.format
          });
        } else {
          inputValue.value = [];
        }
      }
    });
  });
  var popupProps = computed(function () {
    var _props$popupProps$ove, _props$popupProps, _props$popupProps2;
    return _objectSpread(_objectSpread({
      expandAnimation: true
    }, props.popupProps), {}, {
      overlayInnerStyle: (_props$popupProps$ove = (_props$popupProps = props.popupProps) === null || _props$popupProps === void 0 ? void 0 : _props$popupProps.overlayInnerStyle) !== null && _props$popupProps$ove !== void 0 ? _props$popupProps$ove : {
        width: "auto"
      },
      overlayClassName: [(_props$popupProps2 = props.popupProps) === null || _props$popupProps2 === void 0 ? void 0 : _props$popupProps2.overlayClassName, "".concat(COMPONENT_NAME.value, "__panel-container")],
      onVisibleChange: function onVisibleChange(visible, context) {
        var _props$popupProps3, _props$popupProps3$on;
        (_props$popupProps3 = props.popupProps) === null || _props$popupProps3 === void 0 ? void 0 : (_props$popupProps3$on = _props$popupProps3.onVisibleChange) === null || _props$popupProps3$on === void 0 ? void 0 : _props$popupProps3$on.call(_props$popupProps3, visible, context);
        if (context.trigger === "trigger-element-click") {
          var indexMap = {
            0: "first",
            1: "second"
          };
          inputRef.value.focus({
            position: indexMap[activeIndex.value]
          });
          popupVisible.value = true;
          return;
        }
        popupVisible.value = visible;
      }
    });
  });
  watch(value, function (value2) {
    if (!value2) {
      inputValue.value = [];
      return;
    }
    if (!isValidDate(value2, formatRef.value.format)) return;
    inputValue.value = formatDate(value2, {
      format: formatRef.value.format
    });
  }, {
    immediate: true
  });
  watch(activeIndex, function (index) {
    var _inputRef$value, _inputRef$value$focus;
    if (!isMountedRef.value) {
      isMountedRef.value = true;
      return;
    }
    var indexMap = {
      0: "first",
      1: "second"
    };
    (_inputRef$value = inputRef.value) === null || _inputRef$value === void 0 ? void 0 : (_inputRef$value$focus = _inputRef$value.focus) === null || _inputRef$value$focus === void 0 ? void 0 : _inputRef$value$focus.call(_inputRef$value, {
      position: indexMap[index]
    });
  }, {
    immediate: true
  });
  return {
    year: year,
    month: month,
    value: value,
    time: time,
    inputValue: inputValue,
    popupVisible: popupVisible,
    rangeInputProps: rangeInputProps,
    popupProps: popupProps,
    isHoverCell: isHoverCell,
    activeIndex: activeIndex,
    isFirstValueSelected: isFirstValueSelected,
    cacheValue: cacheValue,
    onChange: onChange
  };
}

export { PARTIAL_MAP, useRange as default };
//# sourceMappingURL=useRange.js.map
