"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ComboStore = exports.UniqueGroup = void 0;
var mobx_state_tree_1 = require("mobx-state-tree");
var iRenderer_1 = require("./iRenderer");
var form_1 = require("./form");
var manager_1 = require("./manager");
exports.UniqueGroup = mobx_state_tree_1.types
    .model('UniqueGroup', {
    name: mobx_state_tree_1.types.identifier,
    itemsRef: mobx_state_tree_1.types.array(mobx_state_tree_1.types.string)
})
    .views(function (self) { return ({
    get items() {
        return self.itemsRef.map(function (id) { return manager_1.getStoreById(id); });
    }
}); })
    .actions(function (self) { return ({
    removeItem: function (item) {
        self.itemsRef.replace(self.itemsRef.filter(function (id) { return id !== item.id; }));
    },
    addItem: function (item) {
        self.itemsRef.push(item.id);
    }
}); });
exports.ComboStore = iRenderer_1.iRendererStore
    .named('ComboStore')
    .props({
    uniques: mobx_state_tree_1.types.map(exports.UniqueGroup),
    formsRef: mobx_state_tree_1.types.optional(mobx_state_tree_1.types.array(mobx_state_tree_1.types.string), []),
    minLength: 0,
    maxLength: 0,
    length: 0,
    activeKey: 0
})
    .views(function (self) { return ({
    get forms() {
        return self.formsRef.map(function (item) { return manager_1.getStoreById(item); });
    },
    get addable() {
        if (self.maxLength && self.length >= self.maxLength) {
            return false;
        }
        if (self.uniques.size) {
            var isFull_1 = false;
            self.uniques.forEach(function (item) {
                if (isFull_1 || !item.items.length) {
                    return;
                }
                var total = item.items[0].options.length;
                var current = item.items.reduce(function (total, item) {
                    return total + item.selectedOptions.length;
                }, 0);
                isFull_1 = total && current >= total ? true : false;
            });
            if (isFull_1) {
                return false;
            }
        }
        return true;
    },
    get removable() {
        if (self.minLength && self.minLength >= self.length) {
            return false;
        }
        return true;
    }
}); })
    .actions(function (self) {
    function config(setting) {
        typeof setting.minLength !== 'undefined' &&
            (self.minLength = parseInt(setting.minLength, 10));
        typeof setting.maxLength !== 'undefined' &&
            (self.maxLength = parseInt(setting.maxLength, 10));
        typeof setting.length !== 'undefined' && (self.length = setting.length);
    }
    function bindUniuqueItem(item) {
        if (!self.uniques.has(item.name)) {
            self.uniques.put({
                name: item.name
            });
        }
        var group = self.uniques.get(item.name);
        group.addItem(item);
    }
    function unBindUniuqueItem(item) {
        var group = self.uniques.get(item.name);
        group.removeItem(item);
        if (!group.items.length) {
            self.uniques.delete(item.name);
        }
    }
    function addForm(form) {
        self.formsRef.push(form.id);
    }
    function onChildStoreDispose(child) {
        if (child.storeType === form_1.FormStore.name) {
            var idx = self.formsRef.indexOf(child.id);
            if (~idx) {
                self.formsRef.splice(idx, 1);
                child.items.forEach(function (item) {
                    if (item.unique) {
                        unBindUniuqueItem(item);
                    }
                });
                self.forms.forEach(function (item) {
                    return item.items.forEach(function (item) { return item.unique && item.syncOptions(); });
                });
            }
        }
        self.removeChildId(child.id);
    }
    function setActiveKey(key) {
        self.activeKey = key;
    }
    return {
        config: config,
        setActiveKey: setActiveKey,
        bindUniuqueItem: bindUniuqueItem,
        unBindUniuqueItem: unBindUniuqueItem,
        addForm: addForm,
        onChildStoreDispose: onChildStoreDispose
    };
});
//# sourceMappingURL=./store/combo.js.map
