/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 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/dep-2af9ce5e.js';
import { TimeIcon } from '@ldesign/icons-vue';
import { RangeInputPopup } from '../range-input/index.js';
import useVModel from '../../hooks/use-vmodel.js';
import { usePrefixClass } from '../../hooks/use-config.js';
import { useGlobalIcon } from '../../hooks/use-global-icon.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/dep-74554e14.js';
import '../../_chunks/dep-f38f4c93.js';
import '../../_chunks/dep-5210251b.js';
import './panel/props.js';
import '../../utils/with-install.js';
import 'vue';
import 'lodash';
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/dep-47d77fc8.js';
import '../../_chunks/dep-70c789e5.js';
import '../../utils/helper.js';
import '../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import '../../_chunks/dep-def4c2ce.js';
import '../../utils/mixins.js';
import '../../utils/event.js';
import '../../utils/render-node.js';
import '../input/props.js';
import '../input/group.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/dep-cf772670.js';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import '../popup/props.js';
import '../popup/container.js';
import '../select-input/hooks/use-overlay-innerstyle.js';
import 'lodash/isObject';
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 '../../_chunks/dep-eb467ddf.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) => {
      var _props2$onBlur;
      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 = props2.onBlur) === null || _props2$onBlur === void 0 ? void 0 : _props2$onBlur.call(props2, {
        value: value2,
        e
      });
    };
    const handleInputChange = (inputVal, _ref3) => {
      var _props2$onInput;
      let {
        e,
        position
      } = _ref3;
      currentValue.value = inputVal;
      (_props2$onInput = props2.onInput) === null || _props2$onInput === void 0 ? void 0 : _props2$onInput.call(props2, {
        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) => {
      var _props2$onFocus;
      let {
        e,
        position
      } = _ref4;
      (_props2$onFocus = props2.onFocus) === null || _props2$onFocus === void 0 ? void 0 : _props2$onFocus.call(props2, {
        value: value2,
        e,
        position: position === "first" ? "start" : "end"
      });
      ctx.emit("focus", {
        value: value2,
        e,
        position: position === "first" ? "start" : "end"
      });
    };
    const handleOnPick = pickValue => {
      var _props2$onPick;
      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 = props2.onPick) === null || _props2$onPick === void 0 ? void 0 : _props2$onPick.call(props2, 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
          },
          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
