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(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; }
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; }
import React, { useMemo, useState } from 'react';
import format from 'date-fns/format';
import styled, { useTheme } from 'styled-components';
import { getLocale } from '../../dateUtils';
import Input from '../../../../Input';
import { Box, Flexbox, Icon, Typography } from '../../../../..';
import { capitalize } from '../../textUtils';
var months = _toConsumableArray(Array(12).keys());
var StyledInputSelect = styled(function (props) {
  return React.createElement(Input.Select, props);
}).withConfig({
  displayName: "SelectMonth__StyledInputSelect",
  componentId: "sc-1aq4kym-0"
})(["margin-right:", "px;> div > div > div{margin-right:-22px;margin-top:-10px;}"], function (_ref) {
  var theme = _ref.theme;
  return theme.spacing.unit(3);
});
var SelectMonth = function SelectMonth(_ref2) {
  var id = _ref2.id,
    locale = _ref2.locale,
    viewedDate = _ref2.viewedDate,
    onChange = _ref2.onChange,
    fullscreenMode = _ref2.fullscreenMode,
    _ref2$selectMonthLabe = _ref2.selectMonthLabel,
    selectMonthLabel = _ref2$selectMonthLabe === void 0 ? 'Month' : _ref2$selectMonthLabe;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isHover = _useState2[0],
    setIsHover = _useState2[1];
  var monthOptions = months === null || months === void 0 ? void 0 : months.map(function (index) {
    return {
      value: index,
      label: capitalize(format(new Date(viewedDate.getFullYear(), index), 'MMMM', {
        locale: getLocale(locale)
      }))
    };
  });
  var onChangeHandler = function onChangeHandler(selected) {
    if (onChange) onChange(selected[0].value);
  };
  var useSelectMachineFromContext = Input.Select.useSelectMachineFromContext;
  var components = useMemo(function () {
    return {
      SelectedValue: function SelectedValue() {
        var _useSelectMachineFrom = useSelectMachineFromContext(),
          _useSelectMachineFrom2 = _slicedToArray(_useSelectMachineFrom, 1),
          state = _useSelectMachineFrom2[0];
        var icon = null;
        if (state.value.open === 'on') {
          icon = React.createElement(Icon.ChevronUp8, {
            color: function color(t) {
              return t.colorTokens.neutral.icon_default;
            }
          });
        } else if (isHover || fullscreenMode) {
          icon = React.createElement(Icon.ChevronDown8, {
            color: function color(t) {
              return t.colorTokens.action.background_default;
            }
          });
        } else {
          icon = React.createElement(Box, {
            px: 1
          });
        }
        return React.createElement(Flexbox, {
          container: true
        }, React.createElement(Flexbox, {
          item: true
        }, React.createElement(Box, {
          pr: 1
        }, React.createElement(Typography, {
          type: "primary",
          weight: "semibold"
        }, capitalize(format(viewedDate, 'MMMM', {
          locale: getLocale(locale)
        }))))), React.createElement(Flexbox, {
          item: true
        }, React.createElement(Box, {
          pt: 2,
          mr: 1
        }, icon)));
      }
    };
  }, [useSelectMachineFromContext, isHover, fullscreenMode, viewedDate, locale]);
  var selected = monthOptions.filter(function (p) {
    return p.value === viewedDate.getMonth();
  });
  var theme = useTheme();
  return React.createElement("div", {
    onMouseLeave: function onMouseLeave() {
      return setIsHover(false);
    },
    onMouseEnter: function onMouseEnter() {
      return setIsHover(true);
    }
  }, React.createElement(StyledInputSelect, {
    label: selectMonthLabel,
    id: "".concat(id, "-select-month"),
    options: monthOptions,
    noFormField: true,
    components: components,
    onChange: onChangeHandler,
    value: selected,
    listPosition: "left",
    width: "".concat(theme.spacing.unit(35), "px"),
    fullscreenOnMobile: fullscreenMode
  }));
};
export default SelectMonth;