import React__default, { useState } from 'react';
import classnames from 'classnames';
import { I as Icon } from './Icon-es.js';

var styles = {"switch":"rVg6dYYBFN4-","track":"yyISARGmmJo-","isChecked":"_40c6OIH33p8-","toggle":"zA0Fmn8IIZI-","disabled":"e-cSZjkHGw8-","pip":"PiwOCJz7Zto-","icon":"JS-CLzHa9-I-","spinning":"_0lBGF-nXMrc-"};

function Switch({ value: providedValue, ariaLabel, name, disabled, onChange, }) {
    const [statefulValue, setValue] = useState(false);
    const value = providedValue != undefined ? providedValue : statefulValue;
    const toggleSwitch = () => {
        if (disabled) {
            return;
        }
        const newValue = !value;
        onChange && onChange(newValue);
        if (providedValue == undefined) {
            setValue(newValue);
        }
    };
    const className = classnames(styles.track, styles.switch, {
        [styles.isChecked]: value,
        [styles.disabled]: disabled,
    });
    return (React__default.createElement(React__default.Fragment, null,
        React__default.createElement("button", { id: name, type: "button", role: "switch", "aria-checked": value, "aria-label": ariaLabel, className: className, onClick: toggleSwitch, disabled: disabled },
            React__default.createElement("span", { className: styles.icon, "aria-hidden": "true" },
                React__default.createElement(Icon, Object.assign({ size: "small", name: value ? "checkmark" : "cross" }, getIconColor(value, disabled !== null && disabled !== void 0 ? disabled : false)))),
            React__default.createElement("span", { className: styles.toggle })),
        React__default.createElement("input", { name: name, type: "hidden", value: String(value) })));
}
function getIconColor(isChecked, isDisabled) {
    const checkedColor = {
        color: "surface",
    };
    const uncheckedColor = {
        customColor: isDisabled
            ? "var(--color-disabled)"
            : "var(--color-inactive--onSurface)",
    };
    return isChecked ? checkedColor : uncheckedColor;
}

export { Switch as S };
