import { __awaiter } from 'tslib';
import { Injectable, NgModule } from '@angular/core';
import { NavigationEnd, Router, ActivatedRoute, RouterModule } from '@angular/router';
import { filter, map } from 'rxjs/operators';
import { InventoryService, InventoryBinaryService } from '@c8y/client';
import { gettext, Status, AlertService, ModalService, CommonModule } from '@c8y/ngx-components';
import { saveAs } from 'file-saver';
import { isUndefined } from 'lodash-es';
import { TranslateService } from '@ngx-translate/core';

class BinaryFileDownloadService {
    constructor(router, route, inventoryService, alertService, modalService, translate, inventoryBinary) {
        this.router = router;
        this.route = route;
        this.inventoryService = inventoryService;
        this.alertService = alertService;
        this.modalService = modalService;
        this.translate = translate;
        this.inventoryBinary = inventoryBinary;
    }
    run() {
        this.router.events
            .pipe(filter((event) => {
            return event instanceof NavigationEnd
                && this.route.snapshot.queryParams.download;
        }), map(() => this.route.snapshot.queryParams.download))
            .subscribe((downloadId) => __awaiter(this, void 0, void 0, function* () {
            try {
                const { data } = yield this.inventoryService.detail(downloadId);
                if (!isUndefined(data.c8y_IsBinary)) {
                    this.showDownloadModal(data);
                }
                else {
                    const alertMessage = this.translate.instant(gettext('Could not download the file: object with ID "{{ id }}" is not a valid binary.'), { id: data.id });
                    this.alertService.danger(alertMessage);
                }
            }
            catch (ex) {
                this.alertService.addServerFailure(ex);
            }
        }));
    }
    showDownloadModal(data) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const modalBody = this.translate.instant(gettext('You are about to download file "{{ fileName }}". Do you want to proceed?'), { fileName: data.name });
                yield this.modalService.confirm(gettext('File download'), modalBody, Status.INFO, { ok: gettext('Download') });
                const binary = yield this.getBinary(data.id);
                const fileBinary = new File([binary], data.name, { type: data.contentType });
                saveAs(fileBinary);
            }
            catch (e) {
                // empty body :(
            }
        });
    }
    getBinary(binaryId) {
        return __awaiter(this, void 0, void 0, function* () {
            let binary;
            try {
                const res = yield this.inventoryBinary.download(binaryId);
                binary = yield res.text();
            }
            catch (ex) {
                const msg = gettext('Could not get the binary.');
                this.alertService.danger(msg);
            }
            return binary;
        });
    }
}
BinaryFileDownloadService.decorators = [
    { type: Injectable }
];
BinaryFileDownloadService.ctorParameters = () => [
    { type: Router },
    { type: ActivatedRoute },
    { type: InventoryService },
    { type: AlertService },
    { type: ModalService },
    { type: TranslateService },
    { type: InventoryBinaryService }
];

class BinaryFileDownloadModule {
    constructor(binaryFileDownloadService) {
        binaryFileDownloadService.run();
    }
}
BinaryFileDownloadModule.decorators = [
    { type: NgModule, args: [{
                imports: [CommonModule, RouterModule],
                providers: [BinaryFileDownloadService]
            },] }
];
BinaryFileDownloadModule.ctorParameters = () => [
    { type: BinaryFileDownloadService }
];

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

export { BinaryFileDownloadModule, BinaryFileDownloadService };
//# sourceMappingURL=c8y-ngx-components-binary-file-download.js.map
