"use strict";
/**
 * @file Checkboxes
 * @description 多选输入框
 * @author fex
 */
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importDefault(require("react"));
var uncontrollable_1 = tslib_1.__importDefault(require("uncontrollable"));
var Checkbox_1 = tslib_1.__importDefault(require("./Checkbox"));
var theme_1 = require("../theme");
var helper_1 = require("../utils/helper");
var Checkboxes = /** @class */ (function (_super) {
    tslib_1.__extends(Checkboxes, _super);
    function Checkboxes() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Checkboxes.value2array = function (value, options, option2value) {
        if (option2value === void 0) { option2value = function (option) { return option; }; }
        if (value === void 0) {
            return [];
        }
        if (!Array.isArray(value)) {
            value = [value];
        }
        return value.map(function (value) {
            var option = helper_1.findTree(options, function (option) { return option2value(option) === value; });
            return option || value;
        });
    };
    Checkboxes.prototype.toggleOption = function (option) {
        var _a = this.props, value = _a.value, onChange = _a.onChange, option2value = _a.option2value, options = _a.options;
        if (option.disabled) {
            return;
        }
        var valueArray = Checkboxes.value2array(value, options, option2value);
        var idx = valueArray.indexOf(option);
        if (~idx) {
            valueArray.splice(idx, 1);
        }
        else {
            valueArray.push(option);
        }
        var newValue = option2value
            ? valueArray.map(function (item) { return option2value(item); })
            : valueArray;
        onChange && onChange(newValue);
    };
    Checkboxes.prototype.toggleAll = function () {
        var _a = this.props, value = _a.value, onChange = _a.onChange, option2value = _a.option2value, options = _a.options;
        var valueArray = [];
        if (!Array.isArray(value) || !value.length) {
            valueArray = options.filter(function (option) { return !option.disabled; });
        }
        var newValue = option2value
            ? valueArray.map(function (item) { return option2value(item); })
            : valueArray;
        onChange && onChange(newValue);
    };
    Checkboxes.prototype.render = function () {
        var _this = this;
        var _a = this.props, value = _a.value, options = _a.options, className = _a.className, placeholder = _a.placeholder, inline = _a.inline, labelClassName = _a.labelClassName, disabled = _a.disabled, cx = _a.classnames, option2value = _a.option2value, itemClassName = _a.itemClassName, itemRender = _a.itemRender;
        var valueArray = Checkboxes.value2array(value, options, option2value);
        var body = [];
        if (Array.isArray(options) && options.length) {
            body = options.map(function (option, key) { return (react_1.default.createElement(Checkbox_1.default, { className: cx(itemClassName, option.className), key: key, onChange: function () { return _this.toggleOption(option); }, checked: !!~valueArray.indexOf(option), disabled: disabled || option.disabled, labelClassName: labelClassName, description: option.description }, itemRender(option))); });
        }
        return (react_1.default.createElement("div", { className: cx('Checkboxes', className, inline ? 'Checkboxes--inline' : '') }, body && body.length ? body : react_1.default.createElement("div", null, placeholder)));
    };
    Checkboxes.defaultProps = {
        placeholder: '暂无选项',
        itemRender: function (option) { return react_1.default.createElement("span", null, option.label); }
    };
    tslib_1.__decorate([
        helper_1.autobind,
        tslib_1.__metadata("design:type", Function),
        tslib_1.__metadata("design:paramtypes", []),
        tslib_1.__metadata("design:returntype", void 0)
    ], Checkboxes.prototype, "toggleAll", null);
    return Checkboxes;
}(react_1.default.Component));
exports.Checkboxes = Checkboxes;
exports.default = theme_1.themeable(uncontrollable_1.default(Checkboxes, {
    value: 'onChange'
}));
//# sourceMappingURL=./components/Checkboxes.js.map
