"use strict";
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("../../source/components/cardContainer/builder/index");
var rangeLow = 1;
var rangeHigh = 10001;
var items = lodash_1.map(lodash_1.range(rangeLow, rangeHigh), function (num) {
    return {
        id: num,
        name: 'Item' + num,
        value: num,
    };
});
var CardsSmartDataBootstrapper = (function () {
    function CardsSmartDataBootstrapper(cardContainerBuilder) {
        this.submitAsync = function (data) {
            return new Promise(function (resolve, reject) {
                setTimeout(function () {
                    console.log(data);
                    resolve();
                }, 1000);
            });
        };
        this.builder = this.setupCardContainer(cardContainerBuilder);
    }
    CardsSmartDataBootstrapper.prototype.setupCardContainer = function (cardContainerBuilder) {
        var _this = this;
        var builder = cardContainerBuilder.getInstance({
            paging: true,
            search: true,
        });
        cardContainerBuilder.buildSmartDataSource(builder, function (params) { return _this.search(params); });
        cardContainerBuilder.addColumn(builder, {
            name: 'name',
            label: 'Name',
            size: 6,
            getValue: 'name',
        });
        cardContainerBuilder.addColumn(builder, {
            name: 'value',
            label: 'Value',
            size: 6,
            getValue: 'value',
        });
        this.oddsEvensFilter = cardContainerBuilder.buildFilterGroup(builder, {
            type: 'oddsEventsFilter',
            label: 'Odds Events Filter',
            options: [
                {
                    label: 'All',
                    predicate: function () { return true; },
                    value: null,
                },
                {
                    label: 'Odd',
                    predicate: function (item) { return !!(item.value % 2); },
                    value: 'odd',
                },
                {
                    label: 'Even',
                    predicate: function (item) { return !(item.value % 2); },
                    value: 'even',
                },
            ],
        });
        this.rangeFilterGroup = cardContainerBuilder.buildRangeFilterGroup(builder, {
            type: 'rangeFilter',
            label: 'Range Filter Group',
            getValue: 'value',
            options: [
                {
                    label: 'All',
                },
                {
                    label: '5,000 - 9,800',
                    highInclusive: 9800,
                    lowInclusive: 5000,
                },
                {
                    label: '< 5,000',
                    highExclusive: 5000,
                },
                {
                    label: '> 9,800',
                    lowExclusive: 9800,
                },
            ],
        });
        this.oddsEvensFilter.serialize().skip(1).subscribe(function (value) { return console.log('odds evens filter change', value); });
        this.rangeFilterGroup.serialize().skip(1).subscribe(function (value) { return console.log('range filter change', value); });
        return builder;
    };
    CardsSmartDataBootstrapper.prototype.search = function (params) {
        var _this = this;
        console.log('Searching', params);
        var filtered = lodash_1.filter(items, function (item) {
            var inRange = _this.searchRange(item, params.filters['rangeFilter']);
            var matchesOddOrEven = _this.searchOddEven(item, params.filters['oddsEventsFilter']);
            return inRange && matchesOddOrEven;
        });
        var sorted = params.sorts.length ? lodash_1.sortBy(filtered, params.sorts[0]) : filtered;
        var paged = lodash_1.take(sorted, params.paging.pageSize);
        return rxjs_1.Observable.of({
            count: filtered.length,
            dataSet: paged,
            isEmpty: !!filtered.length,
        }).delay(1000);
    };
    CardsSmartDataBootstrapper.prototype.searchOddEven = function (item, filterValue) {
        if (!filterValue) {
            return true;
        }
        if (filterValue === 'odd') {
            return !!(item.value % 2);
        }
        return !(item.value % 2);
    };
    CardsSmartDataBootstrapper.prototype.searchRange = function (item, option) {
        if (!option) {
            return true;
        }
        var value = item.value;
        var result = true;
        if (option.highExclusive) {
            result = value < option.highExclusive;
        }
        else if (option.highInclusive) {
            result = value <= option.highInclusive;
        }
        if (option.lowExclusive) {
            result = result && value > option.lowExclusive;
        }
        else if (option.lowInclusive) {
            result = result && value >= option.lowInclusive;
        }
        return result;
    };
    return CardsSmartDataBootstrapper;
}());
CardsSmartDataBootstrapper = __decorate([
    core_1.Component({
        selector: 'tsCardsSmartDataBootstrapper',
        template: require('./cardsSmartData.html'),
    }),
    __metadata("design:paramtypes", [index_1.CardContainerBuilderService])
], CardsSmartDataBootstrapper);
exports.CardsSmartDataBootstrapper = CardsSmartDataBootstrapper;
//# sourceMappingURL=data:application/json;base64, 