"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _moment = _interopRequireDefault(require("moment"));

var _noop = _interopRequireDefault(require("lodash/noop"));

var _util = require("../util");

function getNow() {
  return (0, _moment["default"])();
}

function getNowByCurrentStateValue(value) {
  var ret;

  if (value) {
    ret = (0, _util.getTodayTime)(value);
  } else {
    ret = getNow();
  }

  return ret;
}

var CalendarMixin = {
  getDefaultProps: function getDefaultProps() {
    return {
      onKeyDown: _noop["default"]
    };
  },
  getInitialState: function getInitialState() {
    var props = this.props;
    var value = props.value || props.defaultValue || getNow();
    return {
      value: value,
      selectedValue: props.selectedValue || props.defaultSelectedValue
    };
  },
  componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
    var value = nextProps.value;
    var selectedValue = nextProps.selectedValue;

    if ('value' in nextProps) {
      value = value || nextProps.defaultValue || getNowByCurrentStateValue(this.state.value);
      this.setState({
        value: value
      });
    }

    if ('selectedValue' in nextProps) {
      this.setState({
        selectedValue: selectedValue
      });
    }
  },
  onSelect: function onSelect(value, cause) {
    if (value) {
      this.setValue(value);
    }

    this.setSelectedValue(value, cause);
  },
  renderRoot: function renderRoot(newProps) {
    var _className;

    var props = this.props;
    var prefixCls = props.prefixCls;
    var className = (_className = {}, (0, _defineProperty2["default"])(_className, prefixCls, 1), (0, _defineProperty2["default"])(_className, "".concat(prefixCls, "-hidden"), !props.visible), (0, _defineProperty2["default"])(_className, props.className, !!props.className), (0, _defineProperty2["default"])(_className, newProps.className, !!newProps.className), _className);
    return /*#__PURE__*/_react["default"].createElement("div", {
      ref: this.saveRoot,
      className: "".concat((0, _classnames["default"])(className)),
      style: this.props.style,
      tabIndex: "0",
      onKeyDown: this.onKeyDown
    }, newProps.children);
  },
  setSelectedValue: function setSelectedValue(selectedValue, cause) {
    // if (this.isAllowedDate(selectedValue)) {
    if (!('selectedValue' in this.props)) {
      this.setState({
        selectedValue: selectedValue
      });
    }

    this.props.onSelect(selectedValue, cause); // }
  },
  setValue: function setValue(value) {
    var originalValue = this.state.value;

    if (!('value' in this.props)) {
      this.setState({
        value: value
      });
    }

    if (originalValue && value && !originalValue.isSame(value) || !originalValue && value || originalValue && !value) {
      this.props.onChange(value);
    }
  },
  isAllowedDate: function isAllowedDate(value) {
    var disabledDate = this.props.disabledDate;
    var disabledTime = this.props.disabledTime;
    return (0, _util.isAllowedDate)(value, disabledDate, disabledTime);
  }
};
var _default = CalendarMixin;
exports["default"] = _default;
//# sourceMappingURL=CalendarMixin.js.map
