import { ElementRef, EventEmitter, Component, Inject, ViewContainerRef, Output, HostListener, Directive, TemplateRef, NgModule } from '@angular/core';
import { getSingleViewRecursive, registerElement } from '@nativescript/angular';
import { AutoCompleteViewType, RadAutoCompleteTextView, SuggestionView } from 'nativescript-ui-autocomplete';

class ListItemContext extends ElementRef {
    constructor($implicit, item, index, even, odd) {
        super(item);
        this.$implicit = $implicit;
        this.item = item;
        this.index = index;
        this.even = even;
        this.odd = odd;
    }
}
const NG_VIEW = "ng_view";
class RadAutoCompleteTextViewComponent {
    constructor(_elementRef, loader) {
        this._elementRef = _elementRef;
        this.loader = loader;
        this.setupItemView = new EventEmitter();
        this._autoCompleteTextView = _elementRef.nativeElement;
        const component = this;
        this._autoCompleteTextView.itemViewLoader = (viewType) => {
            switch (viewType) {
                case AutoCompleteViewType.ItemView:
                    if (component._itemTemplate && this.loader) {
                        const nativeItem = this.loader.createEmbeddedView(component._itemTemplate, new ListItemContext(), 0);
                        const typedView = getItemViewRoot(nativeItem);
                        typedView[NG_VIEW] = nativeItem;
                        return typedView;
                    }
                    return null;
                default:
                    return null;
            }
        };
    }
    get nativeElement() {
        return this._autoCompleteTextView;
    }
    get autoCompleteTextView() {
        return this._autoCompleteTextView;
    }
    set itemTemplate(value) {
        this._itemTemplate = value;
    }
    onItemLoading(args) {
        let index = args.index;
        let currentItem = args.data;
        const ngView = args.view[NG_VIEW];
        if (ngView) {
            this.setupViewRef(ngView, currentItem, index);
            this.detectChangesOnChild(ngView, index);
        }
    }
    setupViewRef(viewRef, data, index) {
        const context = viewRef.context;
        context.$implicit = data;
        context.item = data;
        context.index = index;
        context.even = (index % 2 === 0);
        context.odd = !context.even;
        this.setupItemView.next({ view: viewRef, data: data, index: index, context: context });
    }
    detectChangesOnChild(viewRef, index) {
        // Manually detect changes in child view ref
        // TODO: Is there a better way of getting viewRef's change detector
        const childChangeDetector = viewRef;
        childChangeDetector.markForCheck();
        childChangeDetector.detectChanges();
    }
}
RadAutoCompleteTextViewComponent.decorators = [
    { type: Component, args: [{
                selector: "RadAutoCompleteTextView",
                template: ``
            },] }
];
RadAutoCompleteTextViewComponent.ctorParameters = () => [
    { type: ElementRef, decorators: [{ type: Inject, args: [ElementRef,] }] },
    { type: ViewContainerRef, decorators: [{ type: Inject, args: [ViewContainerRef,] }] }
];
RadAutoCompleteTextViewComponent.propDecorators = {
    setupItemView: [{ type: Output }],
    onItemLoading: [{ type: HostListener, args: ["itemLoading", ['$event'],] }]
};
class TKAutoCompleteSuggestionViewDirective {
    constructor(owner, _elementRef) {
        this.owner = owner;
        this._elementRef = _elementRef;
    }
    ngOnInit() {
        this._suggestionView = this._elementRef.nativeElement;
        this.owner.autoCompleteTextView.suggestionView = this._suggestionView;
    }
    get nativeElement() {
        return this._suggestionView;
    }
    get autoCompleteTextView() {
        return this._suggestionView;
    }
}
TKAutoCompleteSuggestionViewDirective.decorators = [
    { type: Directive, args: [{
                selector: "[tkAutoCompleteSuggestionView]"
            },] }
];
TKAutoCompleteSuggestionViewDirective.ctorParameters = () => [
    { type: RadAutoCompleteTextViewComponent, decorators: [{ type: Inject, args: [RadAutoCompleteTextViewComponent,] }] },
    { type: ElementRef, decorators: [{ type: Inject, args: [ElementRef,] }] }
];
class TKSuggestionItemTemplateDirective {
    constructor(owner, template) {
        this.owner = owner;
        this.template = template;
    }
    ngOnInit() {
        this.owner.itemTemplate = this.template;
    }
}
TKSuggestionItemTemplateDirective.decorators = [
    { type: Directive, args: [{
                selector: "[tkSuggestionItemTemplate]"
            },] }
];
TKSuggestionItemTemplateDirective.ctorParameters = () => [
    { type: RadAutoCompleteTextViewComponent, decorators: [{ type: Inject, args: [RadAutoCompleteTextViewComponent,] }] },
    { type: TemplateRef, decorators: [{ type: Inject, args: [TemplateRef,] }] }
];
function getItemViewRoot(viewRef, rootLocator = getSingleViewRecursive) {
    const rootView = rootLocator(viewRef.rootNodes, 0);
    rootView.on("unloaded", () => {
        viewRef.destroy();
        delete rootView[NG_VIEW];
    });
    return rootView;
}
const AUTOCOMPLETETEXTVIEW_DIRECTIVES = [RadAutoCompleteTextViewComponent, TKAutoCompleteSuggestionViewDirective, TKSuggestionItemTemplateDirective];
registerElement("RadAutoCompleteTextView", () => RadAutoCompleteTextView);
registerElement("SuggestionView", () => SuggestionView);
class NativeScriptUIAutoCompleteTextViewModule {
}
NativeScriptUIAutoCompleteTextViewModule.decorators = [
    { type: NgModule, args: [{
                declarations: [AUTOCOMPLETETEXTVIEW_DIRECTIVES],
                exports: [AUTOCOMPLETETEXTVIEW_DIRECTIVES]
            },] }
];

/**
 * Generated bundle index. Do not edit.
 */

export { AUTOCOMPLETETEXTVIEW_DIRECTIVES, ListItemContext, NativeScriptUIAutoCompleteTextViewModule, RadAutoCompleteTextViewComponent, TKAutoCompleteSuggestionViewDirective, TKSuggestionItemTemplateDirective };
//# sourceMappingURL=nativescript-ui-autocomplete-angular.js.map
