"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 _constants = require("../../shared/constants");
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 }; }
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; }
var StyledBox = exports.StyledBox = (0, _styledComponents.default)(_.Box).withConfig({
  displayName: "DoubleCalendar__StyledBox",
  componentId: "sc-dj1ati-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 HiddenDate = _styledComponents.default.div.withConfig({
  displayName: "DoubleCalendar__HiddenDate",
  componentId: "sc-dj1ati-1"
})(["min-width:", "px;min-height:", "px;margin:", "px 0;", ""], function (_ref4) {
  var theme = _ref4.theme;
  return theme.spacing.unit(10) + 2;
}, function (_ref5) {
  var theme = _ref5.theme;
  return theme.spacing.unit(10) + 2;
}, function (_ref6) {
  var theme = _ref6.theme;
  return theme.spacing.unit(0.5);
}, function (_ref7) {
  var $withGutter = _ref7.$withGutter;
  return $withGutter ? "margin-right: ".concat(_constants.DOUBLE_CALENDAR_GUTTER, "px;") : '';
});
var StyledCalendarContainer = (0, _styledComponents.default)(_.Flexbox).withConfig({
  displayName: "DoubleCalendar__StyledCalendarContainer",
  componentId: "sc-dj1ati-2"
})(["", ""], function (_ref8) {
  var $withGutter = _ref8.$withGutter;
  return $withGutter ? "margin-right: ".concat(_constants.DOUBLE_CALENDAR_GUTTER, "px;") : '';
});
var DoubleCalendar = function DoubleCalendar(_ref9) {
  var _calendar$weekDays, _calendar$dates;
  var disableDate = _ref9.disableDate,
    enableDate = _ref9.enableDate,
    locale = _ref9.locale,
    viewedDate = _ref9.viewedDate,
    _onClick = _ref9.onClick,
    selectedStartDate = _ref9.selectedStartDate,
    selectedEndDate = _ref9.selectedEndDate,
    onMonthChange = _ref9.onMonthChange;
  var calendarRef = (0, _react.useRef)(null);
  var dateIsDisabled = function dateIsDisabled(dateToCheck) {
    var isEnabled = dateToCheck && (enableDate === null || enableDate === void 0 ? void 0 : enableDate(dateToCheck));
    var isDisabled = dateToCheck && (disableDate === null || disableDate === void 0 ? void 0 : 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 leftViewedDate = viewedDate;
  var rightViewedDate = (0, _dateFns.addMonths)(leftViewedDate, 1);
  var localeObj = (0, _dateUtils.getLocale)(locale);
  var calendar = (0, _react.useMemo)(function () {
    var _leftCalendar$dates;
    var leftCalendar = (0, _dateUtils.getCalendar)(viewedDate, {
      locale: localeObj
    });
    var rightCalendar = (0, _dateUtils.getCalendar)((0, _dateFns.addMonths)(viewedDate, 1), {
      locale: localeObj
    });
    return {
      weekDays: [].concat(_toConsumableArray(leftCalendar.weekDays), _toConsumableArray(rightCalendar.weekDays)),
      dates: leftCalendar === null || leftCalendar === void 0 || (_leftCalendar$dates = leftCalendar.dates) === null || _leftCalendar$dates === void 0 ? void 0 : _leftCalendar$dates.map(function (leftWeek, index) {
        return [].concat(_toConsumableArray(leftWeek), _toConsumableArray(rightCalendar.dates[index]));
      })
    };
  }, [localeObj, viewedDate]);
  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: "row",
    justifyContent: "space-between"
  }, _react.default.createElement(_.Flexbox, {
    container: true,
    direction: "column",
    "aria-hidden": true
  }, _react.default.createElement(_.Flexbox, {
    container: true
  }, calendar === null || calendar === void 0 || (_calendar$weekDays = calendar.weekDays) === null || _calendar$weekDays === void 0 ? void 0 : _calendar$weekDays.map(function (n, index) {
    return _react.default.createElement(StyledCalendarContainer, {
      item: true,
      justifyContent: "center",
      alignItems: "center",
      key: "".concat(n, "-").concat(index < _constants.NUMBER_OF_VISIBLE_WEEKDAYS_SINGLE ? 'left' : 'right'),
      $withGutter: index === _constants.NUMBER_OF_VISIBLE_WEEKDAYS_SINGLE - 1
    }, _react.default.createElement(StyledBox, null, _react.default.createElement(_.Typography, {
      type: "tertiary"
    }, n)));
  })), calendar === null || calendar === void 0 || (_calendar$dates = calendar.dates) === null || _calendar$dates === void 0 ? void 0 : _calendar$dates.map(function () {
    var week = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    var weekIndex = arguments.length > 1 ? arguments[1] : undefined;
    return _react.default.createElement(_.Flexbox, {
      container: true,
      justifyContent: "flex-start",
      key: week.toString()
    }, week === null || week === void 0 ? void 0 : week.map(function (day, dayIndex) {
      var hideDate = !(0, _dateFns.isSameMonth)(leftViewedDate, day) && (0, _dateFns.isSameMonth)(rightViewedDate, day) && dayIndex < _constants.NUMBER_OF_VISIBLE_WEEKDAYS_SINGLE || !(0, _dateFns.isSameMonth)(rightViewedDate, day) && (0, _dateFns.isSameMonth)(leftViewedDate, day) && dayIndex >= _constants.NUMBER_OF_VISIBLE_WEEKDAYS_SINGLE;
      return hideDate ? _react.default.createElement(HiddenDate, {
        key: day.toString(),
        $withGutter: dayIndex === _constants.NUMBER_OF_VISIBLE_WEEKDAYS_SINGLE - 1,
        "aria-hidden": true
      }) : _react.default.createElement(_CalendarDay.default, {
        withGutter: dayIndex === _constants.NUMBER_OF_VISIBLE_WEEKDAYS_SINGLE - 1,
        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: selectedStartDate && (0, _dateFns.isSameDay)(selectedStartDate, day) || selectedEndDate && (0, _dateFns.isSameDay)(selectedEndDate, day),
        sameMonth: (0, _dateFns.isSameMonth)(leftViewedDate, day) || (0, _dateFns.isSameMonth)(rightViewedDate, day),
        locale: localeObj,
        isWithinRange: function () {
          if (selectedStartDate && selectedEndDate && selectedStartDate < selectedEndDate) return (0, _dateFns.isWithinInterval)(day, {
            start: selectedStartDate,
            end: selectedEndDate
          });
          if (selectedStartDate && selectedEndDate && selectedStartDate > selectedEndDate) return (0, _dateFns.isWithinInterval)(day, {
            start: selectedEndDate,
            end: selectedStartDate
          });
          return false;
        }(),
        isFirstDay: weekIndex === 0 && dayIndex === 0 || (0, _dateFns.isFirstDayOfMonth)(day) && (0, _dateFns.isSameMonth)(rightViewedDate, day),
        isLastDay: weekIndex === calendar.dates.length - 1 && dayIndex === week.length - 1 || (0, _dateFns.isLastDayOfMonth)(day) && (0, _dateFns.isSameMonth)(leftViewedDate, day)
      });
    }));
  })));
};
var _default = exports.default = DoubleCalendar;