/**
 * 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 isFunction from 'lodash/isFunction';
import { c as customParseFormat } from '../_chunks/dep-f43c4264.js';
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 './props.js';
import { 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 '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/index.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 _TimePicker = mixins(getConfigReceiverMixins("timePicker")).extend({
  name: "TTimePicker",
  components: {
    PickerPanel: PickerPanel,
    TimeIcon: TimeIcon,
    TPopup: Popup,
    TInput: Input,
    InputItems: InputItems
  },
  model: {
    prop: "value",
    event: "change"
  },
  props: _objectSpread({}, props),
  data: function data() {
    var _this$$props = this.$props,
        defaultValue = _this$$props.defaultValue,
        value = _this$$props.value;
    var time = value || defaultValue;
    return {
      formDisabled: void 0,
      els: [],
      focus: false,
      isShowPanel: false,
      time: time ? dayjs(time, this.format) : void 0,
      inputTime: time ? this.setInputValue(dayjs(time, this.format)) : void 0,
      needClear: false
    };
  },
  computed: {
    tDisabled: function tDisabled() {
      return this.formDisabled || this.disabled;
    },
    panelValue: function panelValue() {
      var time = this.$data.time;

      if (time) {
        return [dayjs(time, this.format)];
      }

      if (this.steps.filter(function (step) {
        return step !== 1;
      }).length < 1) {
        return [dayjs()];
      }

      return [dayjs().hour(0).minute(0).second(0)];
    },
    textClassName: function textClassName() {
      var _this$inputTime, _this$inputTime2, _this$inputTime3;

      var isDefault = !!((_this$inputTime = this.inputTime) !== null && _this$inputTime !== void 0 && _this$inputTime.hour) && !!((_this$inputTime2 = this.inputTime) !== null && _this$inputTime2 !== void 0 && _this$inputTime2.minute) && !!((_this$inputTime3 = this.inputTime) !== null && _this$inputTime3 !== void 0 && _this$inputTime3.second);
      return isDefault ? "" : "".concat(name, "__group-text");
    }
  },
  watch: {
    value: {
      handler: function handler() {
        this.time = this.value ? dayjs(this.value, this.format) : void 0;
        this.inputTime = this.value ? this.setInputValue(dayjs(this.value, this.format)) : void 0;
      }
    }
  },
  methods: {
    inputChange: function inputChange(event) {
      var type = event.type,
          value = event.value;
      var time = this.$data.time;
      var newTime = time;

      if (value === EMPTY_VALUE) {
        this.inputTime[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 = dayjs(newTime);
      this.inputTime = this.setInputValue(this.time);
      this.$emit("input", {
        input: value,
        value: this.time.format(this.format),
        e: event
      });
      var panelRef = this.$refs.panel;
      panelRef.panelColUpdate();
    },
    onBlurDefault: function onBlurDefault(e, trigger, index, input) {
      this.$emit("blur", {
        trigger: trigger,
        input: input,
        value: this.time.format(this.format),
        e: e
      });
    },
    onFocusDefault: function onFocusDefault(e, trigger, index, input) {
      this.$emit("focus", {
        trigger: trigger,
        input: input,
        value: this.time.format(this.format),
        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() {
      var time = this.$data.time;
      var current = time.format("A");
      var currentHour = time.hour() + (current === AM ? 12 : -12);
      this.inputChange({
        type: "hour",
        value: currentHour
      });
    },
    pickTime: function pickTime(col, change, index, value) {
      var time = this.time,
          format = this.format;
      var setTime = time;

      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) {
          currentHour -= 12;
        } else if (change === this.global.postMeridiem && currentHour < 12) {
          currentHour += 12;
        }

        setTime = value.hour(currentHour);
      }

      this.time = setTime;
      this.inputTime = this.setInputValue(setTime);
      var formatValue = dayjs(setTime).format(this.format);
      this.$emit("change", formatValue);
    },
    makeSure: function makeSure(e) {
      this.panelVisibleChange(false, {
        e: e
      });
      this.output();
    },
    nowAction: function nowAction() {
      var currentTime = dayjs();

      if (isFunction(this.disableTime) && this.disableTime(currentTime.get("hour"), currentTime.get("minute"), currentTime.get("second"))) {
        return;
      }

      this.time = currentTime;
      this.inputTime = this.setInputValue(this.time);
      this.$emit("change", currentTime.format(this.format));
    },
    output: function output() {
      if (this.needClear) {
        this.inputTime = this.setInputValue(void 0);
        this.needClear = false;
      } else {
        var _this$time;

        this.time = (_this$time = this.time) !== null && _this$time !== void 0 ? _this$time : dayjs();
        this.inputTime = this.setInputValue(this.time);
      }
    },
    setInputValue: function setInputValue(val) {
      var ans = {
        hour: void 0,
        minute: void 0,
        second: void 0,
        meridiem: AM
      };
      if (!val) return ans;
      return this.dayjs2InputTime(val);
    },
    dayjs2InputTime: function dayjs2InputTime(val) {
      var format = this.$props.format;

      if (!val) {
        return {
          hour: void 0,
          minute: void 0,
          second: void 0,
          meridiem: AM
        };
      }

      var hour = val.hour();
      var minute = val.minute();
      var second = val.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;
      }

      return {
        hour: hour,
        minute: minute,
        second: second,
        meridiem: val.format("a")
      };
    },
    clear: function clear(context) {
      var e = context.e;
      this.time = void 0;
      this.needClear = true;
      this.inputTime = this.setInputValue(void 0);
      this.$emit("change", void 0);
      e.stopPropagation();
    },
    handleTInputFocus: function handleTInputFocus() {
      var _this = this;

      this.$nextTick(function () {
        _this.$refs.tInput.blur();
      });
    },
    renderInput: function renderInput() {
      var _this2 = 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": this.time ? " " : void 0
        },
        "on": {
          "clear": this.clear,
          "focus": this.handleTInputFocus
        },
        "ref": "tInput"
      }, [h("time-icon", {
        "slot": "suffix-icon"
      })]), h("input-items", {
        "attrs": {
          "size": this.size,
          "dayjs": this.inputTime,
          "disabled": this.tDisabled,
          "format": this.format,
          "steps": this.steps,
          "allowInput": this.allowInput,
          "placeholder": this.placeholder || this.global.placeholder
        },
        "on": {
          "toggleMeridiem": function toggleMeridiem() {
            return _this2.toggleInputMeridiem();
          },
          "blurDefault": this.onBlurDefault,
          "focusDefault": this.onFocusDefault,
          "change": function change(e) {
            return _this2.inputChange(e);
          }
        }
      })]);
    }
  },
  render: function render() {
    var h = arguments[0];
    var _this$$props2 = this.$props,
        size = _this$$props2.size,
        className = _this$$props2.className,
        tDisabled = _this$$props2.tDisabled;
    var classes = [name, ClASSNAMES.SIZE[size] || "", className];
    return h("t-popup", helper([{
      "ref": "popup",
      "attrs": {
        "placement": "bottom-left",
        "trigger": "click",
        "disabled": tDisabled,
        "visible": this.isShowPanel,
        "overlayClassName": "".concat(componentName, "__panel-container")
      },
      "class": classes
    }, {
      "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,
        "now-action": this.nowAction
      }
    })])]);
  }
});

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