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

import { __extends } from 'tslib';
import React__default from 'react';
import { columnsSplit, themeable, uncontrollable } from 'amis-core';
import Checkbox from './Checkbox.js';
import { value2array } from './Select.js';

/**
 * @file Radios
 * @description
 * @author fex
 *
 * @param 参数说明：
 * options: [
 *   {
 *      label: '显示的名字',
 *      value: '值',
 *      disabled: false
 *   }
 * ]
 *
 */
var defaultLabelRender = function (item, props) { return (React__default.createElement(React__default.Fragment, null, "".concat(item[props.labelField || 'label']))); };
var Radios = /** @class */ (function (_super) {
    __extends(Radios, _super);
    function Radios() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Radios.prototype.toggleOption = function (option) {
        var _a = this.props, value = _a.value, onChange = _a.onChange, valueField = _a.valueField, clearable = _a.clearable, delimiter = _a.delimiter, options = _a.options;
        var valueArray = value2array(value, {
            multiple: false,
            delimiter: delimiter,
            valueField: valueField,
            options: options
        });
        var idx = valueArray.indexOf(option);
        if (~idx) {
            clearable && valueArray.splice(idx, 1);
        }
        else {
            valueArray = [option];
        }
        var newValue = valueArray[0];
        onChange && onChange(newValue);
    };
    Radios.prototype.renderGroup = function (option, index, valueArray) {
        var _this = this;
        var _a = this.props, cx = _a.classnames; _a.optionType; _a.classPrefix; var _b = _a.renderLabel, renderLabel = _b === void 0 ? defaultLabelRender : _b;
        return (React__default.createElement("div", { key: index, className: cx('RadiosControl-group', option.className) },
            React__default.createElement("label", { className: cx('RadiosControl-groupLabel', option.labelClassName) }, renderLabel(option, this.props)),
            option.children && option.children.length
                ? option.children.map(function (option, index) {
                    return _this.renderItem(option, index, valueArray);
                })
                : null));
    };
    Radios.prototype.renderItem = function (option, index, valueArray) {
        var _this = this;
        var _a;
        if ((_a = option.children) === null || _a === void 0 ? void 0 : _a.length) {
            return this.renderGroup(option, index, valueArray);
        }
        var _b = this.props, disabled = _b.disabled, inline = _b.inline, itemClassName = _b.itemClassName, cx = _b.classnames, labelClassName = _b.labelClassName; _b.labelField; var optionType = _b.optionType; _b.level; _b.btnActiveLevel; _b.classPrefix; var testIdBuilder = _b.testIdBuilder, _c = _b.renderLabel, renderLabel = _c === void 0 ? defaultLabelRender : _c;
        var itemTestIdBuilder = testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild(option.value || index);
        // if (optionType === 'button') {
        //   const active = !!~valueArray.indexOf(option);
        //   return (
        //     <Button
        //       key={index}
        //       active={active}
        //       onClick={() => this.toggleOption(option)}
        //       className={cx(itemClassName, option.className)}
        //       disabled={disabled || option.disabled}
        //       level={(active ? btnActiveLevel : '') || level}
        //     >
        //       <span>{renderLabel(option, this.props)}</span>
        //     </Button>
        //   );
        // }
        return (React__default.createElement(Checkbox, { type: "radio", key: index, onChange: function () { return _this.toggleOption(option); }, checked: !!~valueArray.indexOf(option), className: cx(itemClassName, option.className), disabled: disabled || option.disabled, description: option.description, inline: inline, labelClassName: labelClassName, testIdBuilder: itemTestIdBuilder, optionType: optionType }, renderLabel(option, this.props)));
    };
    Radios.prototype.render = function () {
        var _this = this;
        var _a = this.props, value = _a.value, options = _a.options, className = _a.className, style = _a.style, cx = _a.classnames, placeholder = _a.placeholder, columnsCount = _a.columnsCount; _a.joinValues; _a.extractValue; _a.disabled; var inline = _a.inline, delimiter = _a.delimiter, valueField = _a.valueField;
        var valueArray = value2array(value, {
            multiple: false,
            delimiter: delimiter,
            valueField: valueField,
            options: options
        });
        var body = [];
        if (options) {
            body = options.map(function (option, key) {
                return _this.renderItem(option, key, valueArray);
            });
        }
        if (!inline) {
            body = columnsSplit(body, cx, columnsCount);
        }
        return (React__default.createElement("div", { className: className, style: style }, body && body.length ? body : placeholder));
    };
    Radios.defaultProps = {
        type: 'radio',
        btnActiveLevel: 'primary',
        resetValue: '',
        inline: true,
        joinValues: true,
        clearable: false,
        columnsCount: 1 // 一行显示一个
    };
    return Radios;
}(React__default.Component));
var Radios$1 = themeable(uncontrollable(Radios, {
    value: 'onChange'
}));

export { Radios, Radios$1 as default };
