/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
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-2e5ef949.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/dep-a66683a6.js';
import '../../_chunks/dep-d3027203.js';
import '@babel/runtime/helpers/typeof';
import '../../_chunks/dep-53d4956d.js';
import './panel/props.js';
import '../range-input/range-input.js';
import '../input/index.js';
import '../input/input.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../_chunks/dep-fc383775.js';
import '../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '../config-provider/config-receiver.js';
import 'vue';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../_chunks/dep-a0f2090c.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 '../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/dep-85e8b23b.js';
import '../../utils/dom.js';
import 'raf';
import '../../utils/easing.js';
import '../popup/props.js';
import '../popup/container.js';
import '../select-input/hooks/use-overlay-innerstyle.js';
import '../../_chunks/dep-a65fe6e9.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import '../../_chunks/dep-33b15d9d.js';
import '../../utils/transfer-dom.js';
import '../loading/props.js';
import '../loading/plugin.js';
import '../button/props.js';
import '../../utils/ripple.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
dayjs.extend(customParseFormat);
var _TimeRangePicker = defineComponent({
  name: "LTimeRangePicker",
  components: {
    LTimePickerPanel: TTimePickerPanel,
    LRangeInputPopup: RangeInputPopup
  },
  props: _objectSpread(_objectSpread({}, TimeRangePickerProps), {}, {
    rangeInputProps: Object,
    popupProps: Object
  }),
  setup: function setup(props2) {
    var componentName = usePrefixClass("time-range-picker");

    var _useConfig = useConfig("timePicker"),
        global = _useConfig.global;

    var _useConfig2 = useConfig("classPrefix"),
        classPrefix = _useConfig2.classPrefix;

    var _useGlobalIcon = useGlobalIcon({
      TimeIcon: TimeIcon
    }),
        TimeIcon$1 = _useGlobalIcon.TimeIcon;

    var currentPanelIdx = ref(void 0);
    var currentValue = ref(TIME_PICKER_EMPTY);
    var isShowPanel = ref(false);
    var inputClasses = computed(function () {
      return ["".concat(componentName.value, "__group"), _defineProperty({}, "".concat(classPrefix.value, "-is-focused"), isShowPanel.value)];
    });

    var _toRefs = toRefs(props2),
        value = _toRefs.value,
        format = _toRefs.format;

    var _useVModel = useVModel(value, props2.defaultValue, props2.onChange, "change"),
        _useVModel2 = _slicedToArray(_useVModel, 2),
        innerValue = _useVModel2[0],
        setInnerValue = _useVModel2[1];

    var handleShowPopup = function handleShowPopup(visible, context) {
      if (context.trigger === "trigger-element-click") {
        isShowPanel.value = true;
        return;
      }

      isShowPanel.value = visible;
    };

    var handleClear = function handleClear(context) {
      var e = context.e;
      e.stopPropagation();
      currentValue.value = TIME_PICKER_EMPTY;
      setInnerValue(null);
    };

    var handleClick = function handleClick(_ref2) {
      var position = _ref2.position;
      currentPanelIdx.value = position === "first" ? 0 : 1;
    };

    var handleTimeChange = function handleTimeChange(newValue) {
      if (currentPanelIdx.value === 0) {
        var _currentValue$value$;

        currentValue.value = [newValue, (_currentValue$value$ = currentValue.value[1]) !== null && _currentValue$value$ !== void 0 ? _currentValue$value$ : newValue];
      } else {
        var _currentValue$value$2;

        currentValue.value = [(_currentValue$value$2 = currentValue.value[0]) !== null && _currentValue$value$2 !== void 0 ? _currentValue$value$2 : newValue, newValue];
      }
    };

    var handleInputBlur = function handleInputBlur(value2, _ref3) {
      var _props2$onBlur;

      var e = _ref3.e;

      if (props2.allowInput) {
        var isValidTime = validateInputValue(currentValue.value[currentPanelIdx.value], format.value);

        if (isValidTime) {
          var _currentValue$value$3, _currentValue$value$4;

          var formattedVal = formatInputValue(currentValue.value[currentPanelIdx.value], format.value);
          currentPanelIdx.value === 0 ? currentValue.value = [formattedVal, (_currentValue$value$3 = currentValue.value[1]) !== null && _currentValue$value$3 !== void 0 ? _currentValue$value$3 : formattedVal] : currentValue.value = [(_currentValue$value$4 = currentValue.value[0]) !== null && _currentValue$value$4 !== void 0 ? _currentValue$value$4 : formattedVal, formattedVal];
        }
      }

      (_props2$onBlur = props2.onBlur) === null || _props2$onBlur === void 0 ? void 0 : _props2$onBlur.call(props2, {
        value: value2,
        e: e
      });
    };

    var handleInputChange = function handleInputChange(inputVal, _ref4) {
      var _props2$onInput;

      var e = _ref4.e,
          position = _ref4.position;
      currentValue.value = inputVal;
      (_props2$onInput = props2.onInput) === null || _props2$onInput === void 0 ? void 0 : _props2$onInput.call(props2, {
        value: innerValue.value,
        e: e,
        position: position === "first" ? "start" : "end"
      });
    };

    var handleClickConfirm = function handleClickConfirm() {
      var isValidTime = !currentValue.value.find(function (v) {
        return !validateInputValue(v, format.value);
      });
      if (isValidTime) setInnerValue(currentValue.value);
      isShowPanel.value = false;
    };

    var handleFocus = function handleFocus(value2, _ref5) {
      var _props2$onFocus;

      var e = _ref5.e,
          position = _ref5.position;
      (_props2$onFocus = props2.onFocus) === null || _props2$onFocus === void 0 ? void 0 : _props2$onFocus.call(props2, {
        value: value2,
        e: e,
        position: position === "first" ? "start" : "end"
      });
    };

    watch(function () {
      return isShowPanel.value;
    }, function () {
      var _innerValue$value;

      currentValue.value = isShowPanel.value ? (_innerValue$value = innerValue.value) !== null && _innerValue$value !== void 0 ? _innerValue$value : TIME_PICKER_EMPTY : TIME_PICKER_EMPTY;
      if (!isShowPanel.value) currentPanelIdx.value = void 0;
    });
    return {
      global: global,
      componentName: componentName,
      currentValue: currentValue,
      currentPanelIdx: currentPanelIdx,
      inputClasses: inputClasses,
      innerValue: innerValue,
      isShowPanel: isShowPanel,
      handleInputChange: handleInputChange,
      handleShowPopup: handleShowPopup,
      handleClear: handleClear,
      handleFocus: handleFocus,
      handleClickConfirm: handleClickConfirm,
      handleClick: handleClick,
      handleInputBlur: handleInputBlur,
      handleTimeChange: handleTimeChange,
      TimeIcon: TimeIcon$1
    };
  },
  render: function render() {
    var _this$innerValue,
        _this = this;

    var h = arguments[0];
    var TimeIcon = this.TimeIcon;
    return h("div", {
      "class": this.componentName
    }, [h("l-range-input-popup", {
      "props": _objectSpread({}, {
        onInputChange: this.handleInputChange,
        disabled: this.disabled,
        popupVisible: this.isShowPanel,
        inputValue: this.isShowPanel ? this.currentValue : (_this$innerValue = this.innerValue) !== null && _this$innerValue !== void 0 ? _this$innerValue : TIME_PICKER_EMPTY,
        popupProps: _objectSpread({
          overlayInnerStyle: {
            width: "auto"
          },
          onVisibleChange: this.handleShowPopup
        }, this.popupProps),
        rangeInputProps: _objectSpread({
          size: this.size,
          clearable: this.clearable,
          "class": this.inputClasses,
          placeholder: this.placeholder || [this.global.placeholder, this.global.placeholder],
          suffixIcon: function suffixIcon() {
            return h(TimeIcon);
          },
          onClear: this.handleClear,
          onClick: this.handleClick,
          onFocus: this.handleFocus,
          onBlur: this.handleInputBlur,
          readonly: !this.allowInput,
          activeIndex: this.currentPanelIdx
        }, this.rangeInputProps),
        panel: function panel() {
          return h("l-time-picker-panel", {
            "props": _objectSpread({}, {
              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
