"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

var React = _interopRequireWildcard(require("react"));

var _formControl = require("../form-control");

var _locale = require("../locale");

var _select = require("../select");

var _calendarHeader = _interopRequireDefault(require("./calendar-header"));

var _month = _interopRequireDefault(require("./month"));

var _timepicker = _interopRequireDefault(require("../timepicker/timepicker"));

var _styledComponents = require("./styled-components");

var _dateFnsAdapter = _interopRequireDefault(require("./utils/date-fns-adapter"));

var _dateHelpers = _interopRequireDefault(require("./utils/date-helpers"));

var _overrides = require("../helpers/overrides");

var _constants = require("./constants");

var _button = require("../button");

var _buttonDock = require("../button-dock");

var _excluded = ["overrides"];

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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

  var _super = _createSuper(Calendar);

  // @ts-ignore
  function Calendar(props) {
    var _this;

    _classCallCheck(this, Calendar);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "dateHelpers", void 0);

    _defineProperty(_assertThisInitialized(_this), "calendar", void 0);

    _defineProperty(_assertThisInitialized(_this), "getDateInView", function () {
      var _this$props = _this.props,
          highlightedDate = _this$props.highlightedDate,
          value = _this$props.value; // @ts-ignore

      var minDate = _this.dateHelpers.getEffectiveMinDate(_this.props); // @ts-ignore


      var maxDate = _this.dateHelpers.getEffectiveMaxDate(_this.props);

      var current = _this.dateHelpers.date();

      var initialDate = _this.getSingleDate(value) || highlightedDate;

      if (initialDate) {
        return initialDate;
      } else {
        if (minDate && _this.dateHelpers.isBefore(current, minDate)) {
          return minDate;
        } else if (maxDate && _this.dateHelpers.isAfter(current, maxDate)) {
          return maxDate;
        }
      }

      return current;
    });

    _defineProperty(_assertThisInitialized(_this), "handleMonthChange", function (date) {
      _this.setHighlightedDate(_this.dateHelpers.getStartOfMonth(date));

      if (_this.props.onMonthChange) {
        _this.props.onMonthChange({
          date: date
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleYearChange", function (date) {
      _this.setHighlightedDate(date);

      if (_this.props.onYearChange) {
        _this.props.onYearChange({
          date: date
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "changeMonth", function (_ref) {
      var date = _ref.date;

      _this.setState({
        date: date
      }, function () {
        return _this.handleMonthChange(_this.state.date);
      });
    });

    _defineProperty(_assertThisInitialized(_this), "changeYear", function (_ref2) {
      var date = _ref2.date;

      _this.setState({
        date: date
      }, function () {
        return _this.handleYearChange(_this.state.date);
      });
    });

    _defineProperty(_assertThisInitialized(_this), "renderCalendarHeader", function () {
      var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.state.date;
      var order = arguments.length > 1 ? arguments[1] : undefined;
      return /*#__PURE__*/React.createElement(_calendarHeader.default, _extends({}, _this.props, {
        key: "month-header-".concat(order),
        date: date,
        order: order,
        onMonthChange: _this.changeMonth,
        onYearChange: _this.changeYear
      }));
    });

    _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (event) {
      switch (event.key) {
        case 'ArrowUp':
        case 'ArrowDown':
        case 'ArrowLeft':
        case 'ArrowRight':
        case 'Home':
        case 'End':
        case 'PageUp':
        case 'PageDown':
          _this.handleArrowKey(event.key);

          event.preventDefault();
          event.stopPropagation();
          break;
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleArrowKey", function (key) {
      var oldDate = _this.state.highlightedDate;
      var highlightedDate = oldDate;

      var currentDate = _this.dateHelpers.date();

      switch (key) {
        case 'ArrowLeft':
          // adding `new Date()` as the last option to satisfy Flow
          highlightedDate = _this.dateHelpers.subDays(highlightedDate ? highlightedDate : currentDate, 1);
          break;

        case 'ArrowRight':
          highlightedDate = _this.dateHelpers.addDays( // adding `new Date()` as the last option to satisfy Flow
          highlightedDate ? highlightedDate : currentDate, 1);
          break;

        case 'ArrowUp':
          highlightedDate = _this.dateHelpers.subWeeks( // adding `new Date()` as the last option to satisfy Flow
          highlightedDate ? highlightedDate : currentDate, 1);
          break;

        case 'ArrowDown':
          highlightedDate = _this.dateHelpers.addWeeks( // adding `new Date()` as the last option to satisfy Flow
          highlightedDate ? highlightedDate : currentDate, 1);
          break;

        case 'Home':
          highlightedDate = _this.dateHelpers.getStartOfWeek( // adding `new Date()` as the last option to satisfy Flow
          highlightedDate ? highlightedDate : currentDate);
          break;

        case 'End':
          highlightedDate = _this.dateHelpers.getEndOfWeek( // adding `new Date()` as the last option to satisfy Flow
          highlightedDate ? highlightedDate : currentDate);
          break;

        case 'PageUp':
          highlightedDate = _this.dateHelpers.subMonths( // adding `new Date()` as the last option to satisfy Flow
          highlightedDate ? highlightedDate : currentDate, 1);
          break;

        case 'PageDown':
          highlightedDate = _this.dateHelpers.addMonths( // adding `new Date()` as the last option to satisfy Flow
          highlightedDate ? highlightedDate : currentDate, 1);
          break;
      }

      _this.setState({
        highlightedDate: highlightedDate,
        date: highlightedDate
      });
    });

    _defineProperty(_assertThisInitialized(_this), "focusCalendar", function () {
      if (!_this.state.focused) {
        _this.setState({
          focused: true
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "blurCalendar", function () {
      if (typeof document !== 'undefined') {
        var activeElm = document.activeElement;

        if (_this.calendar && !_this.calendar.contains(activeElm)) {
          _this.setState({
            focused: false
          });
        }
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleTabbing", function (event) {
      if (typeof document !== 'undefined') {
        if (event.keyCode === 9) {
          var activeElm = document.activeElement; // need to look for any tabindex >= 0 and ideally for not disabled
          // focusable by default elements like input, button, etc.

          var focusable = _this.state.rootElement ? _this.state.rootElement.querySelectorAll('[tabindex="0"]') : null;
          var length = focusable ? focusable.length : 0;

          if (event.shiftKey) {
            if (focusable && activeElm === focusable[0]) {
              event.preventDefault();
              focusable[length - 1].focus();
            }
          } else {
            if (focusable && activeElm === focusable[length - 1]) {
              event.preventDefault();
              focusable[0].focus();
            }
          }
        }
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onDayFocus", function (data) {
      var date = data.date;

      _this.setState({
        highlightedDate: date
      });

      _this.focusCalendar();

      _this.props.onDayFocus && _this.props.onDayFocus(data);
    });

    _defineProperty(_assertThisInitialized(_this), "onDayMouseOver", function (data) {
      var date = data.date;

      _this.setState({
        highlightedDate: date
      });

      _this.props.onDayMouseOver && _this.props.onDayMouseOver(data);
    });

    _defineProperty(_assertThisInitialized(_this), "onDayMouseLeave", function (data) {
      var date = data.date;
      var value = _this.props.value;

      var selected = _this.getSingleDate(value);

      _this.setState({
        highlightedDate: selected || date
      });

      _this.props.onDayMouseLeave && _this.props.onDayMouseLeave(data);
    });

    _defineProperty(_assertThisInitialized(_this), "handleDateChange", function (data) {
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
      var _this$props$onChange = _this.props.onChange,
          onChange = _this$props$onChange === void 0 ? function (params) {} : _this$props$onChange;
      var updatedDate = data.date; // Apply the currently selected time values (saved in state) to the updated date

      if (Array.isArray(data.date)) {
        // We'll need to update the date in time values of internal state
        var newTimeState = _toConsumableArray(_this.state.time);

        var start = data.date[0] ? _this.dateHelpers.applyDateToTime(newTimeState[0], data.date[0]) : null;
        var end = data.date[1] ? _this.dateHelpers.applyDateToTime(newTimeState[1], data.date[1]) : null;
        newTimeState[0] = start;

        if (end) {
          updatedDate = [start, end];
          newTimeState[1] = end;
        } else {
          updatedDate = [start];
        } // Update the date in time values of internal state


        _this.setState({
          time: newTimeState
        });
      } else if (!Array.isArray(_this.props.value) && data.date) {
        var _newTimeState = _this.dateHelpers.applyDateToTime(_this.state.time[0], data.date);

        updatedDate = _newTimeState; // Update the date in time values of internal state

        _this.setState({
          time: [_newTimeState]
        });
      }

      onChange({
        date: updatedDate
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleTimeChange", function (time, index) {
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
      var _this$props$onChange2 = _this.props.onChange,
          onChange = _this$props$onChange2 === void 0 ? function (params) {} : _this$props$onChange2; // Save/update the time value in internal state

      var newTimeState = _toConsumableArray(_this.state.time);

      newTimeState[index] = _this.dateHelpers.applyTimeToDate(newTimeState[index], time);

      _this.setState({
        time: newTimeState
      }); // Time change calls calendar's onChange handler
      // with the date value set to the date with updated time


      if (Array.isArray(_this.props.value)) {
        var dates = _this.props.value.map(function (date, i) {
          if (date && index === i) {
            return _this.dateHelpers.applyTimeToDate(date, time);
          }

          return date;
        });

        onChange({
          date: [dates[0], dates[1]]
        });
      } else {
        var date = _this.dateHelpers.applyTimeToDate(_this.props.value, time);

        onChange({
          date: date
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "renderMonths", function (translations) {
      var _this$props2 = _this.props,
          _this$props2$override = _this$props2.overrides,
          overrides = _this$props2$override === void 0 ? {} : _this$props2$override,
          orientation = _this$props2.orientation;
      var monthList = [];

      var _getOverrides = (0, _overrides.getOverrides)(overrides.CalendarContainer, _styledComponents.StyledCalendarContainer),
          _getOverrides2 = _slicedToArray(_getOverrides, 2),
          CalendarContainer = _getOverrides2[0],
          calendarContainerProps = _getOverrides2[1];

      var _getOverrides3 = (0, _overrides.getOverrides)(overrides.MonthContainer, _styledComponents.StyledMonthContainer),
          _getOverrides4 = _slicedToArray(_getOverrides3, 2),
          MonthContainer = _getOverrides4[0],
          monthContainerProps = _getOverrides4[1];

      for (var i = 0; i < (_this.props.monthsShown || 1); ++i) {
        var monthSubComponents = [];

        var monthDate = _this.dateHelpers.addMonths(_this.state.date, i);

        var monthKey = "month-".concat(i); // @ts-ignore

        monthSubComponents.push(_this.renderCalendarHeader(monthDate, i));
        monthSubComponents.push(
        /*#__PURE__*/
        // @ts-ignore
        React.createElement(CalendarContainer, _extends({
          key: monthKey // @ts-ignore
          ,
          ref: function ref(calendar) {
            _this.calendar = calendar;
          },
          role: "grid",
          "aria-roledescription": translations.ariaRoleDescCalMonth,
          "aria-multiselectable": _this.props.range || null,
          onKeyDown: _this.onKeyDown
        }, calendarContainerProps, {
          $density: _this.props.density
        }), /*#__PURE__*/React.createElement(_month.default, {
          adapter: _this.props.adapter,
          date: monthDate,
          dateLabel: _this.props.dateLabel,
          density: _this.props.density,
          excludeDates: _this.props.excludeDates,
          filterDate: _this.props.filterDate,
          highlightedDate: _this.state.highlightedDate,
          includeDates: _this.props.includeDates,
          focusedCalendar: _this.state.focused,
          range: _this.props.range,
          locale: _this.props.locale,
          maxDate: _this.props.maxDate,
          minDate: _this.props.minDate,
          month: _this.dateHelpers.getMonth(_this.state.date),
          onDayBlur: _this.blurCalendar,
          onDayFocus: _this.onDayFocus,
          onDayClick: _this.props.onDayClick,
          onDayMouseOver: _this.onDayMouseOver,
          onDayMouseLeave: _this.onDayMouseLeave,
          onChange: _this.handleDateChange,
          overrides: overrides,
          value: _this.props.value,
          peekNextMonth: _this.props.peekNextMonth,
          fixedHeight: _this.props.fixedHeight,
          hasLockedBehavior: !!_this.props.hasLockedBehavior,
          selectedInput: _this.props.selectedInput
        }))); // @ts-ignore

        monthList.push( /*#__PURE__*/React.createElement("div", {
          key: "month-component-".concat(i)
        }, monthSubComponents));
      }

      return /*#__PURE__*/React.createElement(MonthContainer, _extends({
        $orientation: orientation
      }, monthContainerProps), monthList);
    });

    _defineProperty(_assertThisInitialized(_this), "renderTimeSelect", function (value, onChange, label) {
      var _this$props$overrides = _this.props.overrides,
          overrides = _this$props$overrides === void 0 ? {} : _this$props$overrides;

      var _getOverrides5 = (0, _overrides.getOverrides)(overrides.TimeSelectContainer, _styledComponents.StyledSelectorContainer),
          _getOverrides6 = _slicedToArray(_getOverrides5, 2),
          TimeSelectContainer = _getOverrides6[0],
          timeSelectContainerProps = _getOverrides6[1];

      var _getOverrides7 = (0, _overrides.getOverrides)(overrides.TimeSelectFormControl, _formControl.FormControl),
          _getOverrides8 = _slicedToArray(_getOverrides7, 2),
          TimeSelectFormControl = _getOverrides8[0],
          timeSelectFormControlProps = _getOverrides8[1];

      var _getOverrides9 = (0, _overrides.getOverrides)(overrides.TimeSelect, _timepicker.default),
          _getOverrides10 = _slicedToArray(_getOverrides9, 2),
          TimeSelect = _getOverrides10[0],
          timeSelectProps = _getOverrides10[1];

      return /*#__PURE__*/React.createElement(TimeSelectContainer, timeSelectContainerProps, /*#__PURE__*/React.createElement(TimeSelectFormControl, _extends({
        label: label
      }, timeSelectFormControlProps), /*#__PURE__*/React.createElement(TimeSelect, _extends({
        value: value ? _this.dateHelpers.date(value) : value,
        onChange: onChange,
        nullable: true
      }, timeSelectProps))));
    });

    _defineProperty(_assertThisInitialized(_this), "renderQuickSelect", function () {
      var _this$props$overrides2 = _this.props.overrides,
          overrides = _this$props$overrides2 === void 0 ? {} : _this$props$overrides2;

      var _getOverrides11 = (0, _overrides.getOverrides)(overrides.QuickSelectContainer, _styledComponents.StyledSelectorContainer),
          _getOverrides12 = _slicedToArray(_getOverrides11, 2),
          QuickSelectContainer = _getOverrides12[0],
          quickSelectContainerProps = _getOverrides12[1];

      var _getOverrides13 = (0, _overrides.getOverrides)(overrides.QuickSelectFormControl, _formControl.FormControl),
          _getOverrides14 = _slicedToArray(_getOverrides13, 2),
          QuickSelectFormControl = _getOverrides14[0],
          quickSelectFormControlProps = _getOverrides14[1];

      var _getOverrides15 = (0, _overrides.getOverrides)( //
      overrides.QuickSelect, _select.Select),
          _getOverrides16 = _slicedToArray(_getOverrides15, 2),
          QuickSelect = _getOverrides16[0],
          _getOverrides16$ = _getOverrides16[1],
          quickSelectOverrides = _getOverrides16$.overrides,
          restQuickSelectProps = _objectWithoutProperties(_getOverrides16$, _excluded);

      if (!_this.props.quickSelect) {
        return null;
      }

      var NOW = _this.dateHelpers.set(_this.dateHelpers.date(), {
        hours: 12,
        minutes: 0,
        seconds: 0
      });

      return /*#__PURE__*/React.createElement(_locale.LocaleContext.Consumer, null, function (locale) {
        return /*#__PURE__*/React.createElement(QuickSelectContainer, quickSelectContainerProps, /*#__PURE__*/React.createElement(QuickSelectFormControl, _extends({
          label: locale.datepicker.quickSelectLabel
        }, quickSelectFormControlProps), /*#__PURE__*/React.createElement(QuickSelect, _extends({
          "aria-label": locale.datepicker.quickSelectAriaLabel,
          labelKey: "id" // @ts-ignore
          ,
          onChange: function onChange(params) {
            if (!params.option) {
              _this.setState({
                quickSelectId: null
              });

              _this.props.onChange && _this.props.onChange({
                date: []
              });
            } else {
              _this.setState({
                quickSelectId: params.option.id
              });

              if (_this.props.onChange) {
                if (_this.props.range) {
                  _this.props.onChange({
                    date: [params.option.beginDate, params.option.endDate || NOW]
                  });
                } else {
                  _this.props.onChange({
                    date: params.option.beginDate
                  });
                }
              }
            }

            if (_this.props.onQuickSelectChange) {
              _this.props.onQuickSelectChange(params.option);
            }
          },
          options: _this.props.quickSelectOptions || [{
            id: locale.datepicker.pastWeek,
            beginDate: _this.dateHelpers.subWeeks(NOW, 1)
          }, {
            id: locale.datepicker.pastMonth,
            beginDate: _this.dateHelpers.subMonths(NOW, 1)
          }, {
            id: locale.datepicker.pastThreeMonths,
            beginDate: _this.dateHelpers.subMonths(NOW, 3)
          }, {
            id: locale.datepicker.pastSixMonths,
            beginDate: _this.dateHelpers.subMonths(NOW, 6)
          }, {
            id: locale.datepicker.pastYear,
            beginDate: _this.dateHelpers.subYears(NOW, 1)
          }, {
            id: locale.datepicker.pastTwoYears,
            beginDate: _this.dateHelpers.subYears(NOW, 2)
          }],
          placeholder: locale.datepicker.quickSelectPlaceholder,
          value: _this.state.quickSelectId && [{
            id: _this.state.quickSelectId
          }],
          overrides: (0, _overrides.mergeOverrides)({
            Dropdown: {
              style: {
                textAlign: 'start'
              }
            }
          }, quickSelectOverrides)
        }, restQuickSelectProps))));
      });
    });

    _defineProperty(_assertThisInitialized(_this), "renderActionBar", function () {
      var _this$props3 = _this.props,
          _this$props3$override = _this$props3.overrides,
          overrides = _this$props3$override === void 0 ? {} : _this$props3$override,
          primaryButton = _this$props3.primaryButton,
          secondaryButton = _this$props3.secondaryButton;

      var _getOverrides17 = (0, _overrides.getOverrides)(overrides.ButtonDock, _buttonDock.ButtonDock),
          _getOverrides18 = _slicedToArray(_getOverrides17, 2),
          ButtonDockComponent = _getOverrides18[0],
          buttonDockProps = _getOverrides18[1];

      var _getOverrides19 = (0, _overrides.getOverrides)(overrides.PrimaryButton, _button.Button),
          _getOverrides20 = _slicedToArray(_getOverrides19, 2),
          PrimaryButtonComponent = _getOverrides20[0],
          primaryButtonProps = _getOverrides20[1];

      var _getOverrides21 = (0, _overrides.getOverrides)(overrides.SecondaryButton, _button.Button),
          _getOverrides22 = _slicedToArray(_getOverrides21, 2),
          SecondaryButtonComponent = _getOverrides22[0],
          secondaryButtonProps = _getOverrides22[1];

      var primaryButtonComponent = primaryButton != null ? /*#__PURE__*/React.createElement(PrimaryButtonComponent, _extends({
        onClick: function onClick() {
          return primaryButton.onClick();
        }
      }, primaryButtonProps), primaryButton.label) : null;
      var secondaryButtonComponent = secondaryButton != null ? /*#__PURE__*/React.createElement(SecondaryButtonComponent, _extends({
        onClick: function onClick() {
          return secondaryButton.onClick();
        },
        kind: _button.KIND.tertiary
      }, secondaryButtonProps), secondaryButton.label) : null;

      if (primaryButtonComponent || secondaryButtonComponent) {
        return /*#__PURE__*/React.createElement(ButtonDockComponent, _extends({
          primaryAction: primaryButtonComponent,
          dismissiveAction: secondaryButtonComponent,
          overrides: (0, _overrides.mergeOverrides)({
            ActionSubContainer: {
              style: {
                flexDirection: 'row-reverse'
              }
            },
            Root: {
              style: {
                width: '90%'
              }
            }
          }, buttonDockProps.overrides)
        }, buttonDockProps));
      }

      return null;
    });

    var _this$props4 = _this.props,
        _highlightedDate = _this$props4.highlightedDate,
        _value = _this$props4.value,
        adapter = _this$props4.adapter; // @ts-ignore

    _this.dateHelpers = new _dateHelpers.default(adapter);

    var dateInView = _this.getDateInView(); // @ts-ignore


    var _time = [];

    if (Array.isArray(_value)) {
      // @ts-ignore
      _time = _toConsumableArray(_value);
    } else if (_value) {
      // @ts-ignore
      _time = [_value];
    }

    _this.state = {
      highlightedDate: _this.getSingleDate(_value) || (_highlightedDate && _this.dateHelpers.isSameMonth(dateInView, _highlightedDate) ? _highlightedDate : _this.dateHelpers.date()),
      focused: false,
      date: dateInView,
      quickSelectId: null,
      rootElement: null,
      // @ts-ignore
      time: _time
    };
    return _this;
  }

  _createClass(Calendar, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.props.autoFocusCalendar) {
        this.focusCalendar();
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      if (this.props.highlightedDate && !this.dateHelpers.isSameDay(this.props.highlightedDate, prevProps.highlightedDate)) {
        this.setState({
          date: this.props.highlightedDate
        });
      }

      if (this.props.autoFocusCalendar && this.props.autoFocusCalendar !== prevProps.autoFocusCalendar) {
        this.focusCalendar();
      }

      if (prevProps.value !== this.props.value) {
        var nextDate = this.getDateInView();

        if (!this.isInView(nextDate)) {
          this.setState({
            date: nextDate
          });
        }
      }
    }
  }, {
    key: "isInView",
    value: function isInView(date) {
      // we calculate the month delta between the date arg and the date in the state.
      var currentDate = this.state.date; // First we get the year delta

      var yearDelta = this.dateHelpers.getYear(date) - this.dateHelpers.getYear(currentDate); // then we convert it to months. Then we simply add the date-without-year month delta back in.

      var monthDelta = yearDelta * 12 + this.dateHelpers.getMonth(date) - this.dateHelpers.getMonth(currentDate); // we just check that the delta is between the range given by "this month" (i.e. 0) and "the last month" (i.e. monthsShown)

      return monthDelta >= 0 && monthDelta < (this.props.monthsShown || 1);
    }
  }, {
    key: "getSingleDate",
    value: function getSingleDate(value) {
      // need to check this.props.range but flow would complain
      // at the return value in the else clause
      if (Array.isArray(value)) {
        return value[0] || null;
      }

      return value;
    }
  }, {
    key: "setHighlightedDate",
    value: function setHighlightedDate(date) {
      var value = this.props.value;
      var selected = this.getSingleDate(value);
      var nextState;

      if (selected && this.dateHelpers.isSameMonth(selected, date) && this.dateHelpers.isSameYear(selected, date)) {
        nextState = {
          highlightedDate: selected
        };
      } else {
        nextState = {
          highlightedDate: date
        };
      }

      this.setState(nextState);
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props$overrides3 = this.props.overrides,
          overrides = _this$props$overrides3 === void 0 ? {} : _this$props$overrides3;

      var _getOverrides23 = (0, _overrides.getOverrides)(overrides.Root, _styledComponents.StyledRoot),
          _getOverrides24 = _slicedToArray(_getOverrides23, 2),
          Root = _getOverrides24[0],
          rootProps = _getOverrides24[1]; // @ts-ignore


      var _concat = [].concat(this.props.value),
          _concat2 = _slicedToArray(_concat, 2),
          startDate = _concat2[0],
          endDate = _concat2[1];

      return /*#__PURE__*/React.createElement(_locale.LocaleContext.Consumer, null, function (locale) {
        return /*#__PURE__*/React.createElement(Root, _extends({
          $density: _this2.props.density,
          "data-baseweb": "calendar",
          role: "application",
          "aria-roledescription": "date picker" // @ts-ignore
          ,
          ref: function ref(root) {
            if (root && root instanceof HTMLElement && !_this2.state.rootElement) {
              _this2.setState({
                rootElement: root
              });
            }
          },
          "aria-label": locale.datepicker.ariaLabelCalendar,
          onKeyDown: _this2.props.trapTabbing ? _this2.handleTabbing : null
        }, rootProps), _this2.renderMonths({
          ariaRoleDescCalMonth: locale.datepicker.ariaRoleDescriptionCalendarMonth
        }), _this2.props.timeSelectStart && _this2.renderTimeSelect(startDate, // @ts-ignore
        function (time) {
          return _this2.handleTimeChange(time, 0);
        }, locale.datepicker.timeSelectStartLabel), _this2.props.timeSelectEnd && _this2.props.range && _this2.renderTimeSelect(endDate, // @ts-ignore
        function (time) {
          return _this2.handleTimeChange(time, 1);
        }, locale.datepicker.timeSelectEndLabel), _this2.renderQuickSelect(), _this2.renderActionBar());
      });
    }
  }]);

  return Calendar;
}(React.Component);

exports.default = Calendar;

_defineProperty(Calendar, "defaultProps", {
  autoFocusCalendar: false,
  dateLabel: null,
  density: _constants.DENSITY.default,
  excludeDates: null,
  filterDate: null,
  highlightedDate: null,
  includeDates: null,
  range: false,
  locale: null,
  maxDate: null,
  minDate: null,
  onDayClick: function onDayClick() {},
  onDayFocus: function onDayFocus() {},
  onDayMouseOver: function onDayMouseOver() {},
  onDayMouseLeave: function onDayMouseLeave() {},
  onMonthChange: function onMonthChange() {},
  onYearChange: function onYearChange() {},
  onChange: function onChange() {},
  orientation: _constants.ORIENTATION.horizontal,
  overrides: {},
  peekNextMonth: false,
  // @ts-ignore
  adapter: _dateFnsAdapter.default,
  value: null,
  trapTabbing: false
});