/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

import { ref, computed, watchEffect } from '@vue/composition-api';
import dayjs from 'dayjs';
import { g as getDefaultFormat, f as formatDate, i as isValidDate, p as parseToDayjs } from '../../../chunks/format.js';
import { usePrefixClass } from '../../../hooks/useConfig.js';
import useRangeValue from './use-range-value.js';
import { useConfig } from '../../config-provider/useConfig.js';
import '../../../chunks/dayjs.min-0f6442fd.js';
import '../../../chunks/_commonjsHelpers-fe796db6.js';
import '../../../chunks/log.js';
import '../../../chunks/utils.js';
import '../../../chunks/defineProperty-19da3d4c.js';
import '../../../chunks/_isIndex-8b06cfbb.js';
import '../../../chunks/isFunction-cb08cfdf.js';
import '../../../chunks/isObjectLike-51896b77.js';
import '../../../chunks/isSymbol-0ea4cedf.js';
import '../../config-provider/context.js';
import 'lodash/merge';
import 'lodash/mergeWith';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../../hooks/useVModel.js';
import 'lodash/kebabCase';

const PARTIAL_MAP = {
  first: "start",
  second: "end"
};
function useRange(props, _ref) {
  let {
    emit
  } = _ref;
  const COMPONENT_NAME = usePrefixClass("date-range-picker");
  const {
    global
  } = useConfig("datePicker");
  const isMountedRef = ref(false);
  const inputRef = ref();
  const {
    value,
    onChange,
    time,
    month,
    year,
    cacheValue,
    isFirstValueSelected
  } = useRangeValue(props);
  const formatRef = computed(() => getDefaultFormat({
    mode: props.mode,
    format: props.format,
    valueType: props.valueType,
    enableTimePicker: props.enableTimePicker
  }));
  const popupVisible = ref(false);
  const isHoverCell = ref(false);
  const activeIndex = ref(0);
  const inputValue = ref(formatDate(value.value, {
    format: formatRef.value.format
  }));
  const rangeInputProps = computed(() => ({
    ...props.rangeInputProps,
    ref: inputRef,
    clearable: props.clearable,
    prefixIcon: props.prefixIcon,
    readonly: !props.allowInput,
    separator: props.separator || global.value.rangeSeparator,
    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?.([], {
        dayjsValue: [],
        trigger: "clear"
      });
      emit("clear", [], {
        dayjsValue: [],
        trigger: "clear"
      });
    },
    onBlur: (newVal, _ref4) => {
      let {
        e,
        position
      } = _ref4;
      props.onBlur?.({
        value: newVal,
        partial: PARTIAL_MAP[position],
        e
      });
      emit("blur", {
        value: newVal,
        partial: PARTIAL_MAP[position],
        e
      });
    },
    onFocus: (newVal, _ref5) => {
      let {
        e,
        position
      } = _ref5;
      props.onFocus?.({
        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 (!isValidDate(newVal, formatRef.value.format)) return;
      const newYear = [];
      const newMonth = [];
      const newTime = [];
      newVal.forEach((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: newVal => {
      if (!isValidDate(newVal, formatRef.value.format) && !isValidDate(value.value, formatRef.value.format)) return;
      popupVisible.value = false;
      if (isValidDate(newVal, formatRef.value.format)) {
        onChange?.(formatDate(newVal, {
          format: formatRef.value.format,
          targetFormat: formatRef.value.valueType,
          autoSwap: true
        }), {
          dayjsValue: newVal.map(v => 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 = [];
      }
    }
  }));
  const popupProps = computed(() => ({
    expandAnimation: true,
    ...props.popupProps,
    overlayInnerStyle: props.popupProps?.overlayInnerStyle ?? {
      width: "auto"
    },
    overlayClassName: [props.popupProps?.overlayClassName, `${COMPONENT_NAME.value}__panel-container`],
    onVisibleChange: (visible, context) => {
      props.popupProps?.onVisibleChange?.(visible, context);
      if (context.trigger === "trigger-element-click") {
        const indexMap = {
          0: "first",
          1: "second"
        };
        inputRef.value?.focus?.({
          position: indexMap[activeIndex.value]
        });
        popupVisible.value = true;
        return;
      }
      popupVisible.value = visible;
    }
  }));
  watchEffect(() => {
    if (!value.value) {
      inputValue.value = [];
      return;
    }
    if (!isValidDate(value.value, formatRef.value.format)) return;
    inputValue.value = formatDate(value.value, {
      format: formatRef.value.format
    });
  });
  watchEffect(() => {
    if (!isMountedRef.value) {
      isMountedRef.value = true;
      return;
    }
    const indexMap = {
      0: "first",
      1: "second"
    };
    inputRef.value?.focus?.({
      position: indexMap[activeIndex.value]
    });
  });
  return {
    year,
    month,
    value,
    time,
    inputValue,
    popupVisible,
    rangeInputProps,
    popupProps,
    isHoverCell,
    activeIndex,
    isFirstValueSelected,
    cacheValue,
    onChange
  };
}

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