"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 = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireWildcard(require("styled-components"));
var _add = _interopRequireDefault(require("date-fns/add"));
var _Input = _interopRequireDefault(require("../../../../Input"));
var _ = require("../../../../..");
var _dateUtils = require("../../dateUtils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 _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; }
var StyledInputSelect = (0, _styledComponents.default)(function (props) {
  return _react.default.createElement(_Input.default.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 = (0, _react.useState)(false),
    _useState2 = _slicedToArray(_useState, 2),
    isHover = _useState2[0],
    setIsHover = _useState2[1];
  var futureDate = (0, _add.default)((0, _dateUtils.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.default.Select.useSelectMachineFromContext;
  var components = (0, _react.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.default.createElement(_.Icon.ChevronUp8, {
            color: function color(t) {
              return t.colorTokens.neutral.icon_default;
            }
          });
        } else if (isHover || fullscreenMode) {
          icon = _react.default.createElement(_.Icon.ChevronDown8, {
            color: function color(t) {
              return t.colorTokens.action.background_default;
            }
          });
        } else {
          icon = _react.default.createElement(_.Box, {
            px: 1
          });
        }
        return _react.default.createElement(_.Flexbox, {
          container: true
        }, _react.default.createElement(_.Flexbox, {
          item: true
        }, _react.default.createElement(_.Box, {
          pr: 1
        }, _react.default.createElement(_.Typography, {
          type: "primary",
          weight: "semibold"
        }, viewedDate.getFullYear()))), _react.default.createElement(_.Flexbox, {
          item: true
        }, _react.default.createElement(_.Box, {
          pt: 2,
          mr: 1
        }, icon)));
      }
    };
  }, [useSelectMachineFromContext, isHover, fullscreenMode, viewedDate]);
  var selected = yearOptions.filter(function (p) {
    return p.value === viewedDate.getFullYear();
  });
  var theme = (0, _styledComponents.useTheme)();
  return _react.default.createElement("div", {
    onMouseLeave: function onMouseLeave() {
      return setIsHover(false);
    },
    onMouseEnter: function onMouseEnter() {
      return setIsHover(true);
    }
  }, _react.default.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
  }));
};
var _default = exports.default = SelectYear;