import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import classnames from 'classnames';
import moment from 'moment';
import noop from 'lodash/noop';
import { getTodayTime, isAllowedDate as _isAllowedDate } from '../util';

function getNow() {
  return moment();
}

function getNowByCurrentStateValue(value) {
  var ret;

  if (value) {
    ret = getTodayTime(value);
  } else {
    ret = getNow();
  }

  return ret;
}

var CalendarMixin = {
  getDefaultProps: function getDefaultProps() {
    return {
      onKeyDown: noop
    };
  },
  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 = {}, _defineProperty(_className, prefixCls, 1), _defineProperty(_className, "".concat(prefixCls, "-hidden"), !props.visible), _defineProperty(_className, props.className, !!props.className), _defineProperty(_className, newProps.className, !!newProps.className), _className);
    return /*#__PURE__*/React.createElement("div", {
      ref: this.saveRoot,
      className: "".concat(classnames(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 _isAllowedDate(value, disabledDate, disabledTime);
  }
};
export default CalendarMixin;
//# sourceMappingURL=CalendarMixin.js.map
