'use strict';

var React = require('react');
var classnames = require('classnames');
var Typography = require('./Typography-cjs.js');

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

function Switch({ value: providedValue, ariaLabel, name, disabled, onChange, }) {
    const [statefulValue, setValue] = React.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.createElement(React.Fragment, null,
        React.createElement("button", { id: name, type: "button", role: "switch", "aria-checked": value, "aria-label": ariaLabel, className: className, onClick: toggleSwitch, disabled: disabled },
            React.createElement("span", { className: styles.toggle },
                React.createElement(Label, { as: "On", disabled: disabled }),
                React.createElement("span", { className: styles.pip }),
                React.createElement(Label, { as: "Off", disabled: disabled }))),
        React.createElement("input", { name: name, type: "hidden", value: String(value) })));
}
function Label({ as, disabled }) {
    const getTextColor = () => {
        if (disabled) {
            return "grey";
        }
        else if (as === "On") {
            return "white";
        }
        return "greyBlue";
    };
    return (React.createElement("span", { className: styles.label },
        React.createElement(Typography.Typography, { element: "span", textColor: getTextColor(), size: "small", fontWeight: "bold", textCase: "uppercase" }, as)));
}

exports.Switch = Switch;
