import { Component, Output, EventEmitter } from '@angular/core';
import { BehaviorSubject, combineLatest, from } from 'rxjs';
import { switchMap, shareReplay, debounceTime, distinctUntilChanged } from 'rxjs/operators';
import { isEqual, uniqWith, get } from 'lodash-es';
import { gettext } from '@c8y/ngx-components';
import { TranslateService } from '@ngx-translate/core';
import { DeviceProfileService } from '@c8y/ngx-components/device-profile';
import * as i0 from "@angular/core";
import * as i1 from "@c8y/ngx-components/device-profile";
import * as i2 from "@ngx-translate/core";
import * as i3 from "@c8y/ngx-components";
import * as i4 from "@angular/common";
import * as i5 from "@angular/forms";
import * as i6 from "@c8y/ngx-components/operations/stepper-frames";
export class SelectDeviceProfileStepComponent {
    constructor(deviceProfileService, translate) {
        this.deviceProfileService = deviceProfileService;
        this.translate = translate;
        this.deviceProfile = new EventEmitter();
        this.textFilter$ = new BehaviorSubject('');
        this.deviceType$ = new BehaviorSubject('');
        this.DEVICE_TYPE_NOT_DEFINED = gettext('Device type not defined');
        this.deviceTypes = [];
        this.selectedDeviceType = { name: '' };
        this.deviceProfile$ = combineLatest(this.textFilter$, this.deviceType$).pipe(switchMap(([name, deviceType]) => this.getDeviceProfiles(name, deviceType)), shareReplay(1));
        this.loadDeviceTypes();
    }
    loadDeviceTypes() {
        this.deviceTypeSubscription = this.deviceType$
            .pipe(debounceTime(300), distinctUntilChanged(), switchMap(searchStr => {
            const query = { 'c8y_Filter.type': `*${searchStr}*` };
            return from(this.deviceProfileService.getDeviceProfiles(query));
        }))
            .subscribe(({ data }) => {
            this.deviceTypes = uniqWith(data.map(val => ({ name: val.c8y_Filter.type })).filter(val => val.name), isEqual);
        });
    }
    ngOnDestroy() {
        this.deviceTypeSubscription.unsubscribe();
    }
    selectDeviceProfile(deviceProfile) {
        this.deviceProfile.emit(deviceProfile);
    }
    getDeviceTypeTitle(deviceProfile) {
        return get(deviceProfile, 'c8y_Filter.type', this.translate.instant(this.DEVICE_TYPE_NOT_DEFINED));
    }
    getDeviceProfiles(name, deviceType) {
        const query = deviceType ? { 'c8y_Filter.type': deviceType } : {};
        if (name) {
            query.name = `*${name}*`;
        }
        return this.deviceProfileService.getDeviceProfiles(query);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: SelectDeviceProfileStepComponent, deps: [{ token: i1.DeviceProfileService }, { token: i2.TranslateService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.9", type: SelectDeviceProfileStepComponent, selector: "c8y-select-device-profile-step", outputs: { deviceProfile: "deviceProfile" }, ngImport: i0, template: "<c8y-select-step-frame\n  [header]=\"'Select device profile' | translate\"\n  [noResults]=\"\n    ((textFilter$ | async)?.length || (deviceType$ | async)?.length) &&\n    (deviceProfile$ | async)?.data.length === 0\n  \"\n>\n  <div\n    class=\"row\"\n    filters\n  >\n    <div class=\"col-md-6 m-b-xs-8 m-b-sm-8\">\n      <div class=\"input-group input-group-search\">\n        <input\n          class=\"form-control\"\n          title=\"{{ 'Filter\u2026' | translate }}\"\n          placeholder=\"{{ 'Filter\u2026' | translate }}\"\n          type=\"search\"\n          [ngModel]=\"textFilter$ | async\"\n          (ngModelChange)=\"textFilter$.next($event)\"\n        />\n        <span class=\"input-group-addon\">\n          <i\n            c8yIcon=\"search\"\n            *ngIf=\"(textFilter$ | async).length === 0\"\n          ></i>\n          <i\n            class=\"text-muted\"\n            c8yIcon=\"times\"\n            *ngIf=\"(textFilter$ | async).length\"\n            (click)=\"textFilter$.next('')\"\n          ></i>\n        </span>\n      </div>\n    </div>\n    <div class=\"col-md-6 m-b-xs-8 m-b-sm-8\">\n      <c8y-form-group class=\"m-0\">\n        <c8y-typeahead\n          placeholder=\"{{ 'Type to filter device types\u2026' | translate }}\"\n          name=\"deviceType\"\n          [(ngModel)]=\"selectedDeviceType\"\n          (onSearch)=\"deviceType$.next($event)\"\n          [allowFreeEntries]=\"false\"\n        >\n          <c8y-li\n            class=\"p-l-8 p-r-8 c8y-list__item--link\"\n            (click)=\"selectedDeviceType = { name: '' }; deviceType$.next('')\"\n          >\n            <span>{{ 'All device types' | translate }}</span>\n          </c8y-li>\n          <c8y-li\n            class=\"p-l-8 p-r-8 c8y-list__item--link\"\n            *ngFor=\"let deviceType of deviceTypes\"\n            (click)=\"selectedDeviceType = deviceType; deviceType$.next(deviceType.name)\"\n            [active]=\"selectedDeviceType === deviceType\"\n          >\n            <c8y-highlight\n              [text]=\"deviceType.name\"\n              [pattern]=\"deviceType$ | async\"\n            ></c8y-highlight>\n          </c8y-li>\n        </c8y-typeahead>\n        <c8y-messages>\n          <c8y-message\n            name=\"notExisting\"\n            [text]=\"'Select one of the existing device types.' | translate\"\n          ></c8y-message>\n        </c8y-messages>\n      </c8y-form-group>\n    </div>\n  </div>\n  <c8y-li\n    class=\"hidden-sm hidden-xs m-r-8 m-l-8\"\n    header\n  >\n    <c8y-li-icon><i class=\"p-l-32 p-r-40\"></i></c8y-li-icon>\n    <c8y-li-body class=\"content-flex-40\">\n      <div class=\"col-5\">\n        <span\n          class=\"text-truncate\"\n          title=\" {{ 'Name' | translate }} \"\n        >\n          {{ 'Name' | translate }}\n        </span>\n      </div>\n      <div class=\"col-3\">\n        <span\n          class=\"text-truncate\"\n          title=\"{{ 'Device type' | translate }}\"\n        >\n          {{ 'Device type' | translate }}\n        </span>\n      </div>\n    </c8y-li-body>\n  </c8y-li>\n  <c8y-list-group list>\n    <c8y-li *c8yFor=\"let deviceProfile of deviceProfile$ | async; loadMore: 'auto'\">\n      <c8y-li-radio (onSelect)=\"selectDeviceProfile(deviceProfile)\"></c8y-li-radio>\n      <c8y-li-icon>\n        <i c8yIcon=\"c8y-device-profile\"></i>\n      </c8y-li-icon>\n      <c8y-li-body class=\"content-flex-40\">\n        <div class=\"col-5 m-b-xs-8\">\n          <div\n            class=\"text-truncate\"\n            title=\"{{ deviceProfile.name }}\"\n          >\n            {{ deviceProfile.name }}\n          </div>\n        </div>\n        <div class=\"col-3 m-b-xs-8\">\n          <div\n            class=\"text-truncate\"\n            title=\"{{ 'Device type' | translate }}: {{ getDeviceTypeTitle(deviceProfile) }}\"\n          >\n            <span\n              class=\"text-label-small m-r-8 visible-xs visible-sm\"\n              translate\n            >\n              Device type\n            </span>\n            <span *ngIf=\"deviceProfile.c8y_Filter?.type; else noType\">\n              {{ deviceProfile.c8y_Filter?.type }}\n            </span>\n            <ng-template #noType>\n              <small>\n                <em\n                  class=\"text-muted\"\n                  translate\n                >\n                  Undefined`device type`\n                </em>\n              </small>\n            </ng-template>\n          </div>\n        </div>\n      </c8y-li-body>\n    </c8y-li>\n  </c8y-list-group>\n</c8y-select-step-frame>\n", dependencies: [{ kind: "directive", type: i3.IconDirective, selector: "[c8yIcon]", inputs: ["c8yIcon"] }, { kind: "directive", type: i3.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "directive", type: i4.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i4.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i3.ForOfDirective, selector: "[c8yFor]", inputs: ["c8yForOf", "c8yForLoadMore", "c8yForPipe", "c8yForNotFound", "c8yForMaxIterations", "c8yForLoadingTemplate", "c8yForLoadNextLabel", "c8yForLoadingLabel", "c8yForRealtime", "c8yForRealtimeOptions", "c8yForComparator", "c8yForEnableVirtualScroll", "c8yForVirtualScrollElementSize", "c8yForVirtualScrollStrategy", "c8yForVirtualScrollContainerHeight"], outputs: ["c8yForCount", "c8yForChange", "c8yForLoadMoreComponent"] }, { kind: "component", type: i3.HighlightComponent, selector: "c8y-highlight", inputs: ["pattern", "text", "elementClass", "shouldTrimPattern"] }, { kind: "component", type: i3.TypeaheadComponent, selector: "c8y-typeahead", inputs: ["required", "maxlength", "disabled", "allowFreeEntries", "placeholder", "displayProperty", "icon", "name", "autoClose", "hideNew", "container", "selected", "highlightFirstItem"], outputs: ["onSearch", "onIconClick"] }, { kind: "directive", type: i5.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i5.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i5.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "component", type: i3.FormGroupComponent, selector: "c8y-form-group", inputs: ["hasError", "hasWarning", "hasSuccess", "novalidation", "status"] }, { kind: "directive", type: i3.MessageDirective, selector: "c8y-message", inputs: ["name", "text"] }, { kind: "component", type: i3.MessagesComponent, selector: "c8y-messages", inputs: ["show", "defaults", "helpMessage"] }, { kind: "component", type: i3.ListGroupComponent, selector: "c8y-list-group" }, { kind: "component", type: i3.ListItemComponent, selector: "c8y-list-item, c8y-li", inputs: ["active", "highlighted", "emptyActions", "dense", "collapsed", "selectable"], outputs: ["collapsedChange"] }, { kind: "component", type: i3.ListItemIconComponent, selector: "c8y-list-item-icon, c8y-li-icon", inputs: ["icon", "status"] }, { kind: "component", type: i3.ListItemBodyComponent, selector: "c8y-list-item-body, c8y-li-body", inputs: ["body"] }, { kind: "component", type: i3.ListItemRadioComponent, selector: "c8y-list-item-radio, c8y-li-radio", inputs: ["selected", "name", "disabled", "value"], outputs: ["onSelect"] }, { kind: "component", type: i6.SelectStepFrameComponent, selector: "c8y-select-step-frame", inputs: ["header", "noResults"] }, { kind: "pipe", type: i3.C8yTranslatePipe, name: "translate" }, { kind: "pipe", type: i4.AsyncPipe, name: "async" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: SelectDeviceProfileStepComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-select-device-profile-step', template: "<c8y-select-step-frame\n  [header]=\"'Select device profile' | translate\"\n  [noResults]=\"\n    ((textFilter$ | async)?.length || (deviceType$ | async)?.length) &&\n    (deviceProfile$ | async)?.data.length === 0\n  \"\n>\n  <div\n    class=\"row\"\n    filters\n  >\n    <div class=\"col-md-6 m-b-xs-8 m-b-sm-8\">\n      <div class=\"input-group input-group-search\">\n        <input\n          class=\"form-control\"\n          title=\"{{ 'Filter\u2026' | translate }}\"\n          placeholder=\"{{ 'Filter\u2026' | translate }}\"\n          type=\"search\"\n          [ngModel]=\"textFilter$ | async\"\n          (ngModelChange)=\"textFilter$.next($event)\"\n        />\n        <span class=\"input-group-addon\">\n          <i\n            c8yIcon=\"search\"\n            *ngIf=\"(textFilter$ | async).length === 0\"\n          ></i>\n          <i\n            class=\"text-muted\"\n            c8yIcon=\"times\"\n            *ngIf=\"(textFilter$ | async).length\"\n            (click)=\"textFilter$.next('')\"\n          ></i>\n        </span>\n      </div>\n    </div>\n    <div class=\"col-md-6 m-b-xs-8 m-b-sm-8\">\n      <c8y-form-group class=\"m-0\">\n        <c8y-typeahead\n          placeholder=\"{{ 'Type to filter device types\u2026' | translate }}\"\n          name=\"deviceType\"\n          [(ngModel)]=\"selectedDeviceType\"\n          (onSearch)=\"deviceType$.next($event)\"\n          [allowFreeEntries]=\"false\"\n        >\n          <c8y-li\n            class=\"p-l-8 p-r-8 c8y-list__item--link\"\n            (click)=\"selectedDeviceType = { name: '' }; deviceType$.next('')\"\n          >\n            <span>{{ 'All device types' | translate }}</span>\n          </c8y-li>\n          <c8y-li\n            class=\"p-l-8 p-r-8 c8y-list__item--link\"\n            *ngFor=\"let deviceType of deviceTypes\"\n            (click)=\"selectedDeviceType = deviceType; deviceType$.next(deviceType.name)\"\n            [active]=\"selectedDeviceType === deviceType\"\n          >\n            <c8y-highlight\n              [text]=\"deviceType.name\"\n              [pattern]=\"deviceType$ | async\"\n            ></c8y-highlight>\n          </c8y-li>\n        </c8y-typeahead>\n        <c8y-messages>\n          <c8y-message\n            name=\"notExisting\"\n            [text]=\"'Select one of the existing device types.' | translate\"\n          ></c8y-message>\n        </c8y-messages>\n      </c8y-form-group>\n    </div>\n  </div>\n  <c8y-li\n    class=\"hidden-sm hidden-xs m-r-8 m-l-8\"\n    header\n  >\n    <c8y-li-icon><i class=\"p-l-32 p-r-40\"></i></c8y-li-icon>\n    <c8y-li-body class=\"content-flex-40\">\n      <div class=\"col-5\">\n        <span\n          class=\"text-truncate\"\n          title=\" {{ 'Name' | translate }} \"\n        >\n          {{ 'Name' | translate }}\n        </span>\n      </div>\n      <div class=\"col-3\">\n        <span\n          class=\"text-truncate\"\n          title=\"{{ 'Device type' | translate }}\"\n        >\n          {{ 'Device type' | translate }}\n        </span>\n      </div>\n    </c8y-li-body>\n  </c8y-li>\n  <c8y-list-group list>\n    <c8y-li *c8yFor=\"let deviceProfile of deviceProfile$ | async; loadMore: 'auto'\">\n      <c8y-li-radio (onSelect)=\"selectDeviceProfile(deviceProfile)\"></c8y-li-radio>\n      <c8y-li-icon>\n        <i c8yIcon=\"c8y-device-profile\"></i>\n      </c8y-li-icon>\n      <c8y-li-body class=\"content-flex-40\">\n        <div class=\"col-5 m-b-xs-8\">\n          <div\n            class=\"text-truncate\"\n            title=\"{{ deviceProfile.name }}\"\n          >\n            {{ deviceProfile.name }}\n          </div>\n        </div>\n        <div class=\"col-3 m-b-xs-8\">\n          <div\n            class=\"text-truncate\"\n            title=\"{{ 'Device type' | translate }}: {{ getDeviceTypeTitle(deviceProfile) }}\"\n          >\n            <span\n              class=\"text-label-small m-r-8 visible-xs visible-sm\"\n              translate\n            >\n              Device type\n            </span>\n            <span *ngIf=\"deviceProfile.c8y_Filter?.type; else noType\">\n              {{ deviceProfile.c8y_Filter?.type }}\n            </span>\n            <ng-template #noType>\n              <small>\n                <em\n                  class=\"text-muted\"\n                  translate\n                >\n                  Undefined`device type`\n                </em>\n              </small>\n            </ng-template>\n          </div>\n        </div>\n      </c8y-li-body>\n    </c8y-li>\n  </c8y-list-group>\n</c8y-select-step-frame>\n" }]
        }], ctorParameters: () => [{ type: i1.DeviceProfileService }, { type: i2.TranslateService }], propDecorators: { deviceProfile: [{
                type: Output
            }] } });
//# sourceMappingURL=data:application/json;base64, 