"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.YearMonthPicker = void 0;
var _react = _interopRequireWildcard(require("react"));
var _dateFns = require("date-fns");
var _Styles = require("./Styles");
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 _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 _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 _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 _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var YearMonthPicker = exports.YearMonthPicker = function YearMonthPicker(_ref) {
  var month = _ref.month,
    _ref$onChange = _ref.onChange,
    onChange = _ref$onChange === void 0 ? function () {
      return null;
    } : _ref$onChange,
    _ref$isShown = _ref.isShown,
    isShown = _ref$isShown === void 0 ? true : _ref$isShown,
    onClick = _ref.onClick,
    text = _ref.text,
    selectedDays = _ref.selectedDays,
    selectionMode = _ref.selectionMode;
  var _useState = (0, _react.useState)(),
    _useState2 = _slicedToArray(_useState, 2),
    enteredTo = _useState2[0],
    setEnteredTo = _useState2[1];
  var currentYear = (0, _dateFns.getYear)(new Date());
  var months = Array.from({
    length: 12
  }, function (_, i) {
    return (0, _dateFns.format)(new Date(currentYear, i, 1), "MMMM");
  });
  var handleMonthChange = (0, _react.useCallback)(function (e) {
    onChange(new Date((0, _dateFns.getYear)(month), e.target.dataset.value));
    if (onClick) {
      onClick(e);
    }
  }, [month, onChange, onClick]);
  var onMouseEnterCallback = (0, _react.useCallback)(function (e) {
    if (selectionMode === "quarterly") {
      setEnteredTo(Math.floor(e.target.getAttribute("data-value") / 3));
    }
  }, [selectionMode]);
  var renderMonths = (0, _react.useMemo)(function () {
    return /*#__PURE__*/_react.default.createElement(_Styles.StyledMonths, {
      className: "c-monthPicker",
      "data-test": "months"
    }, months.map(function (singleMonth, i) {
      return /*#__PURE__*/_react.default.createElement(_Styles.StyledMonth, {
        className: "c-monthPicker__month",
        key: i,
        onClick: handleMonthChange,
        onMouseEnter: onMouseEnterCallback,
        "data-value": i,
        "data-test": "month",
        $isCurrent: month.getFullYear() === new Date().getFullYear() && new Date().getMonth() === i,
        $isCurrentQuarter: Math.floor(i / 3) === enteredTo,
        $isSelected: selectedDays && !(selectedDays instanceof Date) && selectedDays.from && selectedDays.to && selectedDays.from.getFullYear() === (0, _dateFns.getYear)(month) && i >= (0, _dateFns.getMonth)(selectedDays.from) && i <= (0, _dateFns.getMonth)(selectedDays.to)
      }, singleMonth.substring(0, 3));
    }));
  }, [months, handleMonthChange, onMouseEnterCallback, enteredTo, selectedDays, month]);
  return /*#__PURE__*/_react.default.createElement(_Styles.StyledYearMonthPicker, {
    className: "c-DayPicker-Caption",
    role: "button"
  }, /*#__PURE__*/_react.default.createElement(_Styles.StyledYearMonthPickerNavBar, {
    role: "button"
  }, /*#__PURE__*/_react.default.createElement(_Styles.StyledYearMonthPickerNavBarItem, {
    "data-test": "show-picker",
    onClick: onClick
  }, text)), isShown && renderMonths);
};
YearMonthPicker.displayName = "YearMonthPicker";
//# sourceMappingURL=YearMonthPicker.js.map