"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = (0, tslib_1.__importStar)(require("react"));
var tea_component_1 = require("tea-component");
var platform_1 = require("../../../utils/platform");
var classnames_1 = (0, tslib_1.__importDefault)(require("../../../utils/classnames"));
var getLocalCounter_1 = (0, tslib_1.__importDefault)(require("../../../utils/getLocalCounter"));
var isObjectEqual_1 = (0, tslib_1.__importDefault)(require("../../../utils/isObjectEqual"));
var formcell_1 = (0, tslib_1.__importDefault)(require("../formcell"));
require("./index.css");
function Radio(_a) {
    var _b;
    var _c;
    var 
    // 系统属性
    id = _a.id, className = _a.className, style = _a.style, events = _a.events, 
    // 组件属性
    label = _a.label, labelVisible = _a.labelVisible, name = _a.name, range = _a.range, layout = _a.layout, disabled = _a.disabled, requiredFlag = _a.requiredFlag;
    var platform = (0, platform_1.usePlatform)();
    var isH5 = platform === 'h5';
    var cls = (0, classnames_1.default)((_b = {
            'weda-ui': true,
            'weda-radio': true
        },
        _b[className] = className,
        _b));
    var subCls = (0, classnames_1.default)({
        'weui-cell': isH5,
        'weui-cell_active': isH5,
        'weui-check__label': isH5,
        'weui-cell_disabled': isH5 && disabled,
    });
    var _d = (0, tslib_1.__read)(React.useState((_c = range.find(function (item) { return item.checked; })) === null || _c === void 0 ? void 0 : _c.value), 2), checkedItemValue = _d[0], setCheckedItemValue = _d[1];
    // 两次range不同时, 需要刷新checkedItemValue
    var prevRangeRef = React.useRef();
    React.useEffect(function () {
        var _a;
        if (!(0, isObjectEqual_1.default)(prevRangeRef.current, range)) {
            prevRangeRef.current = range;
            setCheckedItemValue((_a = range.find(function (item) { return item.checked; })) === null || _a === void 0 ? void 0 : _a.value);
        }
    });
    /**
     * 在ios h5端, label和input的绑定不生效
     * 导致 onChange 事件触发有问题, 所以改为监听 onClick 事件
     */
    var getChangeHandler = function (value) {
        var onChange = function (e) {
            setCheckedItemValue(value);
            events.change({ value: value }, { originEvent: e });
        };
        return onChange;
    };
    // 现在低码组件往源码组件传入的id不唯一, 暂时使用hashcode替代
    var hashcode = (0, getLocalCounter_1.default)();
    var radioElement;
    if (isH5) {
        radioElement = range.map(function (_a, index) {
            var label = _a.label, value = _a.value;
            var _id = "radio-" + hashcode + "-" + index;
            var _name = "radio-" + hashcode + "-" + name;
            return (React.createElement("label", { className: subCls, htmlFor: _id, key: _id, onClick: getChangeHandler(value) },
                React.createElement("div", { className: "weui-cell__hd" },
                    React.createElement("input", { type: "radio", className: "weui-check", value: value, name: _name, id: _id, checked: checkedItemValue === value, disabled: disabled, onClick: function (e) { return e.stopPropagation(); } }),
                    React.createElement("i", { className: "weui-icon-checked" })),
                React.createElement("div", { className: "weui-cell__bd" },
                    React.createElement("p", null, label))));
        });
    }
    else {
        // pc
        radioElement = (React.createElement(tea_component_1.ConfigProvider, { classPrefix: "wedatea2td" },
            React.createElement(tea_component_1.Radio.Group, { disabled: disabled, value: checkedItemValue, onChange: function (value) { return getChangeHandler(value)(); } }, range.map(function (_a, index) {
                var value = _a.value, label = _a.label;
                var _id = "radio-" + hashcode + "-" + index;
                return (React.createElement(tea_component_1.Radio, { key: _id, name: value, id: _id }, label));
            }))));
    }
    return (React.createElement(formcell_1.default, { id: id, className: cls, style: style, label: labelVisible ? label : null, layout: layout, multiCell: true, requiredFlag: requiredFlag }, radioElement));
}
exports.default = Radio;
//# sourceMappingURL=index.js.map