function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _to_array(arr) {
    return _array_with_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import React from 'react';
import styled, { css } from 'styled-components';
import { body1 } from '../../tokens';
import { applyDisabled, addFocus, shadows, applyEllipsis } from '../../mixins';
var triggerWidth = 'var(--plasma-switch-trigger-width, 2.75rem)';
var triggerHeight = 'var(--plasma-switch-trigger-height, 1.75rem)';
var StyledRoot = styled.label.withConfig({
    displayName: "Switch__StyledRoot",
    componentId: "sc-a32af9d8-0"
})([
    "position:relative;display:flex;align-items:center;justify-content:space-between;cursor:pointer;-webkit-tap-highlight-color:rgba(0,0,0,0);&:focus{outline:0 none;}",
    ""
], applyDisabled);
var StyledInput = styled.input.withConfig({
    displayName: "Switch__StyledInput",
    componentId: "sc-a32af9d8-1"
})([
    "position:absolute;right:0;margin:0;opacity:0;width:",
    ";height:",
    ";&:focus{outline:0 none;}"
], triggerWidth, triggerHeight);
var synthesizeFocus = function synthesizeFocus(ruleset, focused) {
    return css([
        ".focus-visible:focus > &,",
        ".focus-visible:focus ~ &{",
        ";}",
        ";"
    ], StyledInput, ruleset, focused && ruleset);
};
var StyledTrigger = styled.div.withConfig({
    displayName: "Switch__StyledTrigger",
    componentId: "sc-a32af9d8-2"
})([
    "position:relative;display:flex;flex:0 0 ",
    ";width:",
    ";height:",
    ";margin-left:auto;border-radius:0.875rem;background-color:var(--plasma-switch-trigger-background);transition:",
    ";&::after{content:'';display:block;position:absolute;top:0;left:0;bottom:0;margin:auto 0.125rem;width:1.5rem;height:1.5rem;border-radius:1.5rem;background-color:var(--plasma-switch-ellipse-background);box-shadow:",
    ";transition:",
    ";}",
    " ",
    ":checked ~ &{background-color:var(--plasma-switch-trigger-background-checked);&::after{left:auto;right:0;}}",
    ""
], triggerWidth, triggerWidth, triggerHeight, function(param) {
    var theme = param.theme;
    return theme.lowPerformance ? 'unset' : 'background-color 0.15s ease-in-out 0.1s';
}, shadows.tiny, function(param) {
    var theme = param.theme;
    return theme.lowPerformance ? 'unset' : 'width 0.15s ease-in-out, left 0.3s ease-in-out, right 0.3s ease-in-out';
}, function(param) {
    var $disabled = param.$disabled, pressed = param.pressed;
    return !$disabled && css([
        "",
        ":active &::after{width:1.875rem;}",
        ""
    ], StyledRoot, pressed && css([
        "&::after{width:1.875rem;}"
    ]));
}, StyledInput, function(param) {
    var focused = param.focused, outlined = param.outlined, theme = param.theme;
    return addFocus(_object_spread_props(_object_spread({}, theme), {
        focused: focused,
        outlined: outlined,
        outlineRadius: '1rem',
        synthesizeFocus: synthesizeFocus
    }));
});
var StyledLabel = styled.span.withConfig({
    displayName: "Switch__StyledLabel",
    componentId: "sc-a32af9d8-3"
})([
    "",
    ";margin-right:0.75rem;user-select:none;",
    ""
], body1, applyEllipsis);
/**
 * Визуальный переключатель между двумя взаимоисключающими состояниями — вкл. и выкл.
 */ // eslint-disable-next-line prefer-arrow-callback
export var Switch = /*#__PURE__*/ React.forwardRef(function Switch(_0, _1) {
    var _ref = [
        _0,
        _1
    ], _ref1 = _to_array(_ref), _ref2 = _ref1[0], _rest = _ref1.slice(1), name = _ref2.name, value = _ref2.value, label = _ref2.label, checked = _ref2.checked, defaultChecked = _ref2.defaultChecked, disabled = _ref2.disabled, readOnly = _ref2.readOnly, pressed = _ref2.pressed, focused = _ref2.focused, outlined = _ref2.outlined, tabIndex = _ref2.tabIndex, onChange = _ref2.onChange, onFocus = _ref2.onFocus, onBlur = _ref2.onBlur, rest = _object_without_properties(_ref2, [
        "name",
        "value",
        "label",
        "checked",
        "defaultChecked",
        "disabled",
        "readOnly",
        "pressed",
        "focused",
        "outlined",
        "tabIndex",
        "onChange",
        "onFocus",
        "onBlur"
    ]), _rest1 = _sliced_to_array(_rest, 1), ref = _rest1[0];
    var exactChecked = Boolean(checked !== undefined ? checked : defaultChecked);
    return /*#__PURE__*/ React.createElement(StyledRoot, _object_spread({
        $disabled: disabled
    }, rest), /*#__PURE__*/ React.createElement(StyledInput, {
        ref: ref,
        role: "switch",
        "aria-checked": exactChecked,
        type: "checkbox",
        name: name,
        value: value,
        checked: checked,
        defaultChecked: defaultChecked,
        readOnly: readOnly,
        disabled: disabled,
        tabIndex: tabIndex,
        onChange: onChange,
        onFocus: onFocus,
        onBlur: onBlur
    }), label && /*#__PURE__*/ React.createElement(StyledLabel, {
        tabIndex: -1
    }, label), /*#__PURE__*/ React.createElement(StyledTrigger, {
        "aria-hidden": true,
        $disabled: disabled,
        pressed: pressed,
        focused: focused,
        outlined: outlined
    }));
});
