"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.StyledBox = void 0;
var _isNil2 = _interopRequireDefault(require("ramda/src/isNil"));
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _dateFns = require("date-fns");
var _ = require("../../../..");
var _dateUtils = require("../../shared/dateUtils");
var _CalendarDay = _interopRequireDefault(require("../../shared/components/CalendarDay"));
var _hooks = require("../../shared/hooks");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var StyledBox = exports.StyledBox = (0, _styledComponents.default)(_.Box).withConfig({
  displayName: "Calendar__StyledBox",
  componentId: "sc-1c6ogvy-0"
})(["border:1px solid transparent;min-width:", "px;text-align:center;margin-bottom:", "px;margin-top:", "px;"], function (_ref) {
  var theme = _ref.theme;
  return theme.spacing.unit(10);
}, function (_ref2) {
  var theme = _ref2.theme;
  return theme.spacing.unit(2);
}, function (_ref3) {
  var theme = _ref3.theme;
  return theme.spacing.unit(3);
});
var Calendar = function Calendar(_ref4) {
  var _calendar$weekDays, _calendar$dates;
  var disableDate = _ref4.disableDate,
    enableDate = _ref4.enableDate,
    locale = _ref4.locale,
    viewedDate = _ref4.viewedDate,
    onMonthChange = _ref4.onMonthChange,
    _onClick = _ref4.onClick,
    selectedDate = _ref4.selectedDate,
    selectedEndDate = _ref4.selectedEndDate,
    fullscreenMode = _ref4.fullscreenMode;
  var calendarRef = (0, _react.useRef)(null);
  var localeObj = (0, _dateUtils.getLocale)(locale);
  var calendar = (0, _dateUtils.getCalendar)(viewedDate || new Date(), {
    locale: localeObj
  });
  var dateIsDisabled = function dateIsDisabled(dateToCheck) {
    var isEnabled = dateToCheck && enableDate && enableDate(dateToCheck);
    var isDisabled = dateToCheck && disableDate && disableDate(dateToCheck);
    if ((0, _isNil2.default)(isEnabled) && (0, _isNil2.default)(isDisabled)) return false;
    if ((0, _isNil2.default)(isEnabled)) return isDisabled;
    if ((0, _isNil2.default)(isDisabled)) return !isEnabled;
    return false;
  };
  var _useFocusedDate = (0, _hooks.useFocusedDate)({
      calendarRef: calendarRef,
      viewedDate: viewedDate,
      calendar: calendar,
      onMonthChange: onMonthChange,
      dateIsDisabled: dateIsDisabled,
      onSelectDate: _onClick
    }),
    focusedDate = _useFocusedDate.focusedDate,
    handleKeyPress = _useFocusedDate.handleKeyPress,
    handleFocus = _useFocusedDate.handleFocus;
  return _react.default.createElement(_.Flexbox, {
    ref: calendarRef,
    container: true,
    direction: "column"
  }, _react.default.createElement(_.Flexbox, {
    container: true,
    justifyContent: "space-between",
    "aria-hidden": true
  }, (_calendar$weekDays = calendar.weekDays) === null || _calendar$weekDays === void 0 ? void 0 : _calendar$weekDays.map(function (n) {
    return _react.default.createElement(_.Flexbox, {
      item: true,
      justifyContent: "center",
      alignItems: "center",
      key: n
    }, _react.default.createElement(StyledBox, null, _react.default.createElement(_.Typography, {
      type: "tertiary"
    }, n)));
  })), (_calendar$dates = calendar.dates) === null || _calendar$dates === void 0 ? void 0 : _calendar$dates.map(function (week, weekIndex) {
    return _react.default.createElement(_.Flexbox, {
      container: true,
      justifyContent: "space-between",
      key: week.toString()
    }, week === null || week === void 0 ? void 0 : week.map(function (day, dayIndex) {
      return _react.default.createElement(_CalendarDay.default, {
        onFocus: function onFocus() {
          handleFocus(day);
        },
        focused: !!focusedDate && (0, _dateFns.isSameDay)(day, focusedDate),
        key: day.toString(),
        date: day,
        disabled: !!dateIsDisabled(day),
        onClick: function onClick() {
          if (!dateIsDisabled(day)) _onClick(day);
        },
        onKeyDown: handleKeyPress,
        selected: selectedDate && (0, _dateFns.isSameDay)(selectedDate, day) || selectedEndDate && (0, _dateFns.isSameDay)(selectedEndDate, day),
        sameMonth: (0, _dateFns.isSameMonth)(viewedDate, day),
        locale: localeObj,
        isWithinRange: function () {
          if (selectedEndDate && selectedDate < selectedEndDate) return (0, _dateFns.isWithinInterval)(day, {
            start: selectedDate,
            end: selectedEndDate
          });
          if (selectedEndDate && selectedDate > selectedEndDate) return (0, _dateFns.isWithinInterval)(day, {
            start: selectedEndDate,
            end: selectedDate
          });
          return false;
        }(),
        isFirstDay: weekIndex === 0 && dayIndex === 0,
        isLastDay: weekIndex === calendar.dates.length - 1 && dayIndex === week.length - 1,
        fullscreenMode: fullscreenMode
      });
    }));
  }));
};
var _default = exports.default = Calendar;