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

import { defineComponent, ref, computed, toRefs, watch } from '@vue/composition-api';
import dayjs from 'dayjs';
import { c as customParseFormat, T as TIME_PICKER_EMPTY, v as validateInputValue, f as formatInputValue } from '../../chunks/single-panel.js';
import { TimeIcon } from '@ldesign/icons-vue';
import { RangeInputPopup } from '../range-input/index.js';
import useVModel from '../../hooks/useVModel.js';
import { usePrefixClass } from '../../hooks/useConfig.js';
import { useGlobalIcon } from '../../hooks/useGlobalIcon.js';
import TTimePickerPanel from './panel/time-picker-panel.js';
import { TimeRangePickerProps } from './props.js';
import { useConfig } from '../config-provider/useConfig.js';
import 'lodash/debounce';
import 'lodash/range';
import 'lodash/padStart';
import '../../chunks/_commonjsHelpers.js';
import '../../chunks/dayjs.min-0f6442fd.js';
import '../../chunks/_commonjsHelpers-fe796db6.js';
import './panel/props.js';
import '../../utils/withInstall.js';
import 'vue';
import 'lodash/capitalize';
import '../../config.js';
import '../range-input/range-input.js';
import '../input/index.js';
import '../../utils/map-props.js';
import '../input/input.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../chunks/helper2.js';
import '../../chunks/defineProperty-19da3d4c.js';
import '../../chunks/index.js';
import '../../chunks/log.js';
import '../../utils/helper.js';
import '../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../../utils/mixins.js';
import '../../utils/event.js';
import '../../utils/render-node.js';
import 'lodash';
import '../input/props.js';
import '../input/group.js';
import '../input/style/css.js';
import '../input/type.js';
import '../../hooks/lnode.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import '../../hooks/render-lnode.js';
import '../range-input/props.js';
import '../range-input/range-input-popup.js';
import '../popup/index.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../../chunks/set-style.js';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import '../popup/props.js';
import '../popup/container.js';
import '../popup/style/css.js';
import '../popup/type.js';
import '../select-input/hooks/use-overlay-innerstyle.js';
import 'lodash/isObject';
import '../range-input/style/css.js';
import '../range-input/type.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/loading.js';
import '../../utils/transfer-dom.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../loading/style/css.js';
import '../loading/type.js';
import '../loading/plugin.js';
import '../../utils/ripple.js';
import '../button/props.js';

dayjs.extend(customParseFormat);
var _TimeRangePicker = defineComponent({
  name: "LTimeRangePicker",
  components: {
    LTimePickerPanel: TTimePickerPanel,
    LRangeInputPopup: RangeInputPopup
  },
  props: {
    ...TimeRangePickerProps,
    rangeInputProps: Object,
    popupProps: Object
  },
  setup(props2, ctx) {
    const componentName = usePrefixClass("time-range-picker");
    const {
      global
    } = useConfig("timePicker");
    const {
      classPrefix
    } = useConfig("classPrefix");
    const {
      TimeIcon: TimeIcon$1
    } = useGlobalIcon({
      TimeIcon: TimeIcon
    });
    const currentPanelIdx = ref(void 0);
    const currentValue = ref(TIME_PICKER_EMPTY);
    const isShowPanel = ref(false);
    const inputClasses = computed(() => [`${componentName.value}__group`, {
      [`${classPrefix.value}-is-focused`]: isShowPanel.value
    }]);
    const {
      value,
      format
    } = toRefs(props2);
    const [innerValue, setInnerValue] = useVModel(value, props2.defaultValue, props2.onChange, "change");
    const handleShowPopup = (visible, context) => {
      if (context.trigger === "trigger-element-click") {
        isShowPanel.value = true;
        return;
      }
      isShowPanel.value = visible;
    };
    const handleClear = context => {
      const {
        e
      } = context;
      e.stopPropagation();
      currentValue.value = TIME_PICKER_EMPTY;
      setInnerValue(null);
    };
    const handleClick = _ref => {
      let {
        position
      } = _ref;
      currentPanelIdx.value = position === "first" ? 0 : 1;
    };
    const handleTimeChange = newValue => {
      if (currentPanelIdx.value === 0) currentValue.value = [newValue, currentValue.value[1] ?? newValue];else currentValue.value = [currentValue.value[0] ?? newValue, newValue];
    };
    const handleInputBlur = (value2, _ref2) => {
      let {
        e
      } = _ref2;
      if (props2.allowInput) {
        const isValidTime = validateInputValue(currentValue.value[currentPanelIdx.value], format.value);
        if (isValidTime) {
          const formattedVal = formatInputValue(currentValue.value[currentPanelIdx.value], format.value);
          currentPanelIdx.value === 0 ? currentValue.value = [formattedVal, currentValue.value[1] ?? formattedVal] : currentValue.value = [currentValue.value[0] ?? formattedVal, formattedVal];
        }
      }
      props2.onBlur?.({
        value: value2,
        e
      });
    };
    const handleInputChange = (inputVal, _ref3) => {
      let {
        e,
        position
      } = _ref3;
      currentValue.value = inputVal;
      props2.onInput?.({
        value: innerValue.value,
        e,
        position: position === "first" ? "start" : "end"
      });
      ctx.emit("input", {
        value: innerValue.value,
        e,
        position: position === "first" ? "start" : "end"
      });
    };
    const handleClickConfirm = () => {
      const isValidTime = !currentValue.value.find(v => !validateInputValue(v, format.value));
      if (isValidTime) setInnerValue(currentValue.value);
      isShowPanel.value = false;
    };
    const handleFocus = (value2, _ref4) => {
      let {
        e,
        position
      } = _ref4;
      props2.onFocus?.({
        value: value2,
        e,
        position: position === "first" ? "start" : "end"
      });
      ctx.emit("focus", {
        value: value2,
        e,
        position: position === "first" ? "start" : "end"
      });
    };
    const handleOnPick = pickValue => {
      let pickedRangeValue = [];
      let context = {};
      if (currentPanelIdx.value === 0) {
        pickedRangeValue = [pickValue, currentValue.value[1] ?? pickValue];
        context = {
          position: "start"
        };
      } else {
        pickedRangeValue = [currentValue.value[0] ?? pickValue, pickValue];
        context = {
          position: "end"
        };
      }
      props2.onPick?.(pickedRangeValue, context);
      ctx.emit("pick", pickedRangeValue, context);
    };
    watch(() => isShowPanel.value, () => {
      currentValue.value = isShowPanel.value ? innerValue.value ?? TIME_PICKER_EMPTY : TIME_PICKER_EMPTY;
      if (!isShowPanel.value) currentPanelIdx.value = void 0;
    });
    return {
      global,
      componentName,
      currentValue,
      currentPanelIdx,
      inputClasses,
      innerValue,
      isShowPanel,
      handleInputChange,
      handleShowPopup,
      handleClear,
      handleFocus,
      handleOnPick,
      handleClickConfirm,
      handleClick,
      handleInputBlur,
      handleTimeChange,
      TimeIcon: TimeIcon$1
    };
  },
  render() {
    const h = arguments[0];
    const {
      TimeIcon
    } = this;
    return h("div", {
      "class": this.componentName
    }, [h("l-range-input-popup", {
      "props": {
        ...{
          onInputChange: this.handleInputChange,
          disabled: this.disabled,
          popupVisible: this.isShowPanel,
          inputValue: this.isShowPanel ? this.currentValue : this.innerValue ?? TIME_PICKER_EMPTY,
          popupProps: {
            overlayInnerStyle: {
              width: "auto",
              padding: 0
            },
            onVisibleChange: this.handleShowPopup,
            ...this.popupProps
          },
          status: this.status,
          tips: this.tips,
          rangeInputProps: {
            size: this.size,
            clearable: this.clearable,
            class: this.inputClasses,
            placeholder: this.placeholder || [this.global.placeholder, this.global.placeholder],
            suffixIcon: () => h(TimeIcon),
            onClear: this.handleClear,
            onClick: this.handleClick,
            onFocus: this.handleFocus,
            onBlur: this.handleInputBlur,
            readonly: !this.allowInput,
            activeIndex: this.currentPanelIdx,
            ...this.rangeInputProps
          },
          panel: () => h("l-time-picker-panel", {
            "props": {
              ...{
                steps: this.steps,
                format: this.format,
                isShowPanel: this.isShowPanel,
                disableTime: this.disableTime,
                hideDisabledTime: this.hideDisabledTime,
                isFooterDisplay: true,
                value: this.currentValue[this.currentPanelIdx || 0],
                onChange: this.handleTimeChange,
                onPick: this.handleOnPick,
                handleConfirmClick: this.handleClickConfirm,
                position: this.currentPanelIdx === 0 ? "start" : "end",
                activeIndex: this.currentPanelIdx,
                presets: this.presets
              }
            }
          })
        }
      }
    })]);
  }
});

export { _TimeRangePicker as default };
//# sourceMappingURL=time-range-picker.js.map
