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 _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 _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; }
import React, { useMemo, useState } from 'react';
import styled, { useTheme } from 'styled-components';
import add from 'date-fns/add';
import Input from '../../../../Input';
import { Box, Flexbox, Icon, Typography } from '../../../../..';
import { newDate } from '../../dateUtils';
var StyledInputSelect = styled(function (props) {
  return React.createElement(Input.Select, props);
}).withConfig({
  displayName: "SelectYear__StyledInputSelect",
  componentId: "sc-d2wt6c-0"
})(["> div > div > div{margin-right:-22px;margin-top:-10px;}"]);
var SelectYear = function SelectYear(_ref) {
  var _ref2;
  var id = _ref.id,
    viewedDate = _ref.viewedDate,
    onChange = _ref.onChange,
    _ref$years = _ref.years,
    years = _ref$years === void 0 ? 100 : _ref$years,
    fullscreenMode = _ref.fullscreenMode,
    _ref$selectYearLabel = _ref.selectYearLabel,
    selectYearLabel = _ref$selectYearLabel === void 0 ? 'Year' : _ref$selectYearLabel;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isHover = _useState2[0],
    setIsHover = _useState2[1];
  var futureDate = add(newDate(), {
    years: 10
  });
  var yearOptions = (_ref2 = _toConsumableArray(Array(years).keys())) === null || _ref2 === void 0 ? void 0 : _ref2.map(function (index) {
    return {
      value: futureDate.getFullYear() - index,
      label: (futureDate.getFullYear() - index).toString()
    };
  });
  var onChangeHandler = function onChangeHandler(selected) {
    if (onChange) onChange(Number(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"
        }, viewedDate.getFullYear()))), React.createElement(Flexbox, {
          item: true
        }, React.createElement(Box, {
          pt: 2,
          mr: 1
        }, icon)));
      }
    };
  }, [useSelectMachineFromContext, isHover, fullscreenMode, viewedDate]);
  var selected = yearOptions.filter(function (p) {
    return p.value === viewedDate.getFullYear();
  });
  var theme = useTheme();
  return React.createElement("div", {
    onMouseLeave: function onMouseLeave() {
      return setIsHover(false);
    },
    onMouseEnter: function onMouseEnter() {
      return setIsHover(true);
    }
  }, React.createElement(StyledInputSelect, {
    label: selectYearLabel,
    id: "".concat(id, "-select-year"),
    options: yearOptions,
    noFormField: true,
    components: components,
    onChange: onChangeHandler,
    value: selected,
    listPosition: "left",
    width: "".concat(theme.spacing.unit(25), "px"),
    fullscreenOnMobile: fullscreenMode
  }));
};
export default SelectYear;