var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import React, { createRef } from 'react';
import { responsiveLayout } from '../../components/ResponsiveLayout/decorator.js';
import { getRandomID, isNonNullable } from '../../lib/utils.js';
import { DatePickerLocaleHelper } from '../../components/DatePicker/locale/index.js';
import { locale } from '../../lib/locale/decorators.js';
import { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { createPropsGetter } from '../../lib/createPropsGetter.js';
import { Select } from '../../components/Select/index.js';
import { MenuItem } from '../../components/MenuItem/index.js';
import { ArrowCollapseCVOpenIcon16Regular } from '../icons2022/ArrowCollapseCVOpenIcon/ArrowCollapseCVOpenIcon16Regular.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
import { globalClasses, getStyles } from './DateSelect.styles.js';
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; });
}
export var 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 = /** @class */ (function (_super) {
    __extends(DateSelect, _super);
    function DateSelect() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.getProps = createPropsGetter(DateSelect_1.defaultProps);
        _this.selectRef = createRef();
        _this.menuId = DateSelectDataTids.menu + getRandomID();
        _this.renderButton = function (params) {
            var _a = _this.props, value = _a.value, disabled = _a.disabled;
            var width = _this.getProps().width;
            var isInteractiveElement = !disabled;
            var Tag = isInteractiveElement ? 'button' : 'span';
            var rootProps = {
                className: _this.cx(_this.styles.root(_this.theme), disabled && _this.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
                    ? "".concat(_this.locale.selectChosenAriaLabel, " ").concat(_this.getProps().type === 'year' ? _this.locale.selectYearAriaLabel : _this.locale.selectMonthAriaLabel, " ").concat(_this.getItem(value))
                    : undefined,
            };
            return (React.createElement(Tag, __assign({}, rootProps),
                React.createElement("div", { "data-tid": DateSelectDataTids.caption, className: _this.styles.caption() }, _this.getItem(value)),
                isInteractiveElement && (React.createElement(ArrowCollapseCVOpenIcon16Regular, { className: _this.cx(globalClasses.arrow), color: "#ADADAD" }))));
        };
        _this.getItems = function () {
            var _a = _this.getProps(), value = _a.value, onValueChange = _a.onValueChange;
            var options = _this.isYearType ? years : months;
            return options.map(function (option) {
                var isSelected = option === value;
                var isOptionDisabled = _this.isOptionDisabled(option);
                return (React.createElement(MenuItem, { scrollIntoView: isSelected, disabled: isOptionDisabled, state: isSelected ? 'selected' : null, onClick: function () { return onValueChange(option); }, "aria-label": "\u0412\u044B\u0431\u0440\u0430\u0442\u044C ".concat(_this.isYearType ? 'год' : 'месяц', " ").concat(_this.getItem(option)), "data-tid": DateSelectDataTids.menuItem, "data-disabled": isOptionDisabled, key: option }, _this.getItem(option)));
            });
        };
        _this.getItem = function (value) {
            var _a;
            if (_this.isYearType) {
                return value;
            }
            return (_a = _this.locale.months) === null || _a === void 0 ? void 0 : _a[value];
        };
        return _this;
    }
    DateSelect_1 = DateSelect;
    Object.defineProperty(DateSelect.prototype, "isYearType", {
        get: function () {
            return this.getProps().type === 'year';
        },
        enumerable: false,
        configurable: true
    });
    DateSelect.prototype.render = function () {
        var _this = this;
        this.styles = getStyles(this.emotion);
        return (React.createElement(ThemeContext.Consumer, null, function (theme) {
            _this.theme = theme;
            return _this.renderMain();
        }));
    };
    DateSelect.prototype.close = function () {
        var _a;
        (_a = this.selectRef.current) === null || _a === void 0 ? void 0 : _a.close();
    };
    DateSelect.prototype.renderMain = function () {
        var _a = this.getProps(), value = _a.value, disabled = _a.disabled, onValueChange = _a.onValueChange;
        var theme = {
            btnBorderRadiusSmall: this.theme.menuItemBorderRadius,
        };
        return (React.createElement(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": "".concat(this.locale.selectChosenAriaLabel, " ").concat(this.isYearType ? this.locale.selectYearAriaLabel : this.locale.selectMonthAriaLabel, " ").concat(this.getItem(value)) }));
    };
    DateSelect.prototype.isOptionDisabled = function (option) {
        var _a = this.getProps(), minValue = _a.minValue, maxValue = _a.maxValue;
        if (isNonNullable(maxValue) && isNonNullable(minValue)) {
            return option > maxValue || option < minValue;
        }
        if (isNonNullable(minValue)) {
            return option < minValue;
        }
        if (isNonNullable(maxValue)) {
            return option > maxValue;
        }
    };
    var DateSelect_1;
    DateSelect.__KONTUR_REACT_UI__ = 'DateSelect';
    DateSelect.displayName = 'DateSelect';
    DateSelect.defaultProps = {
        type: 'year',
        width: 'auto',
    };
    DateSelect = DateSelect_1 = __decorate([
        withRenderEnvironment,
        responsiveLayout,
        locale('Calendar', DatePickerLocaleHelper)
    ], DateSelect);
    return DateSelect;
}(React.PureComponent));
export { DateSelect };
//# sourceMappingURL=DateSelect.js.map