/**
 * tdesign v0.41.7
 * (c) 2022 tdesign
 * @license MIT
 */

import { h as helper } from '../_chunks/dep-37d5b9c4.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import dayjs from 'dayjs';
import { c as customParseFormat } from '../_chunks/dep-f43c4264.js';
import isFunction from 'lodash/isFunction';
import isEqual from 'lodash/isEqual';
import { TimeIcon } from 'tdesign-icons-vue';
import mixins from '../utils/mixins.js';
import getConfigReceiverMixins from '../config-provider/config-receiver.js';
import { EPickerCols } from './interface.js';
import { Popup } from '../popup/index.js';
import { prefix } from '../config.js';
import ClASSNAMES from '../utils/classnames.js';
import PickerPanel from './panel/index.js';
import { Input } from '../input/index.js';
import InputItems from './input-items.js';
import props from './time-range-picker-props.js';
import { TIME_PICKER_EMPTY, EMPTY_VALUE, AM, AM_FORMAT, PM_Format, componentName } from './constant.js';
import 'vue';
import 'lodash/debounce';
import '../_chunks/dep-8600dc2e.js';
import './panel/props.js';
import './props.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../config-provider/type.js';
import './type.js';
import '../popup/popup.js';
import '@babel/runtime/helpers/typeof';
import '@popperjs/core';
import '../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../utils/easing.js';
import '../utils/render-tnode.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';
import '../_common/js/utils/set-style.js';
import '../popup/props.js';
import '../popup/container.js';
import '../utils/map-props.js';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import './style/css.js';
import '../popup/type.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../_common/js/utils/helper.js';
import '../utils/transfer-dom.js';
import '../loading/props.js';
import '../loading/type.js';
import '../loading/plugin.js';
import '../button/props.js';
import '../utils/ripple.js';
import '../input/addon.js';
import '../input/input.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'lodash/kebabCase';
import '../utils/event.js';
import '../input/props.js';
import '../input/input-group.js';
import '../input/type.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; }
var name = "".concat(prefix, "-time-picker");
dayjs.extend(customParseFormat);
var _TimeRangePicker = mixins(getConfigReceiverMixins("timePicker")).extend({
  name: "TTimeRangePicker",
  components: {
    PickerPanel: PickerPanel,
    TimeIcon: TimeIcon,
    InputItems: InputItems,
    TPopup: Popup,
    TInput: Input
  },
  props: _objectSpread({}, props),
  data: function data() {
    return {
      formDisabled: void 0,
      els: [],
      focus: false,
      isShowPanel: false,
      time: TIME_PICKER_EMPTY,
      inputTime: TIME_PICKER_EMPTY
    };
  },
  computed: {
    tDisabled: function tDisabled() {
      return this.formDisabled || this.disabled;
    },
    panelValue: function panelValue() {
      var time = this.time || TIME_PICKER_EMPTY;
      return time.map(function (val) {
        return val ? dayjs(val) : dayjs();
      });
    },
    textClassName: function textClassName() {
      var isDefault = this.inputTime.some(function (item) {
        return !!item.hour && !!item.minute && !!item.second;
      });
      return isDefault ? "" : "".concat(name, "__group-text");
    }
  },
  watch: {
    value: {
      handler: function handler(val, oldVal) {
        if (JSON.stringify(val) === JSON.stringify(oldVal)) return;
        var values = Array.isArray(this.value) ? this.value : [];
        var format = this.format;

        function getVal(value) {
          return value ? dayjs(value, format) : void 0;
        }

        var dayjsList = [getVal(values[0]), getVal(values[1])];
        this.time = dayjsList;
        this.updateInputTime();
      },
      immediate: true
    }
  },
  methods: {
    inputChange: function inputChange(event) {
      var type = event.type,
          value = event.value,
          index = event.index;
      var newTime = this.time[index];

      if (value === EMPTY_VALUE) {
        this.inputTime[index][type] = void 0;
        newTime[type](0);
        return;
      }

      if (!newTime) {
        newTime = dayjs();
        newTime.hour(0);
        newTime.minute(0);
        newTime.second(0);
      }

      newTime = newTime.set(type, value);
      this.time[index] = dayjs(newTime);
      this.updateInputTime();
      this.$emit("input", {
        input: value,
        value: this.time[index].format(this.format),
        e: event
      });
      var panelRef = this.$refs.panel;
      panelRef.panelColUpdate();
    },
    getFormatValues: function getFormatValues() {
      var _this = this;

      var values = [];
      this.time.forEach(function (time) {
        if (time) {
          values.push(time.format(_this.format));
        }
      });
      return values;
    },
    onBlurDefault: function onBlurDefault(e) {
      var value = this.getFormatValues();
      this.$emit("blur", {
        value: value,
        e: e
      });
    },
    onFocusDefault: function onFocusDefault(e) {
      var value = this.getFormatValues();
      this.$emit("focus", {
        value: value,
        e: e
      });
    },
    panelVisibleChange: function panelVisibleChange(val, context) {
      if (this.tDisabled) return;

      if (context.trigger) {
        var isClickDoc = context.trigger === "document";
        this.isShowPanel = !isClickDoc;
        this.$emit(isClickDoc ? "close" : "open", context);
      } else {
        this.isShowPanel = val;
        this.$emit(val ? "open" : "close", context);
      }
    },
    toggleInputMeridiem: function toggleInputMeridiem(index) {
      var curTime = this.time[index];
      var current = curTime.format("a");
      var currentHour = curTime.hour() + (current === AM ? 12 : -12);
      this.inputChange({
        type: "hour",
        value: currentHour,
        index: index
      });
    },
    pickTime: function pickTime(col, change, index, value) {
      var time = this.time,
          format = this.format;
      var panelRef = this.$refs.panel;
      var shouldUpdatePanel = false;
      var setTime = time[index];

      if (EPickerCols.hour === col) {
        setTime = value.set(col, value.hour() >= 12 && (AM_FORMAT.test(format) || PM_Format.test(format)) ? Number(change) + 12 : change);
      } else if ([EPickerCols.minute, EPickerCols.second].includes(col)) {
        setTime = value.set(col, change);
      } else {
        var currentHour = value.hour();

        if (change === this.global.anteMeridiem) {
          currentHour -= 12;
        } else if (change === this.global.postMeridiem) {
          currentHour += 12;
        }

        setTime = value.hour(currentHour);
      }

      this.time[index] = setTime;

      if (index === 0 && !this.time[1]) {
        this.time[1] = setTime;
        shouldUpdatePanel = true;
      } else if (index === 1 && !this.time[0]) {
        this.time[0] = dayjs().hour(0).minute(0).second(0);
        shouldUpdatePanel = true;
      }

      this.updateInputTime();
      shouldUpdatePanel && panelRef.panelColUpdate();
    },
    makeSure: function makeSure(e) {
      this.panelVisibleChange(false, {
        e: e
      });
    },
    updateInputTime: function updateInputTime() {
      var format = this.$props.format;
      var disPlayValues = [];
      (this.time || []).forEach(function (time) {
        if (!time) {
          disPlayValues.push({
            hour: void 0,
            minute: void 0,
            second: void 0,
            meridiem: AM
          });
        } else {
          var hour = time.hour();
          var minute = time.minute();
          var second = time.second();

          if (/[h]{1}/.test(format)) {
            hour %= 12;
          }

          if (/[h|H]{2}/.test(format)) {
            hour = hour < 10 ? "0".concat(hour) : hour;
          }

          if (/[m|M]{2}/.test(format)) {
            minute = minute < 10 ? "0".concat(minute) : minute;
          }

          if (/[s|S]{2}/.test(format)) {
            second = second < 10 ? "0".concat(second) : second;
          }

          disPlayValues.push({
            hour: hour,
            minute: minute,
            second: second,
            meridiem: time.format("a")
          });
        }
      });
      this.inputTime = disPlayValues;
      this.triggerUpdateValue();
    },
    clear: function clear(context) {
      var e = context.e;
      this.time = TIME_PICKER_EMPTY;
      this.updateInputTime();
      e.stopPropagation();
    },
    triggerUpdateValue: function triggerUpdateValue() {
      var _this2 = this;

      var values = [];
      this.time.forEach(function (time) {
        if (time) {
          values.push(time.format(_this2.format));
        }
      });
      this.$emit("change", values);
      isFunction(this.onChange) && this.onChange(values);
    },
    handleTInputFocus: function handleTInputFocus() {
      var _this3 = this;

      this.$nextTick(function () {
        _this3.$refs.tInput.blur();
      });
    },
    renderInput: function renderInput() {
      var _this4 = this;

      var h = this.$createElement;
      var classes = ["".concat(name, "__group"), _defineProperty({}, "".concat(prefix, "-is-focused"), this.isShowPanel)];
      return h("div", {
        "class": classes
      }, [h("t-input", {
        "attrs": {
          "disabled": this.tDisabled,
          "size": this.size,
          "clearable": this.clearable,
          "placeholder": " ",
          "value": !isEqual(this.time, TIME_PICKER_EMPTY) ? " " : void 0
        },
        "on": {
          "clear": this.clear
        },
        "ref": "tInput"
      }, [h("time-icon", {
        "slot": "suffix-icon"
      })]), h("input-items", {
        "attrs": {
          "size": this.size,
          "dayjs": this.inputTime,
          "disabled": this.tDisabled,
          "format": this.format,
          "allowInput": this.allowInput,
          "placeholder": this.placeholder || this.global.placeholder,
          "isRangePicker": true,
          "steps": this.steps
        },
        "on": {
          "toggleMeridiem": function toggleMeridiem(index) {
            return _this4.toggleInputMeridiem(index);
          },
          "blurDefault": this.onBlurDefault,
          "focusDefault": this.onFocusDefault,
          "change": function change(e) {
            return _this4.inputChange(e);
          }
        }
      })]);
    }
  },
  render: function render() {
    var h = arguments[0];
    var _this$$props = this.$props,
        size = _this$$props.size,
        className = _this$$props.className,
        tDisabled = _this$$props.tDisabled;
    var classes = [name, ClASSNAMES.SIZE[size], className];
    return h("t-popup", helper([{
      "ref": "popup",
      "class": classes,
      "attrs": {
        "placement": "bottom-left",
        "trigger": "click",
        "disabled": tDisabled,
        "visible": this.isShowPanel,
        "overlayClassName": "".concat(componentName, "__panel-container ").concat(componentName, "__range")
      }
    }, {
      "on": {
        "visible-change": this.panelVisibleChange
      }
    }, {
      "attrs": {
        "expandAnimation": true
      }
    }]), [this.renderInput(), h("template", {
      "slot": "content"
    }, [h("picker-panel", {
      "ref": "panel",
      "attrs": {
        "format": this.format,
        "value": this.panelValue,
        "disabled": this.tDisabled,
        "isShowPanel": this.isShowPanel,
        "steps": this.steps,
        "hideDisabledTime": this.hideDisabledTime,
        "disableTime": this.disableTime,
        "isFocus": this.focus
      },
      "on": {
        "time-pick": this.pickTime,
        "sure": this.makeSure
      }
    })])]);
  }
});

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