"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 = exports.MonthPicker = void 0;
var _react = _interopRequireWildcard(require("react"));
var _Button = _interopRequireDefault(require("../Button/Button"));
var _Choice = _interopRequireDefault(require("../ChoiceList/Choice"));
var _classnames = _interopRequireDefault(require("classnames"));
var _describeField = _interopRequireDefault(require("../utilities/describeField"));
var _useId = _interopRequireDefault(require("../utilities/useId"));
var _Label = require("../Label");
var _getMonthNames = require("./getMonthNames");
var _i18n = require("../i18n");
var _useLabelProps = require("../Label/useLabelProps");
var _useHint2 = require("../Hint/useHint");
var _useInlineError2 = require("../InlineError/useInlineError");
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 _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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var monthNumbers = function () {
  var months = [];
  for (var m = 1; m <= _getMonthNames.NUM_MONTHS; m++) {
    months.push(m);
  }
  return months;
}();
/**
 * For information about how and when to use this component,
 * [refer to its full documentation page](https://design.cms.gov/components/month-picker/).
 */
var MonthPicker = function MonthPicker(props) {
  var _props$defaultSelecte, _props$disabledMonths, _props$selectAllText, _props$clearAllText;
  var id = (0, _useId.default)('month-picker--', props.id);
  var locale = (0, _i18n.fallbackLocale)((0, _i18n.getLanguage)(), 'US');
  var months = (0, _getMonthNames.getMonthNames)(locale);
  var monthsLong = (0, _getMonthNames.getMonthNames)(locale, false);
  var isControlled = props.selectedMonths !== undefined;
  var _useState = (0, _react.useState)((_props$defaultSelecte = props.defaultSelectedMonths) !== null && _props$defaultSelecte !== void 0 ? _props$defaultSelecte : []),
    _useState2 = _slicedToArray(_useState, 2),
    selectedMonthsState = _useState2[0],
    setSelectedMonthsState = _useState2[1];
  var selectedMonths = isControlled ? props.selectedMonths : selectedMonthsState;
  var disabledMonths = (_props$disabledMonths = props.disabledMonths) !== null && _props$disabledMonths !== void 0 ? _props$disabledMonths : [];
  function handleChange(event) {
    if (props.onChange) {
      props.onChange(event);
    }
    console.log('isControlled ', isControlled);
    if (!isControlled) {
      var month = parseInt(event.target.value);
      var newSelectedMonths = selectedMonths.slice();
      if (newSelectedMonths.includes(month)) {
        newSelectedMonths.splice(newSelectedMonths.indexOf(month), 1);
      } else {
        newSelectedMonths.push(month);
      }
      setSelectedMonthsState(newSelectedMonths);
    }
  }
  function handleSelectAll() {
    if (props.onSelectAll) {
      props.onSelectAll();
    }
    if (!isControlled) {
      setSelectedMonthsState(monthNumbers.filter(function (m) {
        return !disabledMonths.includes(m);
      }));
    }
  }
  function handleClearAll() {
    if (props.onClearAll) {
      props.onClearAll();
    }
    if (!isControlled) {
      setSelectedMonthsState([]);
    }
  }
  var selectAllPressed = selectedMonths.length === _getMonthNames.NUM_MONTHS - disabledMonths.length;
  var clearAllPressed = selectedMonths.length === 0;
  var _useInlineError = (0, _useInlineError2.useInlineError)(_objectSpread(_objectSpread({}, props), {}, {
      id: id
    })),
    errorId = _useInlineError.errorId,
    topError = _useInlineError.topError,
    bottomError = _useInlineError.bottomError,
    invalid = _useInlineError.invalid;
  var _useHint = (0, _useHint2.useHint)(_objectSpread(_objectSpread({}, props), {}, {
      id: id
    })),
    hintId = _useHint.hintId,
    hintElement = _useHint.hintElement;
  var labelProps = (0, _useLabelProps.useLabelProps)(_objectSpread(_objectSpread({}, props), {}, {
    id: id
  }));
  return /*#__PURE__*/_react.default.createElement("fieldset", {
    "aria-invalid": invalid,
    "aria-describedby": (0, _describeField.default)(_objectSpread(_objectSpread({}, props), {}, {
      hintId: hintId,
      errorId: errorId
    })),
    className: (0, _classnames.default)('ds-c-fieldset', 'ds-c-month-picker', props.className)
  }, /*#__PURE__*/_react.default.createElement(_Label.Label, _extends({
    component: "legend"
  }, labelProps)), hintElement, topError, /*#__PURE__*/_react.default.createElement("div", {
    className: "ds-c-month-picker__buttons ds-u-clearfix"
  }, /*#__PURE__*/_react.default.createElement(_Button.default, {
    "aria-pressed": selectAllPressed,
    size: "small",
    className: "ds-c-month-picker__button",
    onClick: handleSelectAll,
    onDark: props.inversed,
    variation: props.buttonVariation
  }, (_props$selectAllText = props.selectAllText) !== null && _props$selectAllText !== void 0 ? _props$selectAllText : (0, _i18n.t)('monthPicker.selectAllText')), /*#__PURE__*/_react.default.createElement(_Button.default, {
    "aria-pressed": clearAllPressed,
    size: "small",
    className: "ds-c-month-picker__button",
    onClick: handleClearAll,
    onDark: props.inversed,
    variation: props.buttonVariation
  }, (_props$clearAllText = props.clearAllText) !== null && _props$clearAllText !== void 0 ? _props$clearAllText : (0, _i18n.t)('monthPicker.clearAllText'))), /*#__PURE__*/_react.default.createElement("div", {
    className: "ds-c-month-picker__months"
  }, /*#__PURE__*/_react.default.createElement("ol", {
    role: "list",
    className: "ds-c-list--bare ds-c-month-picker__months-list"
  }, months.map(function (month, i) {
    return /*#__PURE__*/_react.default.createElement("li", {
      key: month
    }, /*#__PURE__*/_react.default.createElement(_Choice.default, {
      "aria-label": monthsLong[i],
      checked: selectedMonths.includes(i + 1),
      className: "ds-c-month-picker__month",
      disabled: disabledMonths.includes(i + 1),
      inversed: props.inversed,
      onChange: handleChange,
      name: props.name,
      type: "checkbox",
      value: i + 1,
      label: month,
      id: "".concat(id, "__choice--").concat(i + 1)
    }));
  }))), bottomError);
};
exports.MonthPicker = MonthPicker;
var _default = MonthPicker;
exports.default = _default;