var __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "./AtomBinder"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var AtomBinder_1 = require("./AtomBinder");
    var isSelectableItem = {};
    var AtomSelectableList = /** @class */ (function () {
        function AtomSelectableList(allowMultipleSelection, valuePath, labelPath) {
            if (allowMultipleSelection === void 0) { allowMultipleSelection = false; }
            this.allowMultipleSelection = allowMultipleSelection;
            this.valuePath = valuePath;
            this.labelPath = labelPath;
            this.selectedItems = [];
            this.mValue = undefined;
            if (!this.valuePath) {
                this.valuePath = function (x) { return x; };
            }
            if (!this.labelPath) {
                this.labelPath = function (x) { return x.label || x; };
            }
            this.items = [];
        }
        Object.defineProperty(AtomSelectableList.prototype, "selectedIndex", {
            get: function () {
                if (this.selectedItems.length) {
                    return this.items.indexOf(this.selectedItems[0]);
                }
                return -1;
            },
            set: function (n) {
                this.selectedItems.clear();
                if (n === -1) {
                    return;
                }
                this.selectedItems.add(this.items[n]);
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(AtomSelectableList.prototype, "selectedItem", {
            get: function () {
                if (!this.selectedItems.length) {
                    return null;
                }
                var s = this.selectedItems[0];
                return s ? s.item : null;
            },
            set: function (item) {
                this.clearSelection();
                var si = this.items.find(function (s) { return s.item === item; });
                si.select();
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(AtomSelectableList.prototype, "label", {
            get: function () {
                var _this = this;
                var labels = this.selectedItems.map(function (x) { return _this.labelPath(x.item); });
                if (this.allowMultipleSelection) {
                    return labels;
                }
                return labels[0] || null;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(AtomSelectableList.prototype, "selectAll", {
            get: function () {
                if (this.items.length) {
                    return this.items.length === this.selectedItems.length;
                }
                return false;
            },
            set: function (v) {
                var _a;
                if (v) {
                    (_a = this.selectedItems).splice.apply(_a, __spreadArrays([0, this.selectedItems.length], this.items));
                }
                else {
                    this.selectedItems.clear();
                }
                AtomBinder_1.AtomBinder.refreshItems(this.selectedItems);
                for (var _i = 0, _b = this.items; _i < _b.length; _i++) {
                    var iterator = _b[_i];
                    AtomBinder_1.AtomBinder.refreshValue(iterator, "selected");
                }
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(AtomSelectableList.prototype, "value", {
            get: function () {
                var _this = this;
                if (this.selectedItems.length) {
                    if (this.allowMultipleSelection) {
                        return this.selectedItems.map(function (x) { return _this.valuePath(x.item); });
                    }
                    return this.valuePath(this.selectedItems[0].item);
                }
                return this.mValue;
            },
            set: function (v) {
                var _this = this;
                this.mValue = v;
                if (!this.allowMultipleSelection) {
                    v = [v];
                }
                var va = v;
                var values = this.items.filter(function (x) {
                    var vp = _this.valuePath(x.item);
                    var existing = va.find(function (y) { return y === vp; });
                    return existing ? true : false;
                });
                this.selectedItems.replace(values);
            },
            enumerable: true,
            configurable: true
        });
        AtomSelectableList.prototype.replace = function (source, start, size) {
            var _this = this;
            var values = this.value;
            if (!this.allowMultipleSelection) {
                values = [values];
            }
            var map = source.map(function (x) {
                var item = _this.newItem(x);
                if (values && values.length) {
                    var v_1 = _this.valuePath(x);
                    if (values.find(function (v1) { return v1 === v_1; })) {
                        item.selected = true;
                    }
                }
                return item;
            });
            var a = source;
            if (a.total) {
                map.total = a.total;
            }
            this.items.replace(map, start, size);
            this.mValue = undefined;
        };
        AtomSelectableList.prototype.find = function (item) {
            var itemF = function (i) { return item(i); };
            if (typeof item !== "function") {
                var e_1 = item;
                itemF = function (i) { return i === e_1; };
            }
            return this.items.find(function (i) { return itemF(i.item); });
        };
        AtomSelectableList.prototype.select = function (item) {
            var i = item;
            if (i.itemType === isSelectableItem) {
                i.select();
                return;
            }
            var si = this.items.find(function (x) { return x.item === item; });
            si.select();
        };
        AtomSelectableList.prototype.deselect = function (item) {
            var i = item;
            if (i.itemType === isSelectableItem) {
                i.deselect();
                return;
            }
            var si = this.items.find(function (x) { return x.item === item; });
            si.deselect();
        };
        AtomSelectableList.prototype.toggle = function (item) {
            var i = item;
            if (i.itemType === isSelectableItem) {
                i.toggle();
                return;
            }
            var si = this.items.find(function (x) { return x.item === item; });
            si.toggle();
        };
        AtomSelectableList.prototype.newItem = function (item) {
            var self = this;
            var newItem = {
                item: item,
                itemType: isSelectableItem,
                select: null,
                deselect: null,
                toggle: null,
                get selected() {
                    var _this = this;
                    return self.selectedItems.find(function (x) { return x === _this; }) ? true : false;
                },
                set selected(v) {
                    if (v) {
                        if (this.selected) {
                            return;
                        }
                        self.clearSelection();
                        self.selectedItems.add(this);
                    }
                    else {
                        self.selectedItems.remove(this);
                    }
                    AtomBinder_1.AtomBinder.refreshValue(self, "value");
                    AtomBinder_1.AtomBinder.refreshValue(self, "label");
                    AtomBinder_1.AtomBinder.refreshValue(this, "selectAll");
                    AtomBinder_1.AtomBinder.refreshValue(this, "selected");
                    AtomBinder_1.AtomBinder.refreshValue(this, "selectedItem");
                    AtomBinder_1.AtomBinder.refreshValue(this, "selectedIndex");
                }
            };
            newItem.select = function () {
                newItem.selected = true;
            };
            newItem.deselect = function () {
                newItem.selected = false;
            };
            newItem.toggle = function () {
                newItem.selected = !newItem.selected;
            };
            return newItem;
        };
        AtomSelectableList.prototype.clearSelection = function () {
            if (!this.allowMultipleSelection) {
                var items = this.selectedItems.map(function (s) { return s; });
                this.selectedItems.clear();
                for (var _i = 0, items_1 = items; _i < items_1.length; _i++) {
                    var iterator = items_1[_i];
                    AtomBinder_1.AtomBinder.refreshValue(iterator, "selected");
                }
            }
        };
        return AtomSelectableList;
    }());
    exports.default = AtomSelectableList;
});
//# sourceMappingURL=AtomSelectableList.js.map