"use strict";

exports.__esModule = true;
exports.default = void 0;

require("core-js/modules/es6.object.assign");

require("core-js/modules/web.dom.iterable");

require("core-js/modules/es6.array.iterator");

require("core-js/modules/es6.object.keys");

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _DatePickerMonth = _interopRequireDefault(require("../DatePickerMonth"));

var _ = require("../..");

var _dateUtils = require("../../utils/dateUtils");

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

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }

var DatePickerCalendar =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(DatePickerCalendar, _React$Component);

  function DatePickerCalendar() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _this.state = {
      date: null
    };

    _this.setDate = function (date, cb) {
      _this.setState({
        date: date
      }, cb);
    };

    _this.increaseMonth = function (event) {
      var handleMonthChange = _this.context.handleMonthChange;
      var date = _this.state.date;

      _this.setDate((0, _dateUtils.addMonths)(date.clone(), 1), function () {
        return handleMonthChange && handleMonthChange(event, _this.state.date);
      });
    };

    _this.decreaseMonth = function (event) {
      var handleMonthChange = _this.context.handleMonthChange;
      var date = _this.state.date;

      _this.setDate((0, _dateUtils.subtractMonths)(date.clone(), 1), function () {
        return handleMonthChange && handleMonthChange(event, _this.state.date);
      });
    };

    return _this;
  }

  var _proto = DatePickerCalendar.prototype;

  _proto.componentDidMount = function componentDidMount() {
    var _this$context = this.context,
        focus = _this$context.focus,
        selected = _this$context.selected;
    this.setDate(focus || selected || (0, _dateUtils.now)());
  };

  _proto.componentDidUpdate = function componentDidUpdate(prevProps, prevState, prevContext) {
    var focus = this.context.focus;

    if (focus && !(0, _dateUtils.isSameDay)(prevContext.focus, focus)) {
      this.setDate(focus);
    }
  };

  _proto.render = function render() {
    var _this2 = this;

    var date = this.state.date;

    var _this$props = this.props,
        locale = _this$props.locale,
        monthFormat = _this$props.monthFormat,
        nextArialLabel = _this$props.nextArialLabel,
        previousArialLabel = _this$props.previousArialLabel,
        otherProps = _objectWithoutPropertiesLoose(_this$props, ["locale", "monthFormat", "nextArialLabel", "previousArialLabel"]);

    var renderMonthName = function renderMonthName() {
      return _react.default.createElement("div", {
        className: "cui-datepicker__navigation--current-month"
      }, (0, _dateUtils.localizeDate)(date, locale).format(monthFormat));
    };

    var renderPreviousMonthButton = function renderPreviousMonthButton() {
      var minDate = _this2.props.minDate;
      var allPrevDaysDisabled = (0, _dateUtils.shouldPrevMonthDisable)(date, minDate);
      return _react.default.createElement(_.Icon, {
        ariaLabel: previousArialLabel,
        disabled: allPrevDaysDisabled,
        onClick: _this2.decreaseMonth,
        name: "arrow-left_16"
      });
    };

    var renderNextMonthButton = function renderNextMonthButton() {
      var maxDate = _this2.props.maxDate;
      var allNextDaysDisabled = (0, _dateUtils.shouldNextMonthDisable)(date, maxDate);
      return _react.default.createElement(_.Icon, {
        ariaLabel: nextArialLabel,
        disabled: allNextDaysDisabled,
        onClick: _this2.increaseMonth,
        name: "arrow-right_16"
      });
    };

    var header = function header() {
      var startOfWeek = (0, _dateUtils.getStartOfWeek)(date.clone());
      var dayNames = [];
      return dayNames.concat([0, 1, 2, 3, 4, 5, 6].map(function (offset) {
        var day = (0, _dateUtils.addDays)((0, _dateUtils.localizeDate)(startOfWeek, locale), offset);
        var localeData = (0, _dateUtils.getLocaleData)(day);
        var weekDayName = (0, _dateUtils.getWeekdayMinInLocale)(localeData, day);
        return _react.default.createElement("div", {
          key: offset,
          className: "cui-datepicker__day--name"
        }, weekDayName);
      }));
    };

    var renderMonth = function renderMonth() {
      return _react.default.createElement("div", {
        className: "cui-datepicker__month-container"
      }, _react.default.createElement("div", {
        className: "cui-datepicker__header"
      }, _react.default.createElement("div", {
        className: "cui-datepicker__navigation"
      }, renderMonthName(), _react.default.createElement("div", {
        className: "cui-datepicker__navigation--buttons"
      }, renderPreviousMonthButton(), renderNextMonthButton())), _react.default.createElement("div", {
        className: "cui-datepicker__day--names"
      }, header())), _react.default.createElement(_DatePickerMonth.default, _extends({
        day: date
      }, otherProps)));
    };

    return _react.default.createElement("div", null, date && renderMonth());
  };

  return DatePickerCalendar;
}(_react.default.Component);

DatePickerCalendar.displayName = 'DatePickerCalendar';
DatePickerCalendar.contextTypes = {
  focus: _propTypes.default.instanceOf(_moment.default),
  handleMonthChange: _propTypes.default.func,
  selected: _propTypes.default.instanceOf(_moment.default)
};
DatePickerCalendar.propTypes = {
  /** @prop Sets the language for the DatePickerCalendar | 'en' */
  locale: _propTypes.default.string,

  /** @prop Sets the last date in which the calendar does not disable | null */
  maxDate: _propTypes.default.instanceOf(Date),

  /** @prop Sets the first date in which the calendar does not disable | null */
  minDate: _propTypes.default.instanceOf(Date),

  /** @prop Sets the format in how the Month is displayed | null */
  monthFormat: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.array]),

  /** @prop Text to display for blindness accessibility features | 'next' */
  nextArialLabel: _propTypes.default.string,

  /** @prop Text to display for blindness accessibility features | 'previous' */
  previousArialLabel: _propTypes.default.string
};
DatePickerCalendar.defaultProps = {
  locale: 'en',
  maxDate: null,
  minDate: null,
  monthFormat: 'MMMM YYYY',
  nextArialLabel: 'next',
  previousArialLabel: 'previous'
};
var _default = DatePickerCalendar;
exports.default = _default;