import { __assign, __rest } from "tslib";
import React, { useState, useEffect, useContext } from 'react';
import classNames from 'classnames';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
var Switch = React.forwardRef(function (props, ref) {
    var _a;
    var _b = props.size, size = _b === void 0 ? 'md' : _b, _c = props.defaultChecked, defaultChecked = _c === void 0 ? true : _c, disabled = props.disabled, loading = props.loading, onChange = props.onChange, checkedText = props.checkedText, uncheckedText = props.uncheckedText, className = props.className, onClick = props.onClick, customisedCls = props.prefixCls, otherProps = __rest(props, ["size", "defaultChecked", "disabled", "loading", "onChange", "checkedText", "uncheckedText", "className", "onClick", "prefixCls"]);
    var _d = useState('checked' in props ? props.checked : defaultChecked), checked = _d[0], setChecked = _d[1];
    var configContext = useContext(ConfigContext);
    var prefixCls = getPrefixCls('switch', configContext.prefixCls, customisedCls);
    var switchSize = props.size || configContext.componentSize || size;
    var cls = classNames(prefixCls, className, prefixCls + "_" + switchSize, (_a = {},
        _a[prefixCls + "_checked"] = checked,
        _a[prefixCls + "_loading"] = loading,
        _a[prefixCls + "_disabled"] = loading || disabled,
        _a));
    var labelOnClick = function (e) {
        var val = !checked;
        onClick && onClick(val, e);
        if (!(disabled || loading)) {
            onChange && onChange(val, e);
            !('checked' in props) && setChecked(val);
        }
    };
    useEffect(function () {
        'checked' in props && setChecked(props.checked);
    }, [props]);
    return (React.createElement("label", __assign({}, otherProps, { ref: ref, className: cls, onClick: labelOnClick }),
        React.createElement("span", { className: prefixCls + "__bg" },
            React.createElement("span", { className: prefixCls + "__thumb" }),
            React.createElement("span", { className: prefixCls + "__label" }, checked ? checkedText : uncheckedText))));
});
Switch.displayName = 'Switch';
export default Switch;
//# sourceMappingURL=index.js.map