"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Checkboxes_1 = require("./Checkboxes");
var theme_1 = require("../theme");
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 TreeCheckboxes = /** @class */ (function (_super) {
    tslib_1.__extends(TreeCheckboxes, _super);
    function TreeCheckboxes() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            collapsed: []
        };
        return _this;
    }
    TreeCheckboxes.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_1.Checkboxes.value2array(value, options, option2value);
        if (option.value === void 0 &&
            Array.isArray(option.children) &&
            option.children.length) {
            var someCheckedFn_1 = function (child) {
                return (Array.isArray(child.children) && child.children.length
                    ? child.children.some(someCheckedFn_1)
                    : false) ||
                    (child.value !== void 0 && ~valueArray.indexOf(child));
            };
            var someChecked_1 = option.children.some(someCheckedFn_1);
            var eachFn_1 = function (child) {
                if (Array.isArray(child.children) && child.children.length) {
                    child.children.forEach(eachFn_1);
                }
                if (child.value !== void 0) {
                    var idx = valueArray.indexOf(child);
                    ~idx && valueArray.splice(idx, 1);
                    if (!someChecked_1) {
                        valueArray.push(child);
                    }
                }
            };
            option.children.forEach(eachFn_1);
        }
        else {
            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);
    };
    TreeCheckboxes.prototype.toggleCollapsed = function (option) {
        var collapsed = this.state.collapsed.concat();
        var idx = collapsed.indexOf(option);
        if (~idx) {
            collapsed.splice(idx, 1);
        }
        else {
            collapsed.push(option);
        }
        this.setState({
            collapsed: collapsed
        });
    };
    TreeCheckboxes.prototype.renderItem = function (option, index) {
        var _this = this;
        var _a = this.props, labelClassName = _a.labelClassName, disabled = _a.disabled, cx = _a.classnames, itemClassName = _a.itemClassName, itemRender = _a.itemRender;
        var valueArray = this.valueArray;
        var partial = false;
        var checked = false;
        var hasChildren = Array.isArray(option.children) && option.children.length;
        if (option.value === void 0 && hasChildren) {
            var allchecked_1 = true;
            var partialChecked_1 = false;
            var eachFn_2 = function (child) {
                if (Array.isArray(child.children) && child.children.length) {
                    child.children.forEach(eachFn_2);
                }
                if (child.value !== void 0) {
                    var isIn = !!~valueArray.indexOf(child);
                    if (isIn && !partialChecked_1) {
                        partialChecked_1 = true;
                    }
                    else if (!isIn && allchecked_1) {
                        allchecked_1 = false;
                    }
                    checked = partialChecked_1;
                    partial = partialChecked_1 && !allchecked_1;
                }
            };
            option.children.forEach(eachFn_2);
        }
        else {
            checked = !!~valueArray.indexOf(option);
        }
        var collapsed = !!~this.state.collapsed.indexOf(option);
        return (react_1.default.createElement("div", { key: index, className: cx('TreeCheckboxes-item', disabled || option.disabled ? 'is-disabled' : '', collapsed ? 'is-collapsed' : '') },
            react_1.default.createElement("div", { className: cx('TreeCheckboxes-itemInner', itemClassName, option.className), onClick: function () { return _this.toggleOption(option); } },
                hasChildren ? (react_1.default.createElement("a", { onClick: function (e) {
                        e.stopPropagation();
                        _this.toggleCollapsed(option);
                    }, className: cx('Table-expandBtn', !collapsed ? 'is-active' : '') },
                    react_1.default.createElement("i", null))) : null,
                react_1.default.createElement("div", { className: cx('TreeCheckboxes-itemLabel') }, itemRender(option)),
                react_1.default.createElement(Checkbox_1.default, { checked: checked, partial: partial, disabled: disabled || option.disabled, labelClassName: labelClassName, description: option.description })),
            Array.isArray(option.children) && option.children.length ? (react_1.default.createElement("div", { className: cx('TreeCheckboxes-sublist') }, option.children.map(function (option, key) { return _this.renderItem(option, key); }))) : null));
    };
    TreeCheckboxes.prototype.render = function () {
        var _this = this;
        var _a = this.props, value = _a.value, options = _a.options, className = _a.className, placeholder = _a.placeholder, cx = _a.classnames, option2value = _a.option2value;
        this.valueArray = Checkboxes_1.Checkboxes.value2array(value, options, option2value);
        var body = [];
        if (Array.isArray(options) && options.length) {
            body = options.map(function (option, key) { return _this.renderItem(option, key); });
        }
        return (react_1.default.createElement("div", { className: cx('TreeCheckboxes', className) }, body && body.length ? body : react_1.default.createElement("div", null, placeholder)));
    };
    return TreeCheckboxes;
}(Checkboxes_1.Checkboxes));
exports.TreeCheckboxes = TreeCheckboxes;
exports.default = theme_1.themeable(uncontrollable_1.default(TreeCheckboxes, {
    value: 'onChange'
}));
//# sourceMappingURL=./components/TreeCheckboxes.js.map
