"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.ToggleDataTids = exports.Toggle = void 0;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));var _react = _interopRequireDefault(require("react"));


var _keyListener = require("../../lib/events/keyListener");
var _ThemeContext = require("../../lib/theming/ThemeContext");


var _CommonWrapper = require("../../internal/CommonWrapper");
var _Emotion = require("../../lib/theming/Emotion");

var _rootNode = require("../../lib/rootNode");
var _createPropsGetter = require("../../lib/createPropsGetter");
var _currentEnvironment = require("../../lib/currentEnvironment");

var _FocusControlWrapper = require("../../internal/FocusControlWrapper");
var _SizeDecorator = require("../../lib/size/SizeDecorator");

var _Toggle2 = require("./Toggle.styles");var _class, _Toggle;





























































var ToggleDataTids = exports.ToggleDataTids = {
  root: 'Toggle__root'
};



/**
 * `Toggle` переключает состояния. Например, включает или отключает уведомления в настройках.
 * `Toggle` представляет из себя надпись + переключатель.
 *
 * `Toggle` нельзя использовать для выбора элементов в списке. Например, выбрать несколько писем.
 */var


Toggle = exports.Toggle = (0, _rootNode.rootNode)(_class = (0, _SizeDecorator.withSize)(_class = (_Toggle = /*#__PURE__*/function (_React$Component) {


















  function Toggle(props) {var _this;
    _this = _React$Component.call(this, props) || this;_this.getProps = (0, _createPropsGetter.createPropsGetter)(Toggle.defaultProps);_this.input = null;














    /**
     * @public
     */_this.
    focus = function () {
      if (_this.input) {
        _keyListener.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 (_keyListener.keyListener.isTabPressed) {
        _this.setState({ focusByTab: true });
      }
    };_this.

    resetFocus = function () {return _this.setState({ focusByTab: false });};_this.

    handleBlur = function (event) {
      _this.resetFocus();
      _this.props.onBlur == null || _this.props.onBlur(event);
    };_this.state = { focusByTab: false, checked: props.defaultChecked };return _this;}(0, _inheritsLoose2.default)(Toggle, _React$Component);var _proto = Toggle.prototype;_proto.componentDidMount = function componentDidMount() {if (this.props.autoFocus) {_keyListener.keyListener.isTabPressed = true;this.focus();}};_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.getContainerSizeClassName = function getContainerSizeClassName() {switch (this.size) {case 'large':return _Toggle2.styles.containerLarge(this.theme);case 'medium':return _Toggle2.styles.containerMedium(this.theme);case 'small':default:return _Toggle2.styles.containerSmall(this.theme);}};_proto.getHandleSizeClassName = function getHandleSizeClassName() {switch (this.size) {case 'large':return _Toggle2.styles.handleLarge(this.theme);case 'medium':return _Toggle2.styles.handleMedium(this.theme);case 'small':default:return _Toggle2.styles.handleSmall(this.theme);}};_proto.getButtonSizeClassName = function getButtonSizeClassName() {switch (this.size) {case 'large':return _Toggle2.styles.buttonLarge(this.theme);case 'medium':return _Toggle2.styles.buttonMedium(this.theme);case 'small':default:return _Toggle2.styles.buttonSmall(this.theme);}};_proto.getRootSizeClassName = function getRootSizeClassName() {switch (this.size) {case 'large':return _Toggle2.styles.rootLarge(this.theme);case 'medium':return _Toggle2.styles.rootMedium(this.theme);case 'small':default:return _Toggle2.styles.rootSmall(this.theme);}};_proto.getInputSizeClassName = function getInputSizeClassName() {switch (this.size) {case 'large':return _Toggle2.styles.inputLarge(this.theme);case 'medium':return _Toggle2.styles.inputMedium(this.theme);case 'small':default:return _Toggle2.styles.inputSmall(this.theme);}};_proto.getCaptionSizeClassName = function getCaptionSizeClassName() {switch (this.size) {case 'large':return _Toggle2.styles.captionLarge(this.theme);case 'medium':return _Toggle2.styles.captionMedium(this.theme);case 'small':default:return _Toggle2.styles.captionSmall(this.theme);}};_proto.renderMain = function renderMain() {var _cx, _cx2, _cx4, _cx5;var _this$props = this.props,children = _this$props.children,warning = _this$props.warning,error = _this$props.error,id = _this$props.id,name = _this$props.name,ariaDescribedby = _this$props['aria-describedby'],ariaLabel = _this$props['aria-label'];var _this$getProps = this.getProps(),loading = _this$getProps.loading,captionPosition = _this$getProps.captionPosition,disableAnimations = _this$getProps.disableAnimations;var disabled = this.getProps().disabled || loading;var checked = this.isUncontrolled() ? this.state.checked : this.props.checked;var containerClassNames = (0, _Emotion.cx)(this.getContainerSizeClassName(), (_cx = {}, _cx[_Toggle2.styles.container(this.theme)] = true, _cx[_Toggle2.styles.containerDisabled(this.theme)] = !!disabled, _cx[_Toggle2.globalClasses.container] = true, _cx[_Toggle2.globalClasses.containerDisabled] = !!disabled, _cx[_Toggle2.globalClasses.containerLoading] = loading, _cx));var labelClassNames = (0, _Emotion.cx)(this.getRootSizeClassName(), (_cx2 = {}, _cx2[_Toggle2.styles.root(this.theme)] = true, _cx2[_Toggle2.styles.rootLeft()] = captionPosition === 'left', _cx2[_Toggle2.styles.disabled()] = !!disabled, _cx2[_Toggle2.globalClasses.disabled] = !!disabled, _cx2[_Toggle2.styles.disableAnimation()] = disableAnimations, _cx2));var caption = null;if (children) {var _cx3;var captionClass = (0, _Emotion.cx)(this.getCaptionSizeClassName(), (_cx3 = {}, _cx3[_Toggle2.styles.caption(this.theme)] = true, _cx3[_Toggle2.styles.captionLeft(this.theme)] = captionPosition === 'left', _cx3[_Toggle2.styles.disabledCaption(this.theme)] = !!disabled, _cx3));caption = /*#__PURE__*/_react.default.createElement("span", { className: captionClass }, children);}return /*#__PURE__*/_react.default.createElement(_CommonWrapper.CommonWrapper, (0, _extends2.default)({ rootNodeRef: this.setRootNode }, this.props), /*#__PURE__*/_react.default.createElement("label", { "data-tid": ToggleDataTids.root, className: labelClassNames }, /*#__PURE__*/_react.default.createElement("div", { className: (0, _Emotion.cx)(this.getButtonSizeClassName(), (_cx4 = {}, _cx4[_Toggle2.styles.button(this.theme)] = true, _cx4[_Toggle2.styles.buttonRight()] = captionPosition === 'left', _cx4[_Toggle2.styles.isWarning(this.theme)] = !!warning, _cx4[_Toggle2.styles.isError(this.theme)] = !!error, _cx4[_Toggle2.styles.focused(this.theme)] = !disabled && !!this.state.focusByTab, _cx4)) }, /*#__PURE__*/_react.default.createElement(_FocusControlWrapper.FocusControlWrapper, { onBlurWhenDisabled: this.resetFocus }, /*#__PURE__*/_react.default.createElement("input", { type: "checkbox", checked: checked, onChange: this.handleChange, className: (0, _Emotion.cx)(this.getInputSizeClassName(), _Toggle2.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 })), /*#__PURE__*/_react.default.createElement("div", { className: containerClassNames }), /*#__PURE__*/_react.default.createElement("div", { className: (0, _Emotion.cx)(this.getHandleSizeClassName(), _Toggle2.globalClasses.handle, (_cx5 = {}, _cx5[_Toggle2.styles.handle(this.theme)] = true, _cx5[_Toggle2.styles.handleDisabled(this.theme)] = disabled, _cx5)) })), caption));};_proto.

  isUncontrolled = function isUncontrolled() {
    return this.props.checked === undefined;
  };return Toggle;}(_react.default.Component), _Toggle.__KONTUR_REACT_UI__ = 'Toggle', _Toggle.displayName = 'Toggle', _Toggle.defaultProps = { disabled: false, loading: false, captionPosition: 'right', disableAnimations: _currentEnvironment.isTestEnv }, _Toggle)) || _class) || _class;