"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require("@angular/core");
var lodash_1 = require("lodash");
var rxjs_1 = require("rxjs");
var index_1 = require("./sorts/index");
var dataPager_service_1 = require("./paging/dataPager/dataPager.service");
var index_2 = require("./filters/index");
var index_3 = require("../cards/index");
var index_4 = require("./templates/index");
var columnHeader_template_1 = require("./templates/columnHeader.template");
var cardContainer_1 = require("./cardContainer");
var cardContainerBuilder_service_1 = require("./builder/cardContainerBuilder.service");
exports.defaultSelectionTitle = 'Select card';
var SelectableCardContainerComponent = SelectableCardContainerComponent_1 = (function (_super) {
    __extends(SelectableCardContainerComponent, _super);
    function SelectableCardContainerComponent(pager, searchFilter, sortManager) {
        var _this = _super.call(this, pager, searchFilter, sortManager) || this;
        _this.sortDirections = index_1.SortDirection;
        _this.type = cardContainerBuilder_service_1.CardContainerType.selectable;
        _this._selectionFilteredData = new rxjs_1.BehaviorSubject(null);
        _this._selectionData = new rxjs_1.BehaviorSubject(null);
        return _this;
    }
    Object.defineProperty(SelectableCardContainerComponent.prototype, "numberSelected$", {
        get: function () {
            return this.selectionFilteredData$.map(function (selectionFilteredData) {
                var selectedItems = lodash_1.filter(selectionFilteredData, function (selection) {
                    return selection.selected;
                });
                return selectedItems ? selectedItems.length : 0;
            });
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(SelectableCardContainerComponent.prototype, "selectionFilteredData$", {
        get: function () {
            return this._selectionFilteredData.asObservable();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(SelectableCardContainerComponent.prototype, "selectionData$", {
        get: function () {
            return this._selectionData.asObservable();
        },
        enumerable: true,
        configurable: true
    });
    SelectableCardContainerComponent.prototype.ngOnInit = function () {
        var _this = this;
        _super.prototype.ngOnInit.call(this);
        this.dataSource.filteredDataSet$.subscribe(function (filteredData) {
            var selectionData = _this._selectionFilteredData.getValue();
            _this._selectionFilteredData.next(_this.buildSelectionData(filteredData, selectionData));
        });
        this.dataSource.dataSet$.combineLatest(this._selectionFilteredData).subscribe(function (_a) {
            var data = _a[0], selectionFilteredData = _a[1];
            _this._selectionData.next(lodash_1.filter(selectionFilteredData, function (selection) { return lodash_1.includes(data, selection.item); }));
        });
        this.selectionColumn = {
            label: null,
            size: null,
            getValue: function (item) {
                var selection = _this.getSelection(item);
                return selection.selected;
            },
            flipSort: true,
        };
    };
    SelectableCardContainerComponent.prototype.sortSelected = function () {
        this.sort(this.selectionColumn);
    };
    SelectableCardContainerComponent.prototype.setSelected = function (selections, value) {
        var _this = this;
        this.dataSource.filteredDataSet$.take(1).subscribe(function (filteredData) {
            var updatedSelections = lodash_1.map(selections, function (selection) {
                var updated = lodash_1.clone(selection);
                updated.selected = value;
                return updated;
            });
            var selectionFilteredData = _this._selectionFilteredData.getValue();
            _this._selectionFilteredData.next(lodash_1.map(selectionFilteredData, function (oldSelection) {
                var updatedSelection = lodash_1.find(updatedSelections, function (selection) { return oldSelection.item.id === selection.item.id; });
                return updatedSelection || oldSelection;
            }));
        });
    };
    SelectableCardContainerComponent.prototype.getSelection = function (item) {
        var selectionFilteredData = this._selectionFilteredData.getValue();
        return lodash_1.find(selectionFilteredData, function (x) { return x.item.id === item.id; });
    };
    SelectableCardContainerComponent.prototype.buildSelectionData = function (data, selectionData) {
        var _this = this;
        return lodash_1.map(data, function (item) {
            var selection = lodash_1.clone(lodash_1.find(selectionData, function (x) { return x.item.id === item.id; }));
            if (!selection) {
                selection = {
                    item: item,
                    selected: false,
                    selectionTitle: '',
                };
            }
            else {
                selection.item = item;
            }
            selection = _this.setDisableSelection(selection);
            return selection;
        });
    };
    SelectableCardContainerComponent.prototype.setDisableSelection = function (selection) {
        if (this.disableSelection) {
            var disabledReason = this.disableSelection({ item: selection.item });
            selection.disabledSelection = (disabledReason != null);
            selection.selectionTitle = (selection.disabledSelection ? disabledReason : exports.defaultSelectionTitle) || '';
        }
        return selection;
    };
    return SelectableCardContainerComponent;
}(cardContainer_1.CardContainerComponent));
__decorate([
    core_1.ContentChild(index_4.ContainerHeaderTemplate),
    __metadata("design:type", index_4.ContainerHeaderTemplate)
], SelectableCardContainerComponent.prototype, "containerHeader", void 0);
__decorate([
    core_1.ContentChild(index_4.ContainerFooterTemplate),
    __metadata("design:type", index_4.ContainerFooterTemplate)
], SelectableCardContainerComponent.prototype, "containerFooter", void 0);
__decorate([
    core_1.ContentChild(index_3.CardContentTemplate),
    __metadata("design:type", index_3.CardContentTemplate)
], SelectableCardContainerComponent.prototype, "cardContent", void 0);
__decorate([
    core_1.ContentChild(index_3.CardFooterTemplate),
    __metadata("design:type", index_3.CardFooterTemplate)
], SelectableCardContainerComponent.prototype, "cardFooter", void 0);
__decorate([
    core_1.ContentChildren(index_4.ColumnContentTemplate),
    __metadata("design:type", core_1.QueryList)
], SelectableCardContainerComponent.prototype, "columnTemplates", void 0);
__decorate([
    core_1.ContentChildren(columnHeader_template_1.ColumnHeaderTemplate),
    __metadata("design:type", core_1.QueryList)
], SelectableCardContainerComponent.prototype, "columnHeaders", void 0);
SelectableCardContainerComponent = SelectableCardContainerComponent_1 = __decorate([
    core_1.Component({
        selector: 'rlSelectableCardContainer',
        template: require('./selectableCardContainer.html'),
        inputs: [
            cardContainer_1.cardContainerInputs.builder,
            cardContainer_1.cardContainerInputs.save,
        ],
        providers: [
            dataPager_service_1.DataPager,
            index_2.SearchFilter,
            index_1.SortManagerService,
            {
                provide: cardContainer_1.CardContainerComponent,
                useExisting: core_1.forwardRef(function () { return SelectableCardContainerComponent_1; }),
            },
        ],
        changeDetection: core_1.ChangeDetectionStrategy.OnPush,
    }),
    __metadata("design:paramtypes", [dataPager_service_1.DataPager, index_2.SearchFilter, index_1.SortManagerService])
], SelectableCardContainerComponent);
exports.SelectableCardContainerComponent = SelectableCardContainerComponent;
var SelectableCardContainerComponent_1;
//# sourceMappingURL=data:application/json;base64, 