import * as i0 from '@angular/core';
import { Injectable, NgModule } from '@angular/core';
import * as i1 from '@angular/router';
import { NavigationEnd, RouterModule } from '@angular/router';
import { filter, map } from 'rxjs/operators';
import * as i2 from '@c8y/client';
import { gettext } from '@c8y/ngx-components/gettext';
import * as i3 from '@c8y/ngx-components';
import { Status, CommonModule } from '@c8y/ngx-components';
import { saveAs } from 'file-saver';
import { isUndefined } from 'lodash-es';
import * as i4 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(async (downloadId) => {
            try {
                const { data } = await 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);
            }
        });
    }
    async showDownloadModal(binaryMo) {
        try {
            const modalBody = this.translate.instant(gettext('You are about to download file "{{ fileName }}". Do you want to proceed?'), { fileName: binaryMo.name });
            await this.modalService.confirm(gettext('File download'), modalBody, Status.INFO, {
                ok: gettext('Download')
            });
            const arrayBuffer = await this.getArrayBuffer(binaryMo.id);
            const fileBinary = new File([arrayBuffer], binaryMo.name, { type: binaryMo.contentType });
            saveAs(fileBinary);
        }
        catch (e) {
            // empty body :(
            console.log({ e });
        }
    }
    async getArrayBuffer(binaryId) {
        let arrayBuffer;
        try {
            const res = await this.inventoryBinary.download(binaryId);
            arrayBuffer = await res.arrayBuffer();
        }
        catch (ex) {
            const msg = gettext('Could not get the binary.');
            this.alertService.danger(msg);
        }
        return arrayBuffer;
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: BinaryFileDownloadService, deps: [{ token: i1.Router }, { token: i1.ActivatedRoute }, { token: i2.InventoryService }, { token: i3.AlertService }, { token: i3.ModalService }, { token: i4.TranslateService }, { token: i2.InventoryBinaryService }], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: BinaryFileDownloadService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: BinaryFileDownloadService, decorators: [{
            type: Injectable
        }], ctorParameters: () => [{ type: i1.Router }, { type: i1.ActivatedRoute }, { type: i2.InventoryService }, { type: i3.AlertService }, { type: i3.ModalService }, { type: i4.TranslateService }, { type: i2.InventoryBinaryService }] });

class BinaryFileDownloadModule {
    constructor(binaryFileDownloadService) {
        binaryFileDownloadService.run();
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: BinaryFileDownloadModule, deps: [{ token: BinaryFileDownloadService }], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.3.25", ngImport: i0, type: BinaryFileDownloadModule, imports: [CommonModule, RouterModule] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: BinaryFileDownloadModule, providers: [BinaryFileDownloadService], imports: [CommonModule, RouterModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.25", ngImport: i0, type: BinaryFileDownloadModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CommonModule, RouterModule],
                    providers: [BinaryFileDownloadService]
                }]
        }], ctorParameters: () => [{ type: BinaryFileDownloadService }] });

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

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