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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var helper = require('../_chunks/dep-99705d51.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var dayjs = require('dayjs');
var timePicker_panel_panelCol = require('../_chunks/dep-648ca8b7.js');
var isFunction = require('lodash/isFunction');
var isEqual = require('lodash/isEqual');
var tdesignIconsVue = require('tdesign-icons-vue');
var utils_mixins = require('../utils/mixins.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var timePicker_interface = require('./interface.js');
var popup_index = require('../popup/index.js');
var config = require('../config.js');
var utils_classnames = require('../utils/classnames.js');
var timePicker_panel_index = require('./panel/index.js');
var input_index = require('../input/index.js');
var timePicker_inputItems = require('./input-items.js');
var timePicker_timeRangePickerProps = require('./time-range-picker-props.js');
var timePicker_constant = require('./constant.js');
require('vue');
require('lodash/debounce');
require('../_chunks/dep-1641a31f.js');
require('./panel/props.js');
require('./props.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../popup/popup.js');
require('@babel/runtime/helpers/typeof');
require('@popperjs/core');
require('../utils/dom.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('../utils/render-tnode.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');
require('../_common/js/utils/set-style.js');
require('../popup/props.js');
require('../popup/container.js');
require('../utils/map-props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../button/button.js');
require('../loading/index.js');
require('../loading/loading.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../_common/js/utils/helper.js');
require('../utils/transfer-dom.js');
require('../loading/props.js');
require('../loading/plugin.js');
require('../button/props.js');
require('../utils/ripple.js');
require('../input/addon.js');
require('../input/input.js');
require('@babel/runtime/helpers/toConsumableArray');
require('lodash/kebabCase');
require('../utils/event.js');
require('../input/props.js');
require('../input/input-group.js');

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

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);

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__default["default"](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(config.prefix, "-time-picker");
dayjs__default["default"].extend(timePicker_panel_panelCol.customParseFormat);
var _TimeRangePicker = utils_mixins["default"](configProvider_configReceiver["default"]("timePicker")).extend({
  name: "TTimeRangePicker",
  components: {
    PickerPanel: timePicker_panel_index["default"],
    TimeIcon: tdesignIconsVue.TimeIcon,
    InputItems: timePicker_inputItems["default"],
    TPopup: popup_index.Popup,
    TInput: input_index.Input
  },
  props: _objectSpread({}, timePicker_timeRangePickerProps["default"]),
  data: function data() {
    return {
      formDisabled: void 0,
      els: [],
      focus: false,
      isShowPanel: false,
      time: timePicker_constant.TIME_PICKER_EMPTY,
      inputTime: timePicker_constant.TIME_PICKER_EMPTY
    };
  },
  computed: {
    tDisabled: function tDisabled() {
      return this.formDisabled || this.disabled;
    },
    panelValue: function panelValue() {
      var time = this.time || timePicker_constant.TIME_PICKER_EMPTY;
      return time.map(function (val) {
        return val ? dayjs__default["default"](val) : dayjs__default["default"]();
      });
    },
    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__default["default"](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 === timePicker_constant.EMPTY_VALUE) {
        this.inputTime[index][type] = void 0;
        newTime[type](0);
        return;
      }

      if (!newTime) {
        newTime = dayjs__default["default"]();
        newTime.hour(0);
        newTime.minute(0);
        newTime.second(0);
      }

      newTime = newTime.set(type, value);
      this.time[index] = dayjs__default["default"](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 === timePicker_constant.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 (timePicker_interface.EPickerCols.hour === col) {
        setTime = value.set(col, value.hour() >= 12 && (timePicker_constant.AM_FORMAT.test(format) || timePicker_constant.PM_Format.test(format)) ? Number(change) + 12 : change);
      } else if ([timePicker_interface.EPickerCols.minute, timePicker_interface.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__default["default"]().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: timePicker_constant.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 = timePicker_constant.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__default["default"](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__default["default"]({}, "".concat(config.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__default["default"](this.time, timePicker_constant.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, utils_classnames["default"].SIZE[size], className];
    return h("t-popup", helper.helper([{
      "ref": "popup",
      "class": classes,
      "attrs": {
        "placement": "bottom-left",
        "trigger": "click",
        "disabled": tDisabled,
        "visible": this.isShowPanel,
        "overlayClassName": "".concat(timePicker_constant.componentName, "__panel-container ").concat(timePicker_constant.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
      }
    })])]);
  }
});

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