"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 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"));
var object_fromentries_1 = (0, tslib_1.__importDefault)(require("object.fromentries"));
var platform_1 = require("../../../utils/platform");
function Checkbox(_a) {
    var _b;
    var 
    // 系统属性
    className = _a.className, style = _a.style, events = _a.events, 
    // 组件属性
    name = _a.name, label = _a.label, _c = _a.labelVisible, labelVisible = _c === void 0 ? true : _c, range = _a.range, layout = _a.layout, disabled = _a.disabled, requiredFlag = _a.requiredFlag;
    var cls = (0, classnames_1.default)((_b = {
            'weda-ui': true,
            'weda-checkbox': true
        },
        _b[className] = className,
        _b));
    var platform = (0, platform_1.usePlatform)();
    var isH5 = platform === 'h5';
    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(platform === 'h5'
        ? (0, object_fromentries_1.default)(range.map(function (_a) {
            var value = _a.value, checked = _a.checked;
            return [value, !!checked];
        }))
        : range.filter(function (_a) {
            var checked = _a.checked;
            return checked;
        }).map(function (_a) {
            var value = _a.value;
            return value;
        })), 2), checkedItems = _d[0], setCheckedItems = _d[1];
    // 两次range不同时, 需要刷新checkedItems
    var prevRangeRef = React.useRef();
    React.useEffect(function () {
        if (!(0, isObjectEqual_1.default)(prevRangeRef.current, range)) {
            prevRangeRef.current = range;
            setCheckedItems((0, object_fromentries_1.default)(range.map(function (_a) {
                var value = _a.value, checked = _a.checked;
                return [value, !!checked];
            })));
        }
    });
    /**
     * 在ios h5端, label和input的绑定不生效
     * 导致 onChange 事件触发有问题, 所以改为监听 onClick 事件
     */
    var getChangeHandler = function (value) {
        var onChange = function (e) {
            var _a;
            var checked = !checkedItems[value];
            var newMap = Object.assign({}, checkedItems, (_a = {}, _a[value] = checked, _a));
            setCheckedItems(newMap);
            events.change({
                value: Object.entries(newMap)
                    .filter(function (_a) {
                    var _b = (0, tslib_1.__read)(_a, 2), checked = _b[1];
                    return checked;
                })
                    .map(function (_a) {
                    var _b = (0, tslib_1.__read)(_a, 1), value = _b[0];
                    return value;
                }),
            }, { originEvent: e });
        };
        return onChange;
    };
    // 现在低码组件往源码组件传入的id不唯一, 暂时使用hashcode替代
    var hashcode = (0, getLocalCounter_1.default)();
    var checkboxElement;
    if (platform === 'h5') {
        checkboxElement = range.map(function (_a, index) {
            var label = _a.label, value = _a.value;
            var _id = "checkbox-" + hashcode + "-" + index;
            var _name = "checkbox-" + 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: "checkbox", className: "weui-check", value: value, name: _name, id: _id, defaultChecked: checkedItems[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 {
        checkboxElement = (React.createElement(tea_component_1.ConfigProvider, { classPrefix: "wedatea2td" },
            React.createElement(tea_component_1.Checkbox.Group, { disabled: disabled, value: Object.entries(checkedItems)
                    .filter(function (_a) {
                    var _b = (0, tslib_1.__read)(_a, 2), checked = _b[1];
                    return checked;
                })
                    .map(function (_a) {
                    var _b = (0, tslib_1.__read)(_a, 1), value = _b[0];
                    return value;
                }), onChange: function (value) {
                    var newMap = range
                        .map(function (opt) { return opt.value; })
                        .reduce(function (acc, cur) {
                        var idx = value.indexOf(cur);
                        if (idx < 0) {
                            acc[cur] = false;
                        }
                        else {
                            acc[cur] = true;
                        }
                        return acc;
                    }, {});
                    setCheckedItems(newMap);
                    events.change({
                        value: Object.entries(newMap)
                            .filter(function (_a) {
                            var _b = (0, tslib_1.__read)(_a, 2), checked = _b[1];
                            return checked;
                        })
                            .map(function (_a) {
                            var _b = (0, tslib_1.__read)(_a, 1), value = _b[0];
                            return value;
                        }),
                    });
                } }, range.map(function (_a, index) {
                var value = _a.value, label = _a.label;
                var _id = "checkbox-" + hashcode + "-" + index;
                return (React.createElement(tea_component_1.Checkbox, { key: _id, name: value, id: _id }, label));
            }))));
    }
    return (React.createElement(formcell_1.default, { className: cls, style: style, label: labelVisible ? label : null, layout: layout, multiCell: true, requiredFlag: requiredFlag },
        '========================',
        checkboxElement));
}
exports.default = Checkbox;
//# sourceMappingURL=index.js.map