/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { ref, computed, watchEffect } from '@vue/composition-api';
import dayjs from 'dayjs';
import { usePrefixClass } from '../../hooks/useConfig.js';
import { getDefaultFormat, formatDate, isValidDate } from '../../_common/js/date-picker/format.js';
import useRangeValue from './useRangeValue.js';
import { useConfig } from '../../config-provider/useConfig.js';
import '../../config-provider/context.js';
import 'lodash/merge';
import 'lodash/mergeWith';
import '../../_common/js/global-config/default-config.js';
import '../../_common/js/global-config/locale/zh_CN.js';
import '../../config-provider/type.js';
import '../../_common/js/date-picker/utils.js';
import '@babel/runtime/helpers/slicedToArray';
import '@babel/runtime/helpers/extends';
import '../../_chunks/dep-a0d91f92.js';
import '../../_chunks/dep-b98966c0.js';
import 'lodash/chunk';
import '../../_common/js/log/index.js';
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, _ref) {
  var emit = _ref.emit;
  var COMPONENT_NAME = usePrefixClass("date-range-picker");

  var _useConfig = useConfig("datePicker"),
      global = _useConfig.global;

  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,
    targetFormat: formatRef.value.format
  }));
  var rangeInputProps = computed(function () {
    return _objectSpread(_objectSpread({}, props.rangeInputProps), {}, {
      ref: inputRef,
      clearable: props.clearable,
      prefixIcon: props.prefixIcon,
      readonly: !props.allowInput,
      separator: props.separator,
      placeholder: props.placeholder || global.value.placeholder[props.mode],
      activeIndex: popupVisible.value ? activeIndex.value : void 0,
      "class": _defineProperty({}, "".concat(COMPONENT_NAME.value, "__input--placeholder"), isHoverCell.value),
      onClick: function onClick(_ref2) {
        var position = _ref2.position;
        activeIndex.value = position === "first" ? 0 : 1;
      },
      onClear: function onClear(_ref3) {
        var e = _ref3.e;
        e.stopPropagation();
        popupVisible.value = false;
        onChange === null || onChange === void 0 ? void 0 : onChange([], {
          dayjsValue: [],
          trigger: "clear"
        });
        emit("clear", [], {
          dayjsValue: [],
          trigger: "clear"
        });
      },
      onBlur: function onBlur(newVal, _ref4) {
        var _props$onBlur;

        var e = _ref4.e,
            position = _ref4.position;
        (_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 ? void 0 : _props$onBlur.call(props, {
          value: newVal,
          partial: PARTIAL_MAP[position],
          e: e
        });
        emit("blur", {
          value: newVal,
          partial: PARTIAL_MAP[position],
          e: e
        });
      },
      onFocus: function onFocus(newVal, _ref5) {
        var _props$onFocus;

        var e = _ref5.e,
            position = _ref5.position;
        (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 ? void 0 : _props$onFocus.call(props, {
          value: newVal,
          partial: PARTIAL_MAP[position],
          e: e
        });
        emit("focus", {
          value: newVal,
          partial: PARTIAL_MAP[position],
          e: e
        });
        activeIndex.value = position === "first" ? 0 : 1;
      },
      onChange: function onChange(newVal) {
        inputValue.value = newVal;
        if (!isValidDate(newVal, formatRef.value.format)) return;
        var newYear = [];
        var newMonth = [];
        var newTime = [];
        newVal.forEach(function (v, i) {
          newYear.push(dayjs(v).year() || year.value[i]);
          newMonth.push(dayjs(v).month() || month.value[i]);
          newTime.push(dayjs(v).format(formatRef.value.timeFormat) || time.value[i]);
        });
        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
          }), {
            dayjsValue: newVal.map(function (v) {
              return dayjs(v);
            }),
            trigger: "enter"
          });
        } else if (isValidDate(value.value, formatRef.value.format)) {
          inputValue.value = formatDate(value.value, {
            format: formatRef.value.format,
            targetFormat: 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) {
        if (context.trigger === "trigger-element-click") {
          var _inputRef$value, _inputRef$value$focus;

          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[activeIndex.value]
          });
          popupVisible.value = true;
          return;
        }

        if (!visible) {
          isHoverCell.value = false;
          inputValue.value = formatDate(value.value, {
            format: formatRef.value.format,
            targetFormat: formatRef.value.format
          });
        }

        popupVisible.value = visible;
      }
    });
  });
  watchEffect(function () {
    if (!value.value) {
      inputValue.value = [];
      return;
    }

    if (!isValidDate(value.value, "valueType")) return;
    inputValue.value = formatDate(value.value, {
      format: formatRef.value.format,
      targetFormat: formatRef.value.format
    });
  });
  watchEffect(function () {
    var _inputRef$value2, _inputRef$value2$focu;

    if (!isMountedRef.value) {
      isMountedRef.value = true;
      return;
    }

    var indexMap = {
      0: "first",
      1: "second"
    };
    (_inputRef$value2 = inputRef.value) === null || _inputRef$value2 === void 0 ? void 0 : (_inputRef$value2$focu = _inputRef$value2.focus) === null || _inputRef$value2$focu === void 0 ? void 0 : _inputRef$value2$focu.call(_inputRef$value2, {
      position: indexMap[activeIndex.value]
    });
  });
  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
