import * as i0 from '@angular/core';
import { Component, Optional, Inject, Input, ViewChild, NgModule } from '@angular/core';
import * as i1 from '@aztrix/helpers';
import { displayName, isPage, fetchQR$, getActivePageLanguage, downloadDataUrl, HelperPipesModule } from '@aztrix/helpers';
import * as i2 from '@aztrix/sdk';
import { BASE_URL } from '@aztrix/sdk';
import { mergeMap } from 'rxjs';
import * as i3 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i4 from '@aztrix/components/spinner';
import { SpinnerModule } from '@aztrix/components/spinner';

class QrCodeComponent {
    constructor(_translate, _changeDetector, _profileService, _baseUrl) {
        this._translate = _translate;
        this._changeDetector = _changeDetector;
        this._profileService = _profileService;
        this._baseUrl = _baseUrl;
        this.qrGenerated = false;
    }
    ngOnChanges(changes) {
        if (changes.subject && this.subject) {
            this._fetchQR();
        }
    }
    _fetchQR(download = false) {
        const fileName = download
            ? `${displayName(this._translate, this.subject).toLowerCase().split(' ').join('-')}-qr.png`
            : null;
        if (isPage(this.subject)) {
            fetchQR$(250, getActivePageLanguage(this.subject, this._translate.getActiveLang()), this._baseUrl, null, this.queryParams).subscribe((dataUrl) => {
                if (download) {
                    downloadDataUrl(dataUrl, fileName);
                }
                else {
                    this.qrImage.nativeElement.src = dataUrl;
                }
                this.qrGenerated = true;
                this._changeDetector.detectChanges();
            });
        }
        else {
            this._profileService
                .getMeProfile()
                .pipe(mergeMap((myProfile) => {
                return fetchQR$(250, this.subject, this._baseUrl, myProfile.id, null);
            }))
                .subscribe((dataUrl) => {
                if (download) {
                    downloadDataUrl(dataUrl, fileName);
                }
                else {
                    this.qrImage.nativeElement.src = dataUrl;
                }
                this.qrGenerated = true;
                this._changeDetector.detectChanges();
            });
        }
    }
}
QrCodeComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: QrCodeComponent, deps: [{ token: i1.TranslateService }, { token: i0.ChangeDetectorRef }, { token: i2.ProfileService }, { token: BASE_URL, optional: true }], target: i0.ɵɵFactoryTarget.Component });
QrCodeComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "15.1.5", type: QrCodeComponent, selector: "ax-qr-code", inputs: { subject: "subject", queryParams: "queryParams" }, viewQueries: [{ propertyName: "qrImage", first: true, predicate: ["qrCode"], descendants: true, static: true }], usesOnChanges: true, ngImport: i0, template: "<ax-spinner *ngIf=\"!qrGenerated\" [label]=\"'forward.qr.loading' | translate\"></ax-spinner>\n<img\n  [ngStyle]=\"{display: qrGenerated ? 'initial' : 'none'}\"\n  #qrCode\n  alt=\"{{ 'forward.qr.description' | translate }}\"\n/>\n", styles: [":host{display:block;background-color:#fff;transition:background-color .4s cubic-bezier(.25,.8,.25,1)}:host:hover{background-color:#e6e6e6}\n"], dependencies: [{ kind: "directive", type: i3.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i3.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }, { kind: "component", type: i4.SpinnerComponent, selector: "ax-spinner" }, { kind: "pipe", type: i1.TranslatePipe, name: "translate" }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: QrCodeComponent, decorators: [{
            type: Component,
            args: [{ selector: 'ax-qr-code', template: "<ax-spinner *ngIf=\"!qrGenerated\" [label]=\"'forward.qr.loading' | translate\"></ax-spinner>\n<img\n  [ngStyle]=\"{display: qrGenerated ? 'initial' : 'none'}\"\n  #qrCode\n  alt=\"{{ 'forward.qr.description' | translate }}\"\n/>\n", styles: [":host{display:block;background-color:#fff;transition:background-color .4s cubic-bezier(.25,.8,.25,1)}:host:hover{background-color:#e6e6e6}\n"] }]
        }], ctorParameters: function () {
        return [{ type: i1.TranslateService }, { type: i0.ChangeDetectorRef }, { type: i2.ProfileService }, { type: undefined, decorators: [{
                        type: Optional
                    }, {
                        type: Inject,
                        args: [BASE_URL]
                    }] }];
    }, propDecorators: { subject: [{
                type: Input
            }], queryParams: [{
                type: Input
            }], qrImage: [{
                type: ViewChild,
                args: ['qrCode', { static: true }]
            }] } });

class QrCodeModule {
}
QrCodeModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: QrCodeModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
QrCodeModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.1.5", ngImport: i0, type: QrCodeModule, declarations: [QrCodeComponent], imports: [CommonModule, HelperPipesModule, SpinnerModule], exports: [QrCodeComponent] });
QrCodeModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: QrCodeModule, imports: [CommonModule, HelperPipesModule, SpinnerModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: QrCodeModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [QrCodeComponent],
                    imports: [CommonModule, HelperPipesModule, SpinnerModule],
                    exports: [QrCodeComponent],
                }]
        }] });

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

export { QrCodeComponent, QrCodeModule };
//# sourceMappingURL=aztrix-components-qr-code.mjs.map
//# sourceMappingURL=aztrix-components-qr-code.mjs.map
