/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');
var Spinner = require('./Spinner.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
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) {
    tslib.__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 = tslib.__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 (_J$X_("label", tslib.__assign({ className: cx("Switch", className, {
                'is-checked': isChecked,
                'is-disabled': isDisabled
            }), "data-role": "switch" }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getTestId()),
            _J$X_("input", tslib.__assign({ type: "checkbox", checked: isChecked, onChange: this.hanldeCheck, disabled: isDisabled, readOnly: readOnly }, rest)),
            _J$X_("span", { className: "text" }, isChecked ? onText : offText),
            _J$X_("span", { className: "slider" }, loading ? (_J$X_(Spinner.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["default"].PureComponent));
var Switch$1 = amisCore.themeable(Switch);

exports.Switch = Switch;
exports["default"] = Switch$1;
