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

'use strict';

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

var VueCompositionAPI = require('@vue/composition-api');
var dayjs = require('dayjs');
var format = require('../../../chunks/format.js');
var hooks_useConfig = require('../../../hooks/use-config.js');
var components_datePicker_hooks_useRangeValue = require('./use-range-value.js');
var components_configProvider_useConfig = require('../../config-provider/useConfig.js');
require('../../../chunks/dayjs.min-c4f4ce86.js');
require('../../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../../chunks/log.js');
require('../../../chunks/utils.js');
require('../../../chunks/defineProperty-838e4192.js');
require('../../../chunks/_isIndex-52887d1e.js');
require('../../../chunks/isFunction-91acade7.js');
require('../../../chunks/isObjectLike-c22c7e3b.js');
require('../../../chunks/isSymbol-a38a5184.js');
require('../../config-provider/context.js');
require('lodash');
require('../../../chunks/zh_CN.js');
require('../../../hooks/use-vmodel.js');
require('lodash/kebabCase');

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

var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);

const PARTIAL_MAP = {
  first: "start",
  second: "end"
};
function useRange(props, _ref) {
  let {
    emit
  } = _ref;
  const COMPONENT_NAME = hooks_useConfig.usePrefixClass("date-range-picker");
  const {
    global
  } = components_configProvider_useConfig.useConfig("datePicker");
  const isMountedRef = VueCompositionAPI.ref(false);
  const inputRef = VueCompositionAPI.ref();
  const {
    value,
    onChange,
    time,
    month,
    year,
    cacheValue,
    isFirstValueSelected
  } = components_datePicker_hooks_useRangeValue["default"](props);
  const formatRef = VueCompositionAPI.computed(() => format.getDefaultFormat({
    mode: props.mode,
    format: props.format,
    valueType: props.valueType,
    enableTimePicker: props.enableTimePicker
  }));
  const popupVisible = VueCompositionAPI.ref(false);
  const isHoverCell = VueCompositionAPI.ref(false);
  const activeIndex = VueCompositionAPI.ref(0);
  const inputValue = VueCompositionAPI.ref(format.formatDate(props.value, {
    format: formatRef.value.format
  }));
  const rangeInputProps = VueCompositionAPI.computed(() => ({
    ...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: {
      [`${COMPONENT_NAME.value}__input--placeholder`]: isHoverCell.value
    },
    onClick: _ref2 => {
      let {
        position
      } = _ref2;
      activeIndex.value = position === "first" ? 0 : 1;
    },
    onClear: _ref3 => {
      let {
        e
      } = _ref3;
      e.stopPropagation();
      popupVisible.value = false;
      onChange === null || onChange === void 0 ? void 0 : onChange([], {
        dayjsValue: [],
        trigger: "clear"
      });
      emit("clear", [], {
        dayjsValue: [],
        trigger: "clear"
      });
    },
    onBlur: (newVal, _ref4) => {
      var _props$onBlur;
      let {
        e,
        position
      } = _ref4;
      (_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 ? void 0 : _props$onBlur.call(props, {
        value: newVal,
        partial: PARTIAL_MAP[position],
        e
      });
      emit("blur", {
        value: newVal,
        partial: PARTIAL_MAP[position],
        e
      });
    },
    onFocus: (newVal, _ref5) => {
      var _props$onFocus;
      let {
        e,
        position
      } = _ref5;
      (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 ? void 0 : _props$onFocus.call(props, {
        value: newVal,
        partial: PARTIAL_MAP[position],
        e
      });
      emit("focus", {
        value: newVal,
        partial: PARTIAL_MAP[position],
        e
      });
      activeIndex.value = position === "first" ? 0 : 1;
    },
    onChange: newVal => {
      inputValue.value = newVal;
      if (!format.isValidDate(newVal, formatRef.value.format)) return;
      const newYear = [];
      const newMonth = [];
      const newTime = [];
      newVal.forEach((v, i) => {
        newYear.push(dayjs__default["default"](v).year() || year.value[i]);
        newMonth.push(dayjs__default["default"](v).month() || month.value[i]);
        newTime.push(dayjs__default["default"](v).format(formatRef.value.timeFormat) || time.value[i]);
      });
      year.value = newYear;
      month.value = newMonth;
      time.value = newTime;
    },
    onEnter: newVal => {
      if (!format.isValidDate(newVal, formatRef.value.format) && !format.isValidDate(value.value, formatRef.value.format)) return;
      popupVisible.value = false;
      if (format.isValidDate(newVal, formatRef.value.format)) {
        onChange === null || onChange === void 0 ? void 0 : onChange(format.formatDate(newVal, {
          format: formatRef.value.format,
          targetFormat: formatRef.value.valueType,
          autoSwap: true
        }), {
          dayjsValue: newVal.map(v => format.parseToDayjs(v, formatRef.value.format)),
          trigger: "enter"
        });
      } else if (format.isValidDate(value.value, formatRef.value.format)) {
        inputValue.value = format.formatDate(value.value, {
          format: formatRef.value.format
        });
      } else {
        inputValue.value = [];
      }
    }
  }));
  const popupProps = VueCompositionAPI.computed(() => {
    var _props$popupProps, _props$popupProps2;
    return {
      expandAnimation: true,
      ...props.popupProps,
      overlayInnerStyle: ((_props$popupProps = props.popupProps) === null || _props$popupProps === void 0 ? void 0 : _props$popupProps.overlayInnerStyle) ?? {
        width: "auto"
      },
      overlayClassName: [(_props$popupProps2 = props.popupProps) === null || _props$popupProps2 === void 0 ? void 0 : _props$popupProps2.overlayClassName, `${COMPONENT_NAME.value}__panel-container`],
      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 _inputRef$value, _inputRef$value$focus;
          const 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;
        }
        popupVisible.value = visible;
      }
    };
  });
  VueCompositionAPI.watchEffect(() => {
    if (!value.value) {
      inputValue.value = [];
      return;
    }
    if (!format.isValidDate(value.value, formatRef.value.format)) return;
    inputValue.value = format.formatDate(value.value, {
      format: formatRef.value.format
    });
  });
  VueCompositionAPI.watchEffect(() => {
    var _inputRef$value2, _inputRef$value2$focu;
    if (!isMountedRef.value) {
      isMountedRef.value = true;
      return;
    }
    const 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,
    month,
    value,
    time,
    inputValue,
    popupVisible,
    rangeInputProps,
    popupProps,
    isHoverCell,
    activeIndex,
    isFirstValueSelected,
    cacheValue,
    onChange
  };
}

exports.PARTIAL_MAP = PARTIAL_MAP;
exports["default"] = useRange;
//# sourceMappingURL=use-range.js.map
