/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var helper = require('../_chunks/dep-33ee4bce.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var dayjs = require('dayjs');
var isFunction = require('lodash/isFunction');
var timePicker_panel_panelCol = require('../_chunks/dep-058e10f7.js');
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_props = require('./props.js');
var timePicker_constant = require('./constant.js');
require('vue');
require('lodash/debounce');
require('../_chunks/dep-7ed6a5c6.js');
require('./panel/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('lodash/isObject');
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);

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 _TimePicker = utils_mixins["default"](configProvider_configReceiver["default"]("timePicker")).extend({
  name: "TTimePicker",
  components: {
    PickerPanel: timePicker_panel_index["default"],
    TimeIcon: tdesignIconsVue.TimeIcon,
    TPopup: popup_index.Popup,
    TInput: input_index.Input,
    InputItems: timePicker_inputItems["default"]
  },
  model: {
    prop: "value",
    event: "change"
  },
  props: _objectSpread({}, timePicker_props["default"]),
  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__default["default"](time, this.format) : void 0,
      inputTime: time ? this.setInputValue(dayjs__default["default"](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__default["default"](time, this.format)];
      }

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

      return [dayjs__default["default"]().hour(0).minute(0).second(0)];
    },
    textClassName: function textClassName() {
      var isDefault = this.inputTime.some(function (item) {
        return !!item.hour && !!item.minute && !!item.second;
      });
      return isDefault ? "" : "".concat(name, "__group-text");
    }
  },
  watch: {
    time: {
      handler: function handler() {
        this.output();
      }
    },
    value: {
      handler: function handler() {
        this.time = this.value ? dayjs__default["default"](this.value, this.format) : void 0;
        this.inputTime = this.value ? this.setInputValue(dayjs__default["default"](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 === timePicker_constant.EMPTY_VALUE) {
        this.inputTime[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 = dayjs__default["default"](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 === timePicker_constant.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 (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) {
          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__default["default"](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__default["default"]();

      if (isFunction__default["default"](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__default["default"]();
        this.inputTime = this.setInputValue(this.time);
      }
    },
    setInputValue: function setInputValue(val) {
      var ans = {
        hour: void 0,
        minute: void 0,
        second: void 0,
        meridiem: timePicker_constant.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: timePicker_constant.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() {
      this.$refs.tInput.blur();
    },
    renderInput: function renderInput() {
      var _this = 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": 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 _this.toggleInputMeridiem();
          },
          "blurDefault": this.onBlurDefault,
          "focusDefault": this.onFocusDefault,
          "change": function change(e) {
            return _this.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, utils_classnames["default"].SIZE[size] || "", className];
    return h("t-popup", helper.helper([{
      "ref": "popup",
      "attrs": {
        "placement": "bottom-left",
        "trigger": "click",
        "disabled": tDisabled,
        "visible": this.isShowPanel,
        "overlayClassName": "".concat(timePicker_constant.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
      }
    })])]);
  }
});

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