/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __extends, __rest, __assign } from 'tslib';
import React__default from 'react';
import { themeable } from 'amis-core';
import { Spinner } from './Spinner.js';

/**
 * @file Switch
 * @description
 * @author fex
 */
var sizeMap = {
    sm: 'Switch--sm',
    md: 'Switch--md',
    middle: 'Switch--md',
    lg: 'Switch--lg',
    large: 'Switch--lg'
};
var levelMap = {
    info: 'bg-info',
    primary: 'bg-primary',
    danger: 'bg-danger'
};
var Switch = /** @class */ (function (_super) {
    __extends(Switch, _super);
    function Switch(props) {
        var _this = _super.call(this, props) || this;
        _this.hanldeCheck = _this.hanldeCheck.bind(_this);
        return _this;
    }
    Switch.prototype.hanldeCheck = function (e) {
        var _a = this.props, trueValue = _a.trueValue, falseValue = _a.falseValue, onChange = _a.onChange;
        if (!onChange) {
            return;
        }
        onChange(e.currentTarget.checked ? trueValue : falseValue);
    };
    Switch.prototype.render = function () {
        var _a = this.props, size = _a.size, level = _a.level, className = _a.className, classPrefix = _a.classPrefix; _a.onChange; var value = _a.value; _a.inline; var trueValue = _a.trueValue; _a.falseValue; var _b = _a.onText, onText = _b === void 0 ? '' : _b, _c = _a.offText, offText = _c === void 0 ? '' : _c, disabled = _a.disabled, readOnly = _a.readOnly, checked = _a.checked, cx = _a.classnames, loading = _a.loading, loadingConfig = _a.loadingConfig, testIdBuilder = _a.testIdBuilder, rest = __rest(_a, ["size", "level", "className", "classPrefix", "onChange", "value", "inline", "trueValue", "falseValue", "onText", "offText", "disabled", "readOnly", "checked", "classnames", "loading", "loadingConfig", "testIdBuilder"]);
        className =
            (className ? className : '') +
                (size && sizeMap[size] ? " ".concat(sizeMap[size]) : '') +
                (level && levelMap[level] ? " ".concat(levelMap[level]) : '');
        var isChecked = typeof checked !== 'undefined'
            ? checked
            : typeof value === 'undefined'
                ? false
                : value == trueValue;
        var isDisabled = disabled || loading;
        return (React__default.createElement("label", __assign({ className: cx("Switch", className, {
                'is-checked': isChecked,
                'is-disabled': isDisabled
            }), "data-role": "switch" }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getTestId()),
            React__default.createElement("input", __assign({ type: "checkbox", checked: isChecked, onChange: this.hanldeCheck, disabled: isDisabled, readOnly: readOnly }, rest)),
            React__default.createElement("span", { className: "text" }, isChecked ? onText : offText),
            React__default.createElement("span", { className: "slider" }, loading ? (React__default.createElement(Spinner, { classnames: cx, classPrefix: classPrefix, className: cx('Switch-spinner', {
                    'Switch-spinner--sm': size === 'sm',
                    'Switch-spinner--checked': isChecked
                }), spinnerClassName: cx('Switch-spinner-icon'), disabled: !isChecked, size: "sm", icon: "loading-outline", loadingConfig: loadingConfig })) : null)));
    };
    Switch.defaultProps = {
        trueValue: true,
        falseValue: false
    };
    return Switch;
}(React__default.PureComponent));
var Switch$1 = themeable(Switch);

export { Switch, Switch$1 as default };
