import * as tslib_1 from "tslib";
import { Component, Input, Output, EventEmitter, forwardRef, ElementRef, TemplateRef, ViewEncapsulation, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
export var SELECT_OPTIONS_ACCESSOR = {
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(function () { return SelectOptionsComponent; }),
    multi: true
};
var SelectOptionsComponent = /** @class */ (function () {
    function SelectOptionsComponent(cd, el) {
        this.cd = cd;
        this.el = el;
        this.data = [];
        this.styles = [];
        this.onChange = new EventEmitter();
        this.needReposition = new EventEmitter();
        this.onModelChange = function () { };
        this.onModelTouched = function () { };
        this._data = [];
    }
    SelectOptionsComponent_1 = SelectOptionsComponent;
    SelectOptionsComponent.prototype.ngOnChanges = function (simpleChanges) {
        if ('data' in simpleChanges) {
            var current = simpleChanges['data'].currentValue;
            this._data = SelectOptionsComponent_1._clone(this.data);
            this.cd.markForCheck();
        }
        // if ('value' in simpleChanges) {
        //     const currentValue = simpleChanges['value'].currentValue;
        //     if (currentValue) {
        //         const selectedData = this._data.filter(item => item.value === currentValue);
        //         if (selectedData.length) {
        //             this.onChange.emit({...selectedData[0], ...{init: true}});
        //         }
        //     }
        // }
    };
    SelectOptionsComponent.prototype.ngOnInit = function () {
        this._data = SelectOptionsComponent_1._clone(this.data);
    };
    SelectOptionsComponent.prototype.onSelectOption = function (e, data) {
        e.stopPropagation();
        if (data.children && data.children.length) {
            return;
        }
        this.value = data.value;
        this.onModelChange(data);
        this.onModelTouched();
        this.onChange.emit(data);
        this.cd.markForCheck();
    };
    SelectOptionsComponent.prototype.onSearch = function (e) {
        e.stopPropagation();
        var key = e.target.value;
        if (key) {
            var result_1 = [];
            this._data.forEach(function (item) {
                if (item.children && item.children.length) {
                    item.children.forEach(function (child) {
                        if (child.label.search(key) !== -1) {
                            result_1.push(child);
                        }
                    });
                }
                else {
                    if (item.label.search(key) !== -1) {
                        result_1.push(item);
                    }
                }
            });
            this.data = result_1;
        }
        else {
            this.data = this._data;
        }
        this.cd.markForCheck();
    };
    SelectOptionsComponent.prototype.writeValue = function (value) {
        this.value = value;
    };
    SelectOptionsComponent.prototype.registerOnChange = function (fn) {
        this.onModelChange = fn;
    };
    SelectOptionsComponent.prototype.registerOnTouched = function (fn) {
        this.onModelTouched = fn;
    };
    SelectOptionsComponent._clone = function (data) {
        return JSON.parse(JSON.stringify(data));
    };
    var SelectOptionsComponent_1;
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Array)
    ], SelectOptionsComponent.prototype, "data", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Object)
    ], SelectOptionsComponent.prototype, "value", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], SelectOptionsComponent.prototype, "searchable", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], SelectOptionsComponent.prototype, "disabled", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Array)
    ], SelectOptionsComponent.prototype, "styles", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", TemplateRef)
    ], SelectOptionsComponent.prototype, "optionTpl", void 0);
    tslib_1.__decorate([
        Output(),
        tslib_1.__metadata("design:type", EventEmitter)
    ], SelectOptionsComponent.prototype, "onChange", void 0);
    tslib_1.__decorate([
        Output(),
        tslib_1.__metadata("design:type", EventEmitter)
    ], SelectOptionsComponent.prototype, "needReposition", void 0);
    SelectOptionsComponent = SelectOptionsComponent_1 = tslib_1.__decorate([
        Component({
            selector: 'nb-select-options',
            template: "<div class=\"nb-select-options\"><div *ngIf=\"searchable\" class=\"nb-select-search\"><input nbInput type=\"text\" placeholder=\"请输入文字\" (click)=\"$event.stopPropagation();\" (input)=\"onSearch($event)\"> <i class=\"icon iconfont icon-search\" (click)=\"$event.stopPropagation();\"></i></div><div class=\"nb-select-option\" *ngFor=\"let item of data;\"><div class=\"nb-select-option-lable\" [ngClass]=\"{'active': (!item.children && item.value === value),'leaf': !item.children}\" (click)=\"onSelectOption($event, item)\" [attr.value]=\"item.value\"><ng-container *ngIf=\"optionTpl\"><ng-container *ngTemplateOutlet=\"optionTpl;context: {$implicit: item}\"></ng-container></ng-container><ng-container *ngIf=\"!optionTpl\">{{ item.label }}</ng-container></div><ng-container *ngIf=\"item.children\"><div *ngFor=\"let child of item.children\" class=\"nb-select-option-lable sub-option\" [ngClass]=\"{'active': child.value === value, 'leaf': item.children}\" (click)=\"onSelectOption($event, child)\" [attr.value]=\"child.value\"><ng-container *ngIf=\"optionTpl\"><ng-container *ngTemplateOutlet=\"optionTpl;context: {$implicit: child}\"></ng-container></ng-container><ng-container *ngIf=\"!optionTpl\">{{ child.label }}</ng-container></div></ng-container></div></div>",
            providers: [SELECT_OPTIONS_ACCESSOR],
            encapsulation: ViewEncapsulation.None,
            changeDetection: ChangeDetectionStrategy.OnPush,
            preserveWhitespaces: false
        }),
        tslib_1.__metadata("design:paramtypes", [ChangeDetectorRef,
            ElementRef])
    ], SelectOptionsComponent);
    return SelectOptionsComponent;
}());
export { SelectOptionsComponent };
//# sourceMappingURL=select.options.js.map