"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;exports.__esModule = true;exports.DateSelectDataTids = exports.DateSelect = void 0;var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));var _react = _interopRequireWildcard(require("react"));

var _decorator = require("../../components/ResponsiveLayout/decorator");
var _utils = require("../../lib/utils");

var _locale = require("../../components/DatePicker/locale");
var _decorators = require("../../lib/locale/decorators");

var _ThemeContext = require("../../lib/theming/ThemeContext");
var _Emotion = require("../../lib/theming/Emotion");
var _createPropsGetter = require("../../lib/createPropsGetter");

var _Select = require("../../components/Select");
var _MenuItem = require("../../components/MenuItem");
var _ArrowCollapseCVOpenIcon16Regular = require("../icons2022/ArrowCollapseCVOpenIcon/ArrowCollapseCVOpenIcon16Regular");


var _DateSelect2 = require("./DateSelect.styles");var _dec, _class, _DateSelect;

var defaultMinMonth = 0;
var defaultMaxMonth = 11;
var defaultMinYear = 1900;
var defaultMaxYear = 2099;
var months = range(defaultMinMonth, defaultMaxMonth);
var years = range(defaultMinYear, defaultMaxYear);

function range(start, end) {
  return Array(end - start + 1).
  fill(null).
  map(function (_, index) {return start + index;});
}











var DateSelectDataTids = exports.DateSelectDataTids = {
  caption: 'DateSelect__caption',
  menuItem: 'DateSelect__menuItem',
  menu: 'DateSelect__menu'
};



var DATE_SELECT_POSITIONS = [
'middle left',
'middle right',
'bottom left',
'top left',
'bottom right',
'top right'];var




DateSelect = exports.DateSelect = (_dec = (0, _decorators.locale)('Calendar', _locale.DatePickerLocaleHelper), (0, _decorator.responsiveLayout)(_class = _dec(_class = (_DateSelect = /*#__PURE__*/function (_React$PureComponent) {function DateSelect() {var _this;for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}_this = _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args)) || this;_this.








    getProps = (0, _createPropsGetter.createPropsGetter)(DateSelect.defaultProps);_this.







    selectRef = /*#__PURE__*/(0, _react.createRef)();_this.












    menuId = DateSelectDataTids.menu + (0, _utils.getRandomID)();_this.

    renderButton = function (params) {
      var _this$props = _this.props,value = _this$props.value,disabled = _this$props.disabled;
      var width = _this.getProps().width;
      var isInteractiveElement = !disabled;
      var Tag = isInteractiveElement ? 'button' : 'span';
      var rootProps = {
        className: (0, _Emotion.cx)(_DateSelect2.styles.root(_this.theme), disabled && _DateSelect2.styles.disabled()),
        style: { width: width },
        onClick: disabled ? undefined : params.onClick,
        'aria-expanded': isInteractiveElement ? params.opened : undefined,
        'aria-controls': !disabled ? _this.menuId : undefined,
        'aria-label': isInteractiveElement ?
        _this.locale.selectChosenAriaLabel + " " + (
        _this.getProps().type === 'year' ? _this.locale.selectYearAriaLabel : _this.locale.selectMonthAriaLabel) + " " +
        _this.getItem(value) :
        undefined
      };

      return /*#__PURE__*/(
        _react.default.createElement(Tag, rootProps, /*#__PURE__*/
        _react.default.createElement("div", { "data-tid": DateSelectDataTids.caption, className: _DateSelect2.styles.caption() },
        _this.getItem(value)
        ),
        isInteractiveElement && /*#__PURE__*/
        _react.default.createElement(_ArrowCollapseCVOpenIcon16Regular.ArrowCollapseCVOpenIcon16Regular, { className: (0, _Emotion.cx)(_DateSelect2.globalClasses.arrow), color: "#ADADAD" })

        ));

    };_this.
















































    getItems = function () {
      var _this$getProps = _this.getProps(),value = _this$getProps.value,onValueChange = _this$getProps.onValueChange;
      var options = _this.isYearType ? years : months;

      return options.map(function (option) {
        var isSelected = option === value;
        var isOptionDisabled = _this.isOptionDisabled(option);

        return /*#__PURE__*/(
          _react.default.createElement(_MenuItem.MenuItem, {
            scrollIntoView: isSelected,
            disabled: isOptionDisabled,
            state: isSelected ? 'selected' : null,
            onClick: function onClick() {return onValueChange(option);},
            "aria-label": "\u0412\u044B\u0431\u0440\u0430\u0442\u044C " + (_this.isYearType ? 'год' : 'месяц') + " " + _this.getItem(option),
            "data-tid": DateSelectDataTids.menuItem,
            "data-disabled": isOptionDisabled,
            key: option },

          _this.getItem(option)
          ));

      });
    };_this.

    getItem = function (value) {var _this$locale$months;
      if (_this.isYearType) {
        return value;
      }

      return (_this$locale$months = _this.locale.months) == null ? void 0 : _this$locale$months[value];
    };return _this;}(0, _inheritsLoose2.default)(DateSelect, _React$PureComponent);var _proto = DateSelect.prototype;_proto.render = function render() {var _this2 = this;return /*#__PURE__*/_react.default.createElement(_ThemeContext.ThemeContext.Consumer, null, function (theme) {_this2.theme = theme;return _this2.renderMain();});};_proto.close = function close() {var _this$selectRef$curre;(_this$selectRef$curre = this.selectRef.current) == null || _this$selectRef$curre.close();};_proto.renderMain = function renderMain() {var _this$getProps2 = this.getProps(),value = _this$getProps2.value,disabled = _this$getProps2.disabled,onValueChange = _this$getProps2.onValueChange;var theme = { btnBorderRadiusSmall: this.theme.menuItemBorderRadius };return /*#__PURE__*/_react.default.createElement(_Select.Select, { use: "text", value: value, ref: this.selectRef, theme: theme, disabled: disabled, _renderButton: this.renderButton, menuPos: "middle", positions: DATE_SELECT_POSITIONS, renderValue: this.getItem, items: this.getItems(), menuOffset: parseInt(this.theme.menuPaddingX) + parseInt(this.theme.menuItemPaddingXSmall), onValueChange: onValueChange, "aria-label": this.locale.selectChosenAriaLabel + " " + (this.isYearType ? this.locale.selectYearAriaLabel : this.locale.selectMonthAriaLabel) + " " + this.getItem(value) });};_proto.isOptionDisabled = function isOptionDisabled(option) {var _this$getProps3 = this.getProps(),minValue = _this$getProps3.minValue,maxValue = _this$getProps3.maxValue;if ((0, _utils.isNonNullable)(maxValue) && (0, _utils.isNonNullable)(minValue)) {return option > maxValue || option < minValue;}if ((0, _utils.isNonNullable)(minValue)) {return option < minValue;}if ((0, _utils.isNonNullable)(maxValue)) {return option > maxValue;}};return (0, _createClass2.default)(DateSelect, [{ key: "isYearType", get: function get() {return this.getProps().type === 'year';} }]);}(_react.default.PureComponent), _DateSelect.__KONTUR_REACT_UI__ = 'DateSelect', _DateSelect.displayName = 'DateSelect', _DateSelect.defaultProps = { type: 'year', width: 'auto' }, _DateSelect)) || _class) || _class);