import * as i0 from '@angular/core';
import { ViewChild, Input, Optional, Inject, Component, NgModule } from '@angular/core';
import { BASE_URL } from '@aztrix/environment';
import { displayName, isPage, fetchQR$, getActivePageLanguage, downloadDataUrl } from '@aztrix/helpers';
import * as i2 from '@aztrix/sdk';
import * as i1 from '@aztrix/translate';
import { TranslatePipe } from '@aztrix/translate';
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 QrCodePlatformComponent {
    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`
            : undefined;
        if (isPage(this.subject)) {
            fetchQR$(250, getActivePageLanguage(this.subject, this._translate.getActiveLang()), this._baseUrl, undefined, 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);
            }))
                .subscribe((dataUrl) => {
                if (download) {
                    downloadDataUrl(dataUrl, fileName);
                }
                else {
                    this.qrImage.nativeElement.src = dataUrl;
                }
                this.qrGenerated = true;
                this._changeDetector.detectChanges();
            });
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: QrCodePlatformComponent, deps: [{ token: i1.TranslateService }, { token: i0.ChangeDetectorRef }, { token: i2.ProfileService }, { token: BASE_URL, optional: true }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.3.12", type: QrCodePlatformComponent, selector: "ax-qr-code-platform", 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\"></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: "17.3.12", ngImport: i0, type: QrCodePlatformComponent, decorators: [{
            type: Component,
            args: [{ selector: 'ax-qr-code-platform', template: "<ax-spinner *ngIf=\"!qrGenerated\"></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: () => [{ 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 QrCodePlatformModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: QrCodePlatformModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.3.12", ngImport: i0, type: QrCodePlatformModule, declarations: [QrCodePlatformComponent], imports: [CommonModule, TranslatePipe, SpinnerModule], exports: [QrCodePlatformComponent] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: QrCodePlatformModule, imports: [CommonModule, SpinnerModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: QrCodePlatformModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [QrCodePlatformComponent],
                    imports: [CommonModule, TranslatePipe, SpinnerModule],
                    exports: [QrCodePlatformComponent],
                }]
        }] });

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

export { QrCodePlatformComponent, QrCodePlatformModule };
//# sourceMappingURL=aztrix-components-qr-code-platform.mjs.map
