import { Component, EventEmitter, Input, Output } from '@angular/core';
import { gettext } from '@c8y/ngx-components';
import { PRODUCT_EXPERIENCE_REPOSITORY_SHARED } from '@c8y/ngx-components/repository/shared';
import { filter, get } from 'lodash-es';
import { BehaviorSubject, combineLatest, Observable, of, Subject } from 'rxjs';
import { debounceTime, distinctUntilChanged, map, share, switchMap, takeUntil, tap } from 'rxjs/operators';
import { DeviceSoftwareService } from './device-software.service';
import * as i0 from "@angular/core";
import * as i1 from "./device-software.service";
import * as i2 from "@angular/common";
import * as i3 from "@c8y/ngx-components";
import * as i4 from "ngx-bootstrap/tooltip";
export class DeviceSoftwareListComponent {
    set softwareList(softwareList) {
        if (softwareList !== null) {
            this.legacySoftwareList$.next(softwareList);
        }
    }
    constructor(deviceSoftwareService) {
        this.deviceSoftwareService = deviceSoftwareService;
        this.PRODUCT_EXPERIENCE = PRODUCT_EXPERIENCE_REPOSITORY_SHARED;
        this.filterCriteria$ = of(null);
        this.update = new EventEmitter();
        this.remove = new EventEmitter();
        this.onListEmpty = new EventEmitter();
        this.alreadyInstalledMessage = gettext('This software is already installed on the device');
        this.supportsSoftwareOperations = false;
        this.operationTypes = ['c8y_SoftwareUpdate', 'c8y_SoftwareList', 'c8y_Software'];
        this.legacySoftwareList$ = new BehaviorSubject([]);
        this.destroyed$ = new Subject();
    }
    ngOnInit() {
        this.softwareItems$ = combineLatest([
            this.filterCriteria$.pipe(debounceTime(300), distinctUntilChanged()),
            this.legacySoftwareList$
        ]).pipe(switchMap(([filterCriteria, legacySoftwareList]) => this.deviceSoftwareService
            .getSoftwareList(this.device?.id, filterCriteria, legacySoftwareList)
            .pipe(map(resultList => ({ resultList, filterCriteria })))), tap(({ resultList, filterCriteria }) => {
            this.notifyListEmpty(!resultList?.paging?.totalPages && !filterCriteria);
            this.noSearchResults = !resultList?.paging?.totalPages && !!filterCriteria;
        }), map(({ resultList }) => resultList), share(), takeUntil(this.destroyed$));
        const supportedOperations = get(this.device, 'c8y_SupportedOperations', []);
        this.supportsSoftwareOperations = this.operationTypes.some(operationType => supportedOperations.indexOf(operationType) > -1);
    }
    ngAfterContentInit() {
        this.showUpdate = this.update.observers.length > 0;
        this.showRemove = this.remove.observers.length > 0;
    }
    isSoftwareGoingToBeChanged(software) {
        const relevantChanges = filter(this.deviceSoftwareChanges, software);
        return relevantChanges.length > 0;
    }
    ngOnDestroy() {
        this.destroyed$.next();
        this.destroyed$.complete();
    }
    notifyListEmpty(isEmpty) {
        this.emptyList = isEmpty;
        this.onListEmpty.emit(isEmpty);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: DeviceSoftwareListComponent, deps: [{ token: i1.DeviceSoftwareService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: DeviceSoftwareListComponent, selector: "c8y-device-software-list", inputs: { softwareList: "softwareList", device: "device", deviceSoftwareChanges: "deviceSoftwareChanges", filterCriteria$: "filterCriteria$" }, outputs: { update: "update", remove: "remove", onListEmpty: "onListEmpty" }, ngImport: i0, template: "<c8y-list-group class=\"no-border-2nd-last\">\n  <c8y-li\n    [ngClass]=\"{ disabled: isSoftwareGoingToBeChanged(software) }\"\n    *c8yFor=\"let software of softwareItems$\"\n  >\n    <!-- SOFTWARE ICON -->\n    <c8y-li-icon>\n      <i c8yIcon=\"c8y-tools\"></i>\n    </c8y-li-icon>\n\n    <c8y-li-body class=\"content-flex-20\">\n      <div title=\"{{ software.name }}\" class=\"col-9\">\n        <p class=\"d-flex\">\n          <!-- SOFTWARE NAME -->\n          <span class=\"text-truncate\">\n            {{ software.name }}\n          </span>\n          <!-- SOFTWARE TYPE-->\n          <span class=\"text-truncate\">\n            <span class=\"label label-primary m-l-8\">{{ software.softwareType }}</span>\n          </span>\n        </p>\n        <!-- SOFTWARE VERSION -->\n        <p class=\"d-flex a-i-center\">\n          <span class=\"text-truncate text-label-small m-r-4\" translate>Version</span>\n          <span class=\"text-truncate m-r-4\" title=\"{{ software.version }}\">\n            {{ software.version }}\n          </span>\n          <i\n            *ngIf=\"software.installed\"\n            c8yIcon=\"warning\"\n            class=\"text-warning a-s-center\"\n            [tooltip]=\"\n              alreadyInstalledMessage\n                | translate: { name: software.name, version: software.version }\n            \"\n            container=\"body\"\n            placement=\"top\"\n            [delay]=\"500\"\n          ></i>\n        </p>\n      </div>\n\n      <div\n        *ngIf=\"supportsSoftwareOperations && (showUpdate || showRemove)\"\n        class=\"col-3 text-right m-0 flex-grow d-flex a-i-center\"\n      >\n        <!-- UPDATE SOFTWARE -->\n        <button\n          class=\"btn btn-default btn-xs m-l-auto m-r-4\"\n          type=\"button\"\n          title=\"{{ 'Update`software,verb`' | translate }}\"\n          *ngIf=\"showUpdate && !isSoftwareGoingToBeChanged(software)\"\n          (click)=\"update.emit(software)\"\n          c8yProductExperience\n          [actionName]=\"PRODUCT_EXPERIENCE.SOFTWARE.EVENTS.DEVICE_TAB\"\n          [actionData]=\"{\n            component: PRODUCT_EXPERIENCE.SOFTWARE.COMPONENTS.DEVICE_SOFTWARE_LIST,\n            action: PRODUCT_EXPERIENCE.SOFTWARE.ACTIONS.OPEN_UPDATE_SOFTWARE\n          }\"\n        >\n          {{ 'Update`software,verb`' | translate }}\n        </button>\n\n        <!-- REMOVE SOFTWARE -->\n        <button\n          class=\"btn btn-dot btn-dot--danger m-l-4\"\n          type=\"button\"\n          [attr.aria-label]=\"'Remove`software,verb`' | translate\"\n          tooltip=\"{{ 'Remove`software,verb`' | translate }}\"\n          placement=\"bottom\"\n          container=\"body\"\n          [delay]=\"500\"\n          c8yProductExperience\n          [actionName]=\"PRODUCT_EXPERIENCE.SOFTWARE.EVENTS.DEVICE_TAB\"\n          [actionData]=\"{\n            component: PRODUCT_EXPERIENCE.SOFTWARE.COMPONENTS.DEVICE_SOFTWARE_LIST,\n            action: PRODUCT_EXPERIENCE.SOFTWARE.ACTIONS.DELETE_SOFTWARE\n          }\"\n          *ngIf=\"showRemove && !isSoftwareGoingToBeChanged(software)\"\n          (click)=\"remove.emit(software)\"\n        >\n          <i c8yIcon=\"minus-circle\"></i>\n        </button>\n      </div>\n    </c8y-li-body>\n  </c8y-li>\n</c8y-list-group>\n<!-- NO SEARCH RESULTS STATE -->\n<div class=\"card-block\" *ngIf=\"noSearchResults || emptyList\">\n  <ng-content *ngIf=\"emptyList\" select=\".c8y-empty-state:not(.c8y-no-results-state)\"></ng-content>\n  <ng-content *ngIf=\"noSearchResults\" select=\".c8y-no-results-state\"></ng-content>\n</div>\n", dependencies: [{ kind: "directive", type: i2.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i3.IconDirective, selector: "[c8yIcon]", inputs: ["c8yIcon"] }, { kind: "directive", type: i3.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { 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.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: "directive", type: i3.ProductExperienceDirective, selector: "[c8yProductExperience]", inputs: ["actionName", "actionData", "inherit", "suppressDataOverriding"] }, { kind: "directive", type: i4.TooltipDirective, selector: "[tooltip], [tooltipHtml]", inputs: ["adaptivePosition", "tooltip", "placement", "triggers", "container", "containerClass", "boundariesElement", "isOpen", "isDisabled", "delay", "tooltipHtml", "tooltipPlacement", "tooltipIsOpen", "tooltipEnable", "tooltipAppendToBody", "tooltipAnimation", "tooltipClass", "tooltipContext", "tooltipPopupDelay", "tooltipFadeDuration", "tooltipTrigger"], outputs: ["tooltipChange", "onShown", "onHidden", "tooltipStateChanged"], exportAs: ["bs-tooltip"] }, { kind: "pipe", type: i3.C8yTranslatePipe, name: "translate" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: DeviceSoftwareListComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-device-software-list', template: "<c8y-list-group class=\"no-border-2nd-last\">\n  <c8y-li\n    [ngClass]=\"{ disabled: isSoftwareGoingToBeChanged(software) }\"\n    *c8yFor=\"let software of softwareItems$\"\n  >\n    <!-- SOFTWARE ICON -->\n    <c8y-li-icon>\n      <i c8yIcon=\"c8y-tools\"></i>\n    </c8y-li-icon>\n\n    <c8y-li-body class=\"content-flex-20\">\n      <div title=\"{{ software.name }}\" class=\"col-9\">\n        <p class=\"d-flex\">\n          <!-- SOFTWARE NAME -->\n          <span class=\"text-truncate\">\n            {{ software.name }}\n          </span>\n          <!-- SOFTWARE TYPE-->\n          <span class=\"text-truncate\">\n            <span class=\"label label-primary m-l-8\">{{ software.softwareType }}</span>\n          </span>\n        </p>\n        <!-- SOFTWARE VERSION -->\n        <p class=\"d-flex a-i-center\">\n          <span class=\"text-truncate text-label-small m-r-4\" translate>Version</span>\n          <span class=\"text-truncate m-r-4\" title=\"{{ software.version }}\">\n            {{ software.version }}\n          </span>\n          <i\n            *ngIf=\"software.installed\"\n            c8yIcon=\"warning\"\n            class=\"text-warning a-s-center\"\n            [tooltip]=\"\n              alreadyInstalledMessage\n                | translate: { name: software.name, version: software.version }\n            \"\n            container=\"body\"\n            placement=\"top\"\n            [delay]=\"500\"\n          ></i>\n        </p>\n      </div>\n\n      <div\n        *ngIf=\"supportsSoftwareOperations && (showUpdate || showRemove)\"\n        class=\"col-3 text-right m-0 flex-grow d-flex a-i-center\"\n      >\n        <!-- UPDATE SOFTWARE -->\n        <button\n          class=\"btn btn-default btn-xs m-l-auto m-r-4\"\n          type=\"button\"\n          title=\"{{ 'Update`software,verb`' | translate }}\"\n          *ngIf=\"showUpdate && !isSoftwareGoingToBeChanged(software)\"\n          (click)=\"update.emit(software)\"\n          c8yProductExperience\n          [actionName]=\"PRODUCT_EXPERIENCE.SOFTWARE.EVENTS.DEVICE_TAB\"\n          [actionData]=\"{\n            component: PRODUCT_EXPERIENCE.SOFTWARE.COMPONENTS.DEVICE_SOFTWARE_LIST,\n            action: PRODUCT_EXPERIENCE.SOFTWARE.ACTIONS.OPEN_UPDATE_SOFTWARE\n          }\"\n        >\n          {{ 'Update`software,verb`' | translate }}\n        </button>\n\n        <!-- REMOVE SOFTWARE -->\n        <button\n          class=\"btn btn-dot btn-dot--danger m-l-4\"\n          type=\"button\"\n          [attr.aria-label]=\"'Remove`software,verb`' | translate\"\n          tooltip=\"{{ 'Remove`software,verb`' | translate }}\"\n          placement=\"bottom\"\n          container=\"body\"\n          [delay]=\"500\"\n          c8yProductExperience\n          [actionName]=\"PRODUCT_EXPERIENCE.SOFTWARE.EVENTS.DEVICE_TAB\"\n          [actionData]=\"{\n            component: PRODUCT_EXPERIENCE.SOFTWARE.COMPONENTS.DEVICE_SOFTWARE_LIST,\n            action: PRODUCT_EXPERIENCE.SOFTWARE.ACTIONS.DELETE_SOFTWARE\n          }\"\n          *ngIf=\"showRemove && !isSoftwareGoingToBeChanged(software)\"\n          (click)=\"remove.emit(software)\"\n        >\n          <i c8yIcon=\"minus-circle\"></i>\n        </button>\n      </div>\n    </c8y-li-body>\n  </c8y-li>\n</c8y-list-group>\n<!-- NO SEARCH RESULTS STATE -->\n<div class=\"card-block\" *ngIf=\"noSearchResults || emptyList\">\n  <ng-content *ngIf=\"emptyList\" select=\".c8y-empty-state:not(.c8y-no-results-state)\"></ng-content>\n  <ng-content *ngIf=\"noSearchResults\" select=\".c8y-no-results-state\"></ng-content>\n</div>\n" }]
        }], ctorParameters: () => [{ type: i1.DeviceSoftwareService }], propDecorators: { softwareList: [{
                type: Input
            }], device: [{
                type: Input
            }], deviceSoftwareChanges: [{
                type: Input
            }], filterCriteria$: [{
                type: Input
            }], update: [{
                type: Output
            }], remove: [{
                type: Output
            }], onListEmpty: [{
                type: Output
            }] } });
//# sourceMappingURL=data:application/json;base64, 