/**
 * amis v3.5.1
 * build time: 2023-11-02
 * Copyright 2018-2023 baidu
 */

import { __extends, __awaiter, __generator, __decorate, __metadata } from 'tslib';
import React from 'react';
import { resolveEventData, isObject, autobind, FormItem } from 'amis-core';
import { Icon, Switch } from 'amis-ui';
import { supportStatic } from './StaticHoc.js';

var SwitchControl = /** @class */ (function (_super) {
    __extends(SwitchControl, _super);
    function SwitchControl() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    SwitchControl.prototype.handleChange = function (checked) {
        return __awaiter(this, void 0, void 0, function () {
            var _a, dispatchEvent, onChange, rendererEvent;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this.props, dispatchEvent = _a.dispatchEvent, onChange = _a.onChange;
                        return [4 /*yield*/, dispatchEvent('change', resolveEventData(this.props, { value: checked }))];
                    case 1:
                        rendererEvent = _b.sent();
                        if (rendererEvent === null || rendererEvent === void 0 ? void 0 : rendererEvent.prevented) {
                            return [2 /*return*/];
                        }
                        onChange && onChange(checked);
                        return [2 /*return*/];
                }
            });
        });
    };
    SwitchControl.prototype.getResult = function () {
        var _a = this.props, cx = _a.classnames, onText = _a.onText, offText = _a.offText;
        var on = isObject(onText) ? (React.createElement(Icon, { cx: cx, icon: onText.icon, className: "Switch-icon" })) : (onText);
        var off = isObject(offText) ? (React.createElement(Icon, { cx: cx, icon: offText.icon, className: "Switch-icon" })) : (offText);
        return { on: on, off: off };
    };
    SwitchControl.prototype.renderBody = function (children) {
        var _a = this.props, cx = _a.classnames, option = _a.option, optionAtLeft = _a.optionAtLeft;
        var Option = React.createElement("span", { className: cx('Switch-option') }, option);
        return (React.createElement(React.Fragment, null,
            optionAtLeft ? Option : null,
            children,
            optionAtLeft ? null : Option));
    };
    SwitchControl.prototype.renderStatic = function () {
        var _a = this.props, value = _a.value, trueValue = _a.trueValue;
        var _b = this.getResult(), _c = _b.on, on = _c === void 0 ? '开' : _c, _d = _b.off, off = _d === void 0 ? '关' : _d;
        var body = React.createElement("span", null, value === trueValue ? on : off);
        return this.renderBody(body);
    };
    SwitchControl.prototype.render = function () {
        var _a = this.props, size = _a.size, className = _a.className; _a.style; var ns = _a.classPrefix, cx = _a.classnames, value = _a.value, trueValue = _a.trueValue, falseValue = _a.falseValue; _a.onChange; var disabled = _a.disabled;
        var _b = this.getResult(), on = _b.on, off = _b.off;
        return (React.createElement("div", { className: cx("SwitchControl", className) }, this.renderBody(React.createElement(Switch, { classPrefix: ns, value: value, trueValue: trueValue, falseValue: falseValue, onText: on, offText: off, disabled: disabled, onChange: this.handleChange, size: size }))));
    };
    SwitchControl.defaultProps = {
        trueValue: true,
        falseValue: false,
        optionAtLeft: false
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", Promise)
    ], SwitchControl.prototype, "handleChange", null);
    __decorate([
        supportStatic(),
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], SwitchControl.prototype, "render", null);
    return SwitchControl;
}(React.Component));
/** @class */ ((function (_super) {
    __extends(SwitchControlRenderer, _super);
    function SwitchControlRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    SwitchControlRenderer = __decorate([
        FormItem({
            type: 'switch',
            sizeMutable: false
        })
    ], SwitchControlRenderer);
    return SwitchControlRenderer;
})(SwitchControl));

export { SwitchControl as default };
window.amisVersionInfo={version:'3.5.1',buildTime:'2023-11-02'};
