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 { KeyListener } from '../../lib/events/keyListener.js';
import { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { CommonWrapper } from '../../internal/CommonWrapper/index.js';
import { rootNode } from '../../lib/rootNode/index.js';
import { createPropsGetter } from '../../lib/createPropsGetter.js';
import { isTestEnv } from '../../lib/currentEnvironment.js';
import { FocusControlWrapper } from '../../internal/FocusControlWrapper/index.js';
import { withSize } from '../../lib/size/SizeDecorator.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
import { getStyles, globalClasses } from './Toggle.styles.js';
export var ToggleDataTids = {
    root: 'Toggle__root',
};
/**
 * Тогл переключает состояния. Например, включает или отключает уведомления в настройках.
 * Состоит из надписи и переключателя.
 */
var Toggle = /** @class */ (function (_super) {
    __extends(Toggle, _super);
    function Toggle(props) {
        var _this = _super.call(this, props) || this;
        _this.getProps = createPropsGetter(Toggle_1.defaultProps);
        _this.input = null;
        /**
         * @public
         */
        _this.focus = function () {
            if (_this.input) {
                _this.keyListener.isTabPressed = true;
                _this.input.focus();
            }
        };
        _this.inputRef = function (element) {
            _this.input = element;
        };
        _this.handleChange = function (event) {
            if (_this.props.onValueChange) {
                _this.props.onValueChange(event.target.checked);
            }
            if (_this.isUncontrolled()) {
                _this.setState({
                    checked: event.target.checked,
                });
            }
            if (_this.props.onChange) {
                _this.props.onChange(event);
            }
        };
        _this.handleFocus = function (event) {
            if (_this.props.onFocus) {
                _this.props.onFocus(event);
            }
            if (_this.keyListener.isTabPressed) {
                _this.setState({ focusByTab: true });
            }
        };
        _this.resetFocus = function () { return _this.setState({ focusByTab: false }); };
        _this.handleBlur = function (event) {
            var _a, _b;
            _this.resetFocus();
            (_b = (_a = _this.props).onBlur) === null || _b === void 0 ? void 0 : _b.call(_a, event);
        };
        _this.state = {
            focusByTab: false,
            checked: props.defaultChecked,
        };
        return _this;
    }
    Toggle_1 = Toggle;
    Toggle.prototype.componentDidMount = function () {
        this.keyListener = new KeyListener(this.globalObject);
        if (this.props.autoFocus) {
            this.keyListener.isTabPressed = true;
            this.focus();
        }
    };
    Toggle.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();
        }));
    };
    Toggle.prototype.getContainerSizeClassName = function () {
        switch (this.size) {
            case 'large':
                return this.styles.containerLarge(this.theme);
            case 'medium':
                return this.styles.containerMedium(this.theme);
            case 'small':
            default:
                return this.styles.containerSmall(this.theme);
        }
    };
    Toggle.prototype.getHandleSizeClassName = function () {
        switch (this.size) {
            case 'large':
                return this.styles.handleLarge(this.theme);
            case 'medium':
                return this.styles.handleMedium(this.theme);
            case 'small':
            default:
                return this.styles.handleSmall(this.theme);
        }
    };
    Toggle.prototype.getButtonSizeClassName = function () {
        switch (this.size) {
            case 'large':
                return this.styles.buttonLarge(this.theme);
            case 'medium':
                return this.styles.buttonMedium(this.theme);
            case 'small':
            default:
                return this.styles.buttonSmall(this.theme);
        }
    };
    Toggle.prototype.getRootSizeClassName = function () {
        switch (this.size) {
            case 'large':
                return this.styles.rootLarge(this.theme);
            case 'medium':
                return this.styles.rootMedium(this.theme);
            case 'small':
            default:
                return this.styles.rootSmall(this.theme);
        }
    };
    Toggle.prototype.getInputSizeClassName = function () {
        switch (this.size) {
            case 'large':
                return this.styles.inputLarge(this.theme);
            case 'medium':
                return this.styles.inputMedium(this.theme);
            case 'small':
            default:
                return this.styles.inputSmall(this.theme);
        }
    };
    Toggle.prototype.getCaptionSizeClassName = function () {
        switch (this.size) {
            case 'large':
                return this.styles.captionLarge(this.theme);
            case 'medium':
                return this.styles.captionMedium(this.theme);
            case 'small':
            default:
                return this.styles.captionSmall(this.theme);
        }
    };
    Toggle.prototype.renderMain = function () {
        var _a, _b, _c, _d, _e;
        var _f = this.props, children = _f.children, warning = _f.warning, error = _f.error, id = _f.id, name = _f.name, ariaDescribedby = _f["aria-describedby"], ariaLabel = _f["aria-label"];
        var _g = this.getProps(), loading = _g.loading, captionPosition = _g.captionPosition, disableAnimations = _g.disableAnimations;
        var disabled = this.getProps().disabled || loading;
        var checked = this.isUncontrolled() ? this.state.checked : this.props.checked;
        var containerClassNames = this.cx(this.getContainerSizeClassName(), (_a = {},
            _a[this.styles.container(this.theme)] = true,
            _a[this.styles.containerDisabled(this.theme)] = !!disabled,
            _a[globalClasses.container] = true,
            _a[globalClasses.containerDisabled] = !!disabled,
            _a[globalClasses.containerLoading] = loading,
            _a));
        var labelClassNames = this.cx(this.getRootSizeClassName(), (_b = {},
            _b[this.styles.root(this.theme)] = true,
            _b[this.styles.rootLeft()] = captionPosition === 'left',
            _b[this.styles.disabled()] = !!disabled,
            _b[globalClasses.disabled] = !!disabled,
            _b[this.styles.disableAnimation()] = disableAnimations,
            _b));
        var caption = null;
        if (children) {
            var captionClass = this.cx(this.getCaptionSizeClassName(), (_c = {},
                _c[this.styles.caption(this.theme)] = true,
                _c[this.styles.captionLeft(this.theme)] = captionPosition === 'left',
                _c[this.styles.disabledCaption(this.theme)] = !!disabled,
                _c));
            caption = React.createElement("span", { className: captionClass }, children);
        }
        return (React.createElement(CommonWrapper, __assign({ rootNodeRef: this.setRootNode }, this.props),
            React.createElement("label", { "data-tid": ToggleDataTids.root, className: labelClassNames },
                React.createElement("div", { className: this.cx(this.getButtonSizeClassName(), (_d = {},
                        _d[this.styles.button(this.theme)] = true,
                        _d[this.styles.buttonRight()] = captionPosition === 'left',
                        _d[this.styles.isWarning(this.theme)] = !!warning,
                        _d[this.styles.isError(this.theme)] = !!error,
                        _d[this.styles.focused(this.theme)] = !disabled && !!this.state.focusByTab,
                        _d)) },
                    React.createElement(FocusControlWrapper, { onBlurWhenDisabled: this.resetFocus },
                        React.createElement("input", { type: "checkbox", checked: checked, onChange: this.handleChange, className: this.cx(this.getInputSizeClassName(), this.styles.input(this.theme)), onFocus: this.handleFocus, onBlur: this.handleBlur, ref: this.inputRef, disabled: disabled, id: id, name: name, role: "switch", "aria-label": ariaLabel, "aria-describedby": ariaDescribedby })),
                    React.createElement("div", { className: containerClassNames }),
                    React.createElement("div", { className: this.cx(this.getHandleSizeClassName(), globalClasses.handle, (_e = {},
                            _e[this.styles.handle(this.theme)] = true,
                            _e[this.styles.handleDisabled(this.theme)] = disabled,
                            _e)) })),
                caption)));
    };
    Toggle.prototype.isUncontrolled = function () {
        return this.props.checked === undefined;
    };
    var Toggle_1;
    Toggle.__KONTUR_REACT_UI__ = 'Toggle';
    Toggle.displayName = 'Toggle';
    Toggle.defaultProps = {
        disabled: false,
        loading: false,
        captionPosition: 'right',
        disableAnimations: isTestEnv,
    };
    Toggle = Toggle_1 = __decorate([
        withRenderEnvironment,
        rootNode,
        withSize
    ], Toggle);
    return Toggle;
}(React.Component));
export { Toggle };
//# sourceMappingURL=Toggle.js.map