import { __decorate, __metadata } from "tslib";
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { InventoryService } from '@c8y/client';
import { memoize } from '@c8y/ngx-components';
import { BulkOperationsService } from '@c8y/ngx-components/operations/bulk-operations-service';
import { RepositoryService } from '@c8y/ngx-components/repository/shared';
import { property } from 'lodash-es';
import { defer, of } from 'rxjs';
import { catchError, distinctUntilChanged, distinctUntilKeyChanged, map, shareReplay, switchMap, tap } from 'rxjs/operators';
import * as i0 from "@angular/core";
import * as i1 from "@c8y/ngx-components/repository/shared";
import * as i2 from "@c8y/client";
import * as i3 from "@c8y/ngx-components/operations/bulk-operations-service";
import * as i4 from "@c8y/ngx-components";
import * as i5 from "@angular/common";
export class VersionOrPatchComponent {
    constructor(repositoryService, inventoryService, bulkOpsService) {
        this.repositoryService = repositoryService;
        this.inventoryService = inventoryService;
        this.bulkOpsService = bulkOpsService;
        this.versionOrPatch = new EventEmitter();
        this.elementCount = 0;
        this.DD_LOW_COUNT = 10;
        this.firmware$ = this.bulkOpsService.firmwareId.pipe(distinctUntilChanged(), switchMap(id => defer(() => this.inventoryService.detail(id).then(result => result.data)).pipe(catchError(error => of(error)))), shareReplay(1));
        this.baseVersions$ = this.firmware$.pipe(distinctUntilKeyChanged('id'), switchMap(firmware => this.repositoryService.listBaseVersions(firmware)), tap(resp => {
            this.elementCount = resp.data ? resp.data.length : 0;
        }), shareReplay(1));
        this.isLegacy$ = this.firmware$.pipe(map(firmware => this.repositoryService.isLegacyEntry(firmware)), shareReplay(1));
        this.expanded = {};
        this.DD_LOW_COUNT = this.bulkOpsService.DD_LOW_COUNT;
    }
    getBinaryName$(binaryUrl) {
        return this.repositoryService.getBinaryName$(binaryUrl);
    }
    getBaseVersionAndPatches$(baseVersion) {
        return this.firmware$.pipe(distinctUntilKeyChanged('id'), switchMap(firmware => this.repositoryService.listBaseVersionAndPatches(firmware, baseVersion)), shareReplay(1));
    }
    getPatchVersionsCount$(baseVersion) {
        return this.firmware$.pipe(distinctUntilKeyChanged('id'), switchMap(() => this.firmware$), switchMap(firmware => this.repositoryService.getPatchVersionsCount$(firmware, baseVersion)), shareReplay(1));
    }
    selectVersionOrPatch(selected, versionOrPatch) {
        if (selected) {
            this.versionOrPatch.emit(versionOrPatch);
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: VersionOrPatchComponent, deps: [{ token: i1.RepositoryService }, { token: i2.InventoryService }, { token: i3.BulkOperationsService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.9", type: VersionOrPatchComponent, selector: "c8y-version-or-patch-step", inputs: { selected: "selected" }, outputs: { versionOrPatch: "versionOrPatch" }, ngImport: i0, template: "<c8y-list-group\n  class=\"m-b-16\"\n  *ngIf=\"(baseVersions$ | async)?.data.length > 0\"\n>\n  <c8y-li *c8yFor=\"let baseVersion of baseVersions$ | async; let i = index; loadMore: 'auto'\">\n    <c8y-li-icon>\n      <i c8yIcon=\"c8y-firmware\"></i>\n    </c8y-li-icon>\n\n    <c8y-li-body class=\"content-flex-50\">\n      <div class=\"col-10\">\n        <p>{{ baseVersion.c8y_Firmware.version }}</p>\n      </div>\n      <div class=\"col-2\">\n        <span\n          class=\"label label-warning\"\n          *ngIf=\"isLegacy$ | async\"\n        >\n          {{ 'Legacy' | translate }}\n        </span>\n\n        <span *ngIf=\"!(isLegacy$ | async)\">\n          <span *ngIf=\"(getPatchVersionsCount$(baseVersion) | async) === null\">\n            <span class=\"label label-info\">\n              <i\n                class=\"icon-spin\"\n                c8yIcon=\"circle-o-notch\"\n              ></i>\n            </span>\n          </span>\n\n          <span *ngIf=\"(getPatchVersionsCount$(baseVersion) | async) !== null\">\n            <span [ngPlural]=\"getPatchVersionsCount$(baseVersion) | async\">\n              <ng-template ngPluralCase=\"=0\">\n                <span class=\"label label-default\"><span translate>No patches</span></span>\n              </ng-template>\n              <ng-template ngPluralCase=\"=1\">\n                <span class=\"label label-info\">\n                  <span translate>1 patch</span>\n                </span>\n              </ng-template>\n              <ng-template ngPluralCase=\"other\">\n                <span class=\"label label-info\">\n                  <span\n                    ngNonBindable\n                    translate\n                    [translateParams]=\"{ count: getPatchVersionsCount$(baseVersion) | async }\"\n                  >\n                    {{ count }} patches\n                  </span>\n                </span>\n              </ng-template>\n            </span>\n          </span>\n        </span>\n      </div>\n    </c8y-li-body>\n\n    <c8y-li-collapse>\n      <c8y-list-group>\n        <c8y-li\n          *c8yFor=\"\n            let patchVersion of getBaseVersionAndPatches$(baseVersion) | async;\n            let i = index;\n            loadMore: 'auto'\n          \"\n        >\n          <c8y-li-radio\n            (onSelect)=\"selectVersionOrPatch($event, patchVersion)\"\n            [selected]=\"patchVersion === selected\"\n          ></c8y-li-radio>\n          <c8y-li-icon>\n            <i c8yIcon=\"c8y-firmware\"></i>\n          </c8y-li-icon>\n          <c8y-li-body class=\"content-flex-50\">\n            <div class=\"col-3\">\n              {{ patchVersion.c8y_Firmware.version }}\n            </div>\n            <div class=\"col-3\">\n              <span\n                class=\"label label-info\"\n                *ngIf=\"patchVersion.c8y_Patch; else version\"\n                translate\n              >\n                patch\n              </span>\n              <ng-template #version>\n                <span\n                  class=\"label label-primary\"\n                  translate\n                >\n                  version\n                </span>\n              </ng-template>\n            </div>\n            <div class=\"col-6 text-truncate\">\n              <span\n                class=\"text-label-small m-r-4\"\n                translate\n              >\n                File\n              </span>\n              <span title=\"{{ getBinaryName$(patchVersion.c8y_Firmware.url) | async }}\">\n                {{ getBinaryName$(patchVersion.c8y_Firmware.url) | async }}\n              </span>\n            </div>\n          </c8y-li-body>\n        </c8y-li>\n      </c8y-list-group>\n    </c8y-li-collapse>\n  </c8y-li>\n</c8y-list-group>\n", dependencies: [{ kind: "directive", type: i4.IconDirective, selector: "[c8yIcon]", inputs: ["c8yIcon"] }, { kind: "directive", type: i4.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "directive", type: i5.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i5.NgPlural, selector: "[ngPlural]", inputs: ["ngPlural"] }, { kind: "directive", type: i5.NgPluralCase, selector: "[ngPluralCase]" }, { kind: "directive", type: i4.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: i4.ListGroupComponent, selector: "c8y-list-group" }, { kind: "component", type: i4.ListItemComponent, selector: "c8y-list-item, c8y-li", inputs: ["active", "highlighted", "emptyActions", "dense", "collapsed", "selectable"], outputs: ["collapsedChange"] }, { kind: "component", type: i4.ListItemIconComponent, selector: "c8y-list-item-icon, c8y-li-icon", inputs: ["icon", "status"] }, { kind: "component", type: i4.ListItemBodyComponent, selector: "c8y-list-item-body, c8y-li-body", inputs: ["body"] }, { kind: "component", type: i4.ListItemCollapseComponent, selector: "c8y-list-item-collapse, c8y-li-collapse", inputs: ["collapseWay"] }, { kind: "component", type: i4.ListItemRadioComponent, selector: "c8y-list-item-radio, c8y-li-radio", inputs: ["selected", "name", "disabled", "value"], outputs: ["onSelect"] }, { kind: "pipe", type: i4.C8yTranslatePipe, name: "translate" }, { kind: "pipe", type: i5.AsyncPipe, name: "async" }] }); }
}
__decorate([
    memoize(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], VersionOrPatchComponent.prototype, "getBinaryName$", null);
__decorate([
    memoize(property('id')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], VersionOrPatchComponent.prototype, "getBaseVersionAndPatches$", null);
__decorate([
    memoize(property('id')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], VersionOrPatchComponent.prototype, "getPatchVersionsCount$", null);
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: VersionOrPatchComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-version-or-patch-step', template: "<c8y-list-group\n  class=\"m-b-16\"\n  *ngIf=\"(baseVersions$ | async)?.data.length > 0\"\n>\n  <c8y-li *c8yFor=\"let baseVersion of baseVersions$ | async; let i = index; loadMore: 'auto'\">\n    <c8y-li-icon>\n      <i c8yIcon=\"c8y-firmware\"></i>\n    </c8y-li-icon>\n\n    <c8y-li-body class=\"content-flex-50\">\n      <div class=\"col-10\">\n        <p>{{ baseVersion.c8y_Firmware.version }}</p>\n      </div>\n      <div class=\"col-2\">\n        <span\n          class=\"label label-warning\"\n          *ngIf=\"isLegacy$ | async\"\n        >\n          {{ 'Legacy' | translate }}\n        </span>\n\n        <span *ngIf=\"!(isLegacy$ | async)\">\n          <span *ngIf=\"(getPatchVersionsCount$(baseVersion) | async) === null\">\n            <span class=\"label label-info\">\n              <i\n                class=\"icon-spin\"\n                c8yIcon=\"circle-o-notch\"\n              ></i>\n            </span>\n          </span>\n\n          <span *ngIf=\"(getPatchVersionsCount$(baseVersion) | async) !== null\">\n            <span [ngPlural]=\"getPatchVersionsCount$(baseVersion) | async\">\n              <ng-template ngPluralCase=\"=0\">\n                <span class=\"label label-default\"><span translate>No patches</span></span>\n              </ng-template>\n              <ng-template ngPluralCase=\"=1\">\n                <span class=\"label label-info\">\n                  <span translate>1 patch</span>\n                </span>\n              </ng-template>\n              <ng-template ngPluralCase=\"other\">\n                <span class=\"label label-info\">\n                  <span\n                    ngNonBindable\n                    translate\n                    [translateParams]=\"{ count: getPatchVersionsCount$(baseVersion) | async }\"\n                  >\n                    {{ count }} patches\n                  </span>\n                </span>\n              </ng-template>\n            </span>\n          </span>\n        </span>\n      </div>\n    </c8y-li-body>\n\n    <c8y-li-collapse>\n      <c8y-list-group>\n        <c8y-li\n          *c8yFor=\"\n            let patchVersion of getBaseVersionAndPatches$(baseVersion) | async;\n            let i = index;\n            loadMore: 'auto'\n          \"\n        >\n          <c8y-li-radio\n            (onSelect)=\"selectVersionOrPatch($event, patchVersion)\"\n            [selected]=\"patchVersion === selected\"\n          ></c8y-li-radio>\n          <c8y-li-icon>\n            <i c8yIcon=\"c8y-firmware\"></i>\n          </c8y-li-icon>\n          <c8y-li-body class=\"content-flex-50\">\n            <div class=\"col-3\">\n              {{ patchVersion.c8y_Firmware.version }}\n            </div>\n            <div class=\"col-3\">\n              <span\n                class=\"label label-info\"\n                *ngIf=\"patchVersion.c8y_Patch; else version\"\n                translate\n              >\n                patch\n              </span>\n              <ng-template #version>\n                <span\n                  class=\"label label-primary\"\n                  translate\n                >\n                  version\n                </span>\n              </ng-template>\n            </div>\n            <div class=\"col-6 text-truncate\">\n              <span\n                class=\"text-label-small m-r-4\"\n                translate\n              >\n                File\n              </span>\n              <span title=\"{{ getBinaryName$(patchVersion.c8y_Firmware.url) | async }}\">\n                {{ getBinaryName$(patchVersion.c8y_Firmware.url) | async }}\n              </span>\n            </div>\n          </c8y-li-body>\n        </c8y-li>\n      </c8y-list-group>\n    </c8y-li-collapse>\n  </c8y-li>\n</c8y-list-group>\n" }]
        }], ctorParameters: () => [{ type: i1.RepositoryService }, { type: i2.InventoryService }, { type: i3.BulkOperationsService }], propDecorators: { selected: [{
                type: Input
            }], versionOrPatch: [{
                type: Output
            }], getBinaryName$: [], getBaseVersionAndPatches$: [], getPatchVersionsCount$: [] } });
//# sourceMappingURL=data:application/json;base64, 