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 from 'react';
import { locale } from '../../lib/locale/decorators.js';
import { RenderLayer } from '../../internal/RenderLayer/index.js';
import { isNonNullable } from '../../lib/utils.js';
import { isKeyCapsLock } from '../../lib/events/keyboard/identifiers.js';
import { KeyboardEventCodes as Codes } from '../../lib/events/keyboard/KeyboardEventCodes.js';
import { Input } from '../Input/index.js';
import { CommonWrapper } from '../../internal/CommonWrapper/index.js';
import { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { rootNode } from '../../lib/rootNode/index.js';
import { withSize } from '../../lib/size/SizeDecorator.js';
import { createPropsGetter } from '../../lib/createPropsGetter.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/RenderEnvironmentDecorator.js';
import { getStyles } from './PasswordInput.styles.js';
import { PasswordInputIcon } from './PasswordInputIcon.js';
import { PasswordInputLocaleHelper } from './locale/index.js';
export var PasswordInputDataTids = {
    root: 'PasswordInput',
    capsLockDetector: 'PasswordInputCapsLockDetector',
    eyeIcon: 'PasswordInputEyeIcon',
};
/**
 * Однострочное поле для ввода пароля, в котором символы заменяются на точки.
 */
var PasswordInput = /** @class */ (function (_super) {
    __extends(PasswordInput, _super);
    function PasswordInput() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            visible: false,
            focused: false,
            capsLockEnabled: false,
        };
        _this.getProps = createPropsGetter({});
        /** Программно устанавливает фокус на поле.
         * Появляется фокусная рамка, элемент получает клавиатурные события и воспринимается как текущий элемент для чтения скринридерами.
         * @public
         */
        _this.focus = function () {
            if (_this.input) {
                _this.input.focus();
            }
        };
        /** Программно снимает фокус с кнопки.
         * @public
         */
        _this.blur = function () {
            if (_this.input) {
                _this.input.blur();
            }
        };
        _this.handleKeyPress = function (e) {
            var _a = _this.props, onKeyPress = _a.onKeyPress, detectCapsLock = _a.detectCapsLock;
            if (onKeyPress) {
                onKeyPress(e);
            }
            if (!detectCapsLock) {
                return;
            }
            var capsLockEnabled = e.getModifierState(Codes.CapsLock);
            _this.setState({ capsLockEnabled: capsLockEnabled });
        };
        _this.handleKeydown = function (e) {
            var _a = _this, _b = _a.props, detectCapsLock = _b.detectCapsLock, onKeyDown = _b.onKeyDown, capsLockEnabled = _a.state.capsLockEnabled;
            if (onKeyDown) {
                onKeyDown(e);
            }
            if (!detectCapsLock) {
                return;
            }
            if (isKeyCapsLock(e) && isNonNullable(capsLockEnabled)) {
                _this.setState({ capsLockEnabled: !capsLockEnabled });
            }
        };
        _this.handleToggleVisibility = function () {
            _this.setState(function (prevState) { return ({ visible: !prevState.visible }); }, _this.focusOnInput);
        };
        _this.focusOnInput = function () {
            if (_this.input) {
                _this.input.focus();
            }
        };
        _this.handleFocus = function (event) {
            if (_this.state.focused) {
                return;
            }
            _this.setState({ focused: true });
            if (_this.props.onFocus) {
                _this.props.onFocus(event);
            }
        };
        _this.handleFocusOutside = function () {
            _this.hideSymbols();
            if (_this.state.focused) {
                _this.setState({ focused: false });
            }
        };
        _this.renderEye = function () {
            var capsLockEnabled = _this.state.capsLockEnabled;
            return (React.createElement("span", { className: _this.styles.iconWrapper() },
                capsLockEnabled && (React.createElement("span", { className: _this.styles.capsLockDetector(), "data-tid": PasswordInputDataTids.capsLockDetector })),
                React.createElement("span", { className: _this.cx(_this.styles.toggleVisibility(_this.theme), _this.getEyeWrapperClassname()) }, !_this.props.disabled && (React.createElement("button", { type: "button", "aria-label": _this.state.visible ? _this.locale.eyeClosedAriaLabel : _this.locale.eyeOpenedAriaLabel, onClick: _this.handleToggleVisibility, className: _this.styles.icon(), "data-tid": PasswordInputDataTids.eyeIcon },
                    React.createElement(PasswordInputIcon, { size: _this.size, visible: _this.state.visible }))))));
        };
        _this.refInput = function (element) {
            _this.input = element;
        };
        _this.hideSymbols = function () {
            _this.setState({ visible: false });
        };
        _this.renderMain = function (props) {
            return (React.createElement(RenderLayer, { active: _this.state.focused, onFocusOutside: _this.handleFocusOutside, onClickOutside: _this.handleFocusOutside },
                React.createElement("div", { "data-tid": PasswordInputDataTids.root, className: _this.styles.root() },
                    React.createElement(Input, __assign({ ref: _this.refInput, type: _this.state.visible ? 'text' : 'password', onKeyDown: _this.handleKeydown, onKeyPress: _this.handleKeyPress, rightIcon: _this.renderEye(), onFocus: _this.handleFocus }, props)))));
        };
        return _this;
    }
    PasswordInput.prototype.componentDidMount = function () {
        if (this.props.detectCapsLock) {
            this.setState({ capsLockEnabled: null });
        }
    };
    PasswordInput.getDerivedStateFromProps = function (props, state) {
        if (props.disabled) {
            return { visible: false };
        }
        return state;
    };
    PasswordInput.prototype.render = function () {
        var _this = this;
        this.styles = getStyles(this.emotion);
        return (React.createElement(ThemeContext.Consumer, null, function (theme) {
            _this.theme = theme;
            return (React.createElement(CommonWrapper, __assign({ rootNodeRef: _this.setRootNode }, _this.getProps()), _this.renderMain));
        }));
    };
    PasswordInput.prototype.getEyeWrapperClassname = function () {
        switch (this.size) {
            case 'large':
                return this.styles.eyeWrapperLarge(this.theme);
            case 'medium':
                return this.styles.eyeWrapperMedium(this.theme);
            case 'small':
            default:
                return this.styles.eyeWrapperSmall(this.theme);
        }
    };
    PasswordInput.__KONTUR_REACT_UI__ = 'PasswordInput';
    PasswordInput.displayName = 'PasswordInput';
    PasswordInput = __decorate([
        withRenderEnvironment,
        rootNode,
        locale('PasswordInput', PasswordInputLocaleHelper),
        withSize
    ], PasswordInput);
    return PasswordInput;
}(React.PureComponent));
export { PasswordInput };
//# sourceMappingURL=PasswordInput.js.map