/**
 * ldesign v0.2.3
 * (c) 2022年9月24日 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 TTimePickerPanel from './panel/time-picker-panel.js';
import { TimeRangePickerProps } from './props.js';
import useVModel from '../../hooks/use-vmodel.js';
import { usePrefixClass } from '../../hooks/use-config.js';
import { useGlobalIcon } from '../../hooks/use-global-icon.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-be98bc0e.js';
import '../../chunks/_commonjsHelpers-8b4be15a.js';
import './panel/props.js';
import '../range-input/range-input.js';
import '../input/index.js';
import '../input/input.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../chunks/helper2.js';
import '../../chunks/defineProperty-56324511.js';
import '../../utils/helper.js';
import '../config-provider/config-receiver.js';
import 'vue';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../../utils/mixins.js';
import '../../utils/event.js';
import '../input/props.js';
import '../../utils/render-node.js';
import '../input/group.js';
import '../../utils/map-props.js';
import '../../utils/with-install.js';
import '../../config.js';
import '../input/style';
import '../input/type.js';
import '../range-input/props.js';
import '../../hooks/lnode.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import '../../hooks/render-lnode.js';
import 'lodash/isString';
import 'lodash/isObject';
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 '../../utils/easing.js';
import '../popup/props.js';
import '../popup/container.js';
import '../popup/style';
import '../popup/type.js';
import '../select-input/hooks/use-overlay-innerstyle.js';
import '../range-input/style';
import '../range-input/type.js';
import '../../chunks/index.js';
import '../../chunks/log.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import '../../utils/transfer-dom.js';
import '../loading/props.js';
import '../loading/style';
import '../loading/type.js';
import '../loading/plugin.js';
import '../button/props.js';
import '../../utils/ripple.js';

dayjs.extend(customParseFormat);
var _TimeRangePicker = defineComponent({
  name: "LTimeRangePicker",
  components: {
    LTimePickerPanel: TTimePickerPanel,
    LRangeInputPopup: RangeInputPopup
  },
  props: { ...TimeRangePickerProps,
    rangeInputProps: Object,
    popupProps: Object
  },

  setup(props2) {
    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"
      });
    };

    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"
      });
    };

    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,
      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"
            },
            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,
                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
