/**
 * 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 components_timePicker_panel_singlePanel = require('../../chunks/single-panel.js');
var iconsVue = require('@ldesign/icons-vue');
var components_rangeInput_index = require('../range-input/index.js');
var hooks_useVmodel = require('../../hooks/use-vmodel.js');
var hooks_useConfig = require('../../hooks/use-config.js');
var hooks_useGlobalIcon = require('../../hooks/use-global-icon.js');
var components_timePicker_panel_timePickerPanel = require('./panel/time-picker-panel.js');
var components_timePicker_props = require('./props.js');
var components_configProvider_useConfig = require('../config-provider/useConfig.js');
require('lodash/debounce');
require('lodash/range');
require('lodash/padStart');
require('../../chunks/_commonjsHelpers.js');
require('../../chunks/dayjs.min-c4f4ce86.js');
require('../../chunks/_commonjsHelpers-2e6815bd.js');
require('./panel/props.js');
require('../../utils/with-install.js');
require('vue');
require('lodash');
require('../../config.js');
require('../range-input/range-input.js');
require('../input/index.js');
require('../../utils/map-props.js');
require('../input/input.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../chunks/helper2.js');
require('../../chunks/log.js');
require('../../utils/helper.js');
require('../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('../../chunks/zh_CN.js');
require('../../utils/mixins.js');
require('../../utils/event.js');
require('../../utils/render-node.js');
require('../input/props.js');
require('../input/group.js');
require('../../hooks/lnode.js');
require('lodash/isEmpty');
require('lodash/isFunction');
require('../../hooks/render-lnode.js');
require('../range-input/props.js');
require('../range-input/range-input-popup.js');
require('../popup/index.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../../chunks/set-style.js');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('../popup/props.js');
require('../popup/container.js');
require('../select-input/hooks/use-overlay-innerstyle.js');
require('lodash/isObject');
require('../button/button.js');
require('../loading/index.js');
require('../loading/loading.js');
require('../../utils/transfer-dom.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../../chunks/defineProperty-838e4192.js');
require('../loading/plugin.js');
require('../../utils/ripple.js');
require('../button/props.js');

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

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

dayjs__default["default"].extend(components_timePicker_panel_singlePanel.customParseFormat);
var _TimeRangePicker = VueCompositionAPI.defineComponent({
  name: "LTimeRangePicker",
  components: {
    LTimePickerPanel: components_timePicker_panel_timePickerPanel["default"],
    LRangeInputPopup: components_rangeInput_index.RangeInputPopup
  },
  props: {
    ...components_timePicker_props.TimeRangePickerProps,
    rangeInputProps: Object,
    popupProps: Object
  },
  setup(props2, ctx) {
    const componentName = hooks_useConfig.usePrefixClass("time-range-picker");
    const {
      global
    } = components_configProvider_useConfig.useConfig("timePicker");
    const {
      classPrefix
    } = components_configProvider_useConfig.useConfig("classPrefix");
    const {
      TimeIcon
    } = hooks_useGlobalIcon.useGlobalIcon({
      TimeIcon: iconsVue.TimeIcon
    });
    const currentPanelIdx = VueCompositionAPI.ref(void 0);
    const currentValue = VueCompositionAPI.ref(components_timePicker_panel_singlePanel.TIME_PICKER_EMPTY);
    const isShowPanel = VueCompositionAPI.ref(false);
    const inputClasses = VueCompositionAPI.computed(() => [`${componentName.value}__group`, {
      [`${classPrefix.value}-is-focused`]: isShowPanel.value
    }]);
    const {
      value,
      format
    } = VueCompositionAPI.toRefs(props2);
    const [innerValue, setInnerValue] = hooks_useVmodel["default"](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 = components_timePicker_panel_singlePanel.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 = components_timePicker_panel_singlePanel.validateInputValue(currentValue.value[currentPanelIdx.value], format.value);
        if (isValidTime) {
          const formattedVal = components_timePicker_panel_singlePanel.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 => !components_timePicker_panel_singlePanel.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);
    };
    VueCompositionAPI.watch(() => isShowPanel.value, () => {
      currentValue.value = isShowPanel.value ? innerValue.value ?? components_timePicker_panel_singlePanel.TIME_PICKER_EMPTY : components_timePicker_panel_singlePanel.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
    };
  },
  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 ?? components_timePicker_panel_singlePanel.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
              }
            }
          })
        }
      }
    })]);
  }
});

exports["default"] = _TimeRangePicker;
//# sourceMappingURL=time-range-picker.js.map
