import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
import memoize from 'memoize-one';
import YearPanel from './panel/YearPanel';
import MonthPanel from './panel/MonthPanel';
import DatePanel from './panel/DatePanel';
import TimePanel from '../timePicker/TimePanel';
import TypeSelect from './TypeSelect';
/**
 * @ignore
 * */

var Calendar =
/*#__PURE__*/
function (_React$PureComponent) {
  _inherits(Calendar, _React$PureComponent);

  _createClass(Calendar, null, [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, state) {
      var newState = null;

      if (props.panelDate !== state.prevPropsPanelDate) {
        newState = {
          panelDate: props.panelDate,
          prevPropsPanelDate: props.panelDate
        };
      }

      if (props.panelType !== state.prevPropsPanelType) {
        newState = newState || {};
        newState.panelType = props.panelType;
        newState.prevPropsPanelType = props.panelType;
      }

      return newState;
    }
  }]);

  function Calendar(props) {
    var _this;

    _classCallCheck(this, Calendar);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Calendar).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "handlePanelDateChange", function (value) {
      var onPanelDateChange = _this.props.onPanelDateChange;

      _this.setState({
        panelDate: value
      });

      onPanelDateChange && onPanelDateChange(value);
    });

    _defineProperty(_assertThisInitialized(_this), "handlePanelTypeChange", function (type) {
      _this.setState({
        panelType: type
      }); // const { onPanelTypeChange } = this.props;
      // onPanelTypeChange && onPanelTypeChange(type);

    });

    _defineProperty(_assertThisInitialized(_this), "handleTimeChange", function (value) {
      var _this$props = _this.props,
          onTimeChange = _this$props.onTimeChange,
          rangeType = _this$props.rangeType;
      onTimeChange(value, rangeType);

      _this.handlePanelDateChange(value);
    });

    _defineProperty(_assertThisInitialized(_this), "conditions", memoize(function (date, type, disabledTime) {
      return disabledTime ? disabledTime(moment(date), type) : {};
    }));

    _defineProperty(_assertThisInitialized(_this), "disabledHoursCondition", function (selectedDate, hour) {
      var _this$props2 = _this.props,
          rangeType = _this$props2.rangeType,
          value = _this$props2.value,
          panelDate = _this$props2.panelDate,
          disabledTime = _this$props2.disabledTime;

      var timeConditions = _this.conditions(panelDate, rangeType, disabledTime);

      if (rangeType === 'start') {
        return value[0] && value[1] && moment(value[0]).hour(hour).isAfter(value[1], 'hour') || timeConditions.disabledHours && timeConditions.disabledHours(selectedDate, hour);
      }

      if (rangeType === 'end') {
        return value[0] && value[1] && value[0].isAfter(moment(value[1]).hour(hour), 'hour') || timeConditions.disabledHours && timeConditions.disabledHours(selectedDate, hour);
      }

      return timeConditions.disabledHours && timeConditions.disabledHours(selectedDate, hour);
    });

    _defineProperty(_assertThisInitialized(_this), "disabledMinutesCondition", function (selectedDate, selectedHour, minute) {
      var _this$props3 = _this.props,
          rangeType = _this$props3.rangeType,
          value = _this$props3.value,
          panelDate = _this$props3.panelDate,
          disabledTime = _this$props3.disabledTime;

      var timeConditions = _this.conditions(panelDate, rangeType, disabledTime);

      if (rangeType === 'start') {
        return value[0] && value[1] && moment(value[0]).minute(minute).isAfter(value[1], 'minute') || timeConditions.disabledMinutes && timeConditions.disabledMinutes(selectedDate, selectedHour, minute);
      }

      if (rangeType === 'end') {
        return value[0] && value[1] && value[0].isAfter(moment(value[1]).minute(minute), 'minute') || timeConditions.disabledMinutes && timeConditions.disabledMinutes(selectedDate, selectedHour, minute);
      }

      return timeConditions.disabledMinutes && timeConditions.disabledMinutes(selectedDate, selectedHour, minute);
    });

    _defineProperty(_assertThisInitialized(_this), "disabledSecondsCondition", function (selectedDate, selectedHour, selectedMinute, second) {
      var _this$props4 = _this.props,
          rangeType = _this$props4.rangeType,
          value = _this$props4.value,
          panelDate = _this$props4.panelDate,
          disabledTime = _this$props4.disabledTime;

      var timeConditions = _this.conditions(panelDate, rangeType, disabledTime);

      if (rangeType === 'start') {
        return value[0] && value[1] && moment(value[0]).second(second).isSameOrAfter(value[1], 'second') || timeConditions.disabledSeconds && timeConditions.disabledSeconds(selectedDate, selectedHour, selectedMinute, second);
      }

      if (rangeType === 'end') {
        return value[0] && value[1] && value[0].isSameOrAfter(moment(value[1]).second(second), 'second') || timeConditions.disabledSeconds && timeConditions.disabledSeconds(selectedDate, selectedHour, selectedMinute, second);
      }

      return timeConditions.disabledSeconds && timeConditions.disabledSeconds(selectedDate, selectedHour, selectedMinute, second);
    });

    var _value = props.value,
        _panelDate = props.panelDate,
        selectType = props.selectType,
        panelType = props.panelType;
    var initialPanelDate = moment(_panelDate || _value);
    _this.state = {
      panelDate: initialPanelDate,
      panelType: panelType || selectType
    };
    return _this;
  }

  _createClass(Calendar, [{
    key: "render",
    value: function render() {
      var _this$props5 = this.props,
          onValueChange = _this$props5.onValueChange,
          value = _this$props5.value,
          selectType = _this$props5.selectType,
          prefixCls = _this$props5.prefixCls,
          disabledDate = _this$props5.disabledDate,
          disabledMonth = _this$props5.disabledMonth,
          disabledYear = _this$props5.disabledYear,
          disabledTime = _this$props5.disabledTime,
          rangeType = _this$props5.rangeType,
          startPanelDate = _this$props5.startPanelDate,
          endPanelDate = _this$props5.endPanelDate;
      var _this$state = this.state,
          panelDate = _this$state.panelDate,
          panelType = _this$state.panelType;
      var panelCommonProps = {
        value: value,
        panelDate: panelDate,
        onValueChange: onValueChange,
        selectType: selectType,
        rangeType: rangeType,
        onPanelTypeChange: this.handlePanelTypeChange,
        onPanelDateChange: this.handlePanelDateChange
      };
      var timeValue = value;

      if (rangeType === 'start') {
        var _value2 = _slicedToArray(value, 1);

        timeValue = _value2[0];
      } else if (rangeType === 'end') {
        var _value3 = _slicedToArray(value, 2);

        timeValue = _value3[1];
      }

      return React.createElement("table", {
        className: "".concat(prefixCls, " ").concat(prefixCls, "-").concat(panelType)
      }, React.createElement("caption", null, React.createElement(TypeSelect, {
        startPanelDate: startPanelDate // forceUpdate
        ,
        endPanelDate: endPanelDate // forceUpdate
        ,
        prefixCls: prefixCls,
        rangeType: rangeType,
        disabledYear: disabledYear,
        disabledMonth: disabledMonth,
        disabledDate: disabledDate,
        panelType: panelType,
        panelDate: panelDate,
        onPanelTypeChange: this.handlePanelTypeChange,
        onPanelDateChange: this.handlePanelDateChange
      })), panelType === 'date' && React.createElement(DatePanel, _extends({}, panelCommonProps, {
        prefixCls: prefixCls,
        disabledDate: disabledDate
      })), panelType === 'month' && React.createElement(MonthPanel, _extends({}, panelCommonProps, {
        disabledMonth: disabledMonth,
        prefixCls: prefixCls
      })), panelType === 'year' && React.createElement(YearPanel, _extends({}, panelCommonProps, {
        disabledYear: disabledYear,
        prefixCls: prefixCls
      })), panelType === 'time' && React.createElement(TimePanel, _extends({}, panelCommonProps, {
        rangeValue: value // forceUpdate
        ,
        value: timeValue,
        onValueChange: this.handleTimeChange,
        disabledTime: disabledTime,
        disabledHours: this.disabledHoursCondition,
        disabledMinutes: this.disabledMinutesCondition,
        disabledSeconds: this.disabledSecondsCondition
      })));
    }
  }]);

  return Calendar;
}(React.PureComponent);

_defineProperty(Calendar, "propTypes", {
  selectType: PropTypes.string,
  rangeSeparator: PropTypes.string
});

_defineProperty(Calendar, "defaultProps", {
  value: moment(),
  selectType: 'date',
  prefixCls: 'ne-calendar'
});

export { Calendar as default };