"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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var core_1 = require("@angular/core");
var rxjs_1 = require("rxjs");
var lodash_1 = require("lodash");
var typescript_angular_utilities_1 = require("typescript-angular-utilities");
var __object = typescript_angular_utilities_1.services.object;
var __array = typescript_angular_utilities_1.services.array;
var __guid = typescript_angular_utilities_1.services.guid;
var __transform = typescript_angular_utilities_1.services.transform;
var __search = typescript_angular_utilities_1.services.search;
var validationInput_1 = require("../validationInput");
var componentValidator_service_1 = require("../../../services/componentValidator/componentValidator.service");
var form_1 = require("../../form/form");
var index_1 = require("./templates/index");
var TypeaheadListComponent = (function (_super) {
    __extends(TypeaheadListComponent, _super);
    function TypeaheadListComponent(transformService, rlForm, componentValidator, object, array, guid, searchUtility) {
        var _this = _super.call(this, rlForm, componentValidator, object, array, guid) || this;
        _this.removalButtonText = '';
        _this.searchItems = function (search) {
            return _this.loadItems(search).map(function (items) {
                return _this.filter(items, search);
            });
        };
        _this.add = function (item) {
            var action = _this.asObservable(_this.onAdd(item));
            action.subscribe(function (newItem) {
                newItem = newItem || item;
                // immutability
                var newValue = _this.value.concat([newItem]);
                _this.setValue(newValue);
                if (_this.cachedItems$) {
                    _this.array.remove(_this.cachedItemsArray, item);
                    _this.cachedItems$.next(_this.cachedItemsArray);
                }
                return newItem;
            });
            return action;
        };
        _this.remove = function (item) {
            var action = _this.asObservable(_this.onRemove(item));
            action.subscribe(function () {
                // immutability
                var newValue = _this.value.slice();
                _this.array.remove(newValue, item);
                _this.setValue(newValue);
                if (_this.cachedItems$ != null) {
                    _this.cachedItemsArray.push(item);
                    _this.cachedItems$.next(_this.cachedItemsArray);
                }
            });
            return action;
        };
        _this.transformService = transformService;
        _this.searchUtility = searchUtility;
        _this.inputType = 'typeaheadList';
        _this.value = [];
        return _this;
    }
    TypeaheadListComponent.prototype.loadItems = function (search) {
        if (this.clientSearch || this.disableSearching) {
            if (this.cachedItems$) {
                return this.cachedItems$;
            }
            else {
                return rxjs_1.Observable.from(this.getItems());
            }
        }
        else {
            return rxjs_1.Observable.from(this.getItems(search));
        }
    };
    TypeaheadListComponent.prototype.ngOnInit = function () {
        var _this = this;
        _super.prototype.ngOnInit.call(this);
        if (!this.onAdd) {
            this.onAdd = function () { return _this.immediateObservable(); };
        }
        if (!this.onRemove) {
            this.onRemove = function () { return _this.immediateObservable(); };
        }
        if (this.disableSearching) {
            this.loadCachedItems();
        }
    };
    TypeaheadListComponent.prototype.ngOnChanges = function (changes) {
        //super.ngOnChanges(<any>changes);
        if (changes.disableSearching && changes.disableSearching.currentValue && !this.cachedItems$) {
            this.loadCachedItems();
        }
    };
    TypeaheadListComponent.prototype.getDisplayName = function (item) {
        return this.transformService.getValue(item, this.transform);
    };
    TypeaheadListComponent.prototype.newItemTemplate = function () {
        return lodash_1.clone(this.listItem.template);
    };
    TypeaheadListComponent.prototype.filter = function (list, search) {
        var _this = this;
        var filteredList = lodash_1.filter(list, function (item) { return !lodash_1.find(_this.value, item); });
        if (this.clientSearch) {
            this.cachedItemsArray = filteredList;
            this.cachedItems$ = new rxjs_1.BehaviorSubject(this.cachedItemsArray);
            return lodash_1.filter(filteredList, function (item) { return _this.searchUtility.tokenizedSearch(item, search); });
        }
        else {
            return filteredList;
        }
    };
    TypeaheadListComponent.prototype.loadCachedItems = function () {
        var _this = this;
        this.searchItems().subscribe(function (items) {
            _this.cachedItemsArray = items;
            _this.cachedItems$ = new rxjs_1.BehaviorSubject(_this.cachedItemsArray);
        });
    };
    TypeaheadListComponent.prototype.asObservable = function (value) {
        if (value) {
            return rxjs_1.Observable.from(value);
        }
        else {
            return this.immediateObservable();
        }
    };
    TypeaheadListComponent.prototype.immediateObservable = function (data) {
        return new rxjs_1.BehaviorSubject(data);
    };
    return TypeaheadListComponent;
}(validationInput_1.ValidatedInputComponent));
__decorate([
    core_1.Input(),
    __metadata("design:type", Object)
], TypeaheadListComponent.prototype, "transform", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Object)
], TypeaheadListComponent.prototype, "getItems", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", String)
], TypeaheadListComponent.prototype, "prefix", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Boolean)
], TypeaheadListComponent.prototype, "clientSearch", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Boolean)
], TypeaheadListComponent.prototype, "disableSearching", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Object)
], TypeaheadListComponent.prototype, "create", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Object)
], TypeaheadListComponent.prototype, "onAdd", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Object)
], TypeaheadListComponent.prototype, "onRemove", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", Number)
], TypeaheadListComponent.prototype, "minRequired", void 0);
__decorate([
    core_1.Input(),
    __metadata("design:type", String)
], TypeaheadListComponent.prototype, "removalButtonText", void 0);
__decorate([
    core_1.ContentChild(index_1.ListHeaderTemplate),
    __metadata("design:type", index_1.ListHeaderTemplate)
], TypeaheadListComponent.prototype, "listHeader", void 0);
__decorate([
    core_1.ContentChild(index_1.ListItemTemplate),
    __metadata("design:type", index_1.ListItemTemplate)
], TypeaheadListComponent.prototype, "listItem", void 0);
TypeaheadListComponent = __decorate([
    core_1.Component({
        selector: 'rlTypeaheadList',
        template: require('./typeaheadList.html'),
        inputs: validationInput_1.validationInputs,
        outputs: validationInput_1.baseOutputs,
        providers: [componentValidator_service_1.ComponentValidator],
    }),
    __param(1, core_1.Optional()),
    __metadata("design:paramtypes", [__transform.TransformService, form_1.FormComponent,
        componentValidator_service_1.ComponentValidator, __object.ObjectUtility, __array.ArrayUtility, __guid.GuidService, __search.SearchUtility])
], TypeaheadListComponent);
exports.TypeaheadListComponent = TypeaheadListComponent;
//# sourceMappingURL=data:application/json;base64, 