import * as i0 from '@angular/core';
import { Component, Optional, Inject, ViewChild, Input, Injectable, Directive, HostListener, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import * as i5 from 'ng2-pdfjs-viewer';
import { PdfJsViewerModule } from 'ng2-pdfjs-viewer';
import * as i1 from '@angular/common/http';
import * as i2 from '@ngx-translate/core';
import * as i3 from '@eui/core';
import { DYNAMIC_COMPONENT_CONFIG, consumeEvent } from '@eui/core';
import * as i1$1 from '@eui/components';
import { UxDynamicModalConfig } from '@eui/components';

class UxDocumentViewerConfig {
    constructor(values) {
        this.viewerWidth = '100%';
        this.viewerHeight = '600px';
        this.startPage = 1;
        this.startZoom = 'page-fit';
        this.isPrintable = true;
        this.isDownloadable = true;
        this.isOpenable = false;
        Object.assign(this, values);
    }
}

const _c0 = ["viewer"];
class UxDocumentViewerComponent {
    constructor(uxDocumentViewerConfig, httpClient, cdRef, translateService, asService) {
        this.httpClient = httpClient;
        this.cdRef = cdRef;
        this.translateService = translateService;
        this.asService = asService;
        this.viewerWidth = '100%';
        this.viewerHeight = '600px';
        this.startPage = 1;
        this.startZoom = 'page-fit';
        this.isPrintable = true;
        this.isDownloadable = true;
        this.isOpenable = false;
        this.e2eAttr = 'ux-document-viewer';
        if (uxDocumentViewerConfig) {
            this.config = uxDocumentViewerConfig;
            this.documentSrc = uxDocumentViewerConfig.documentSrc;
            this.viewerWidth = uxDocumentViewerConfig.viewerWidth;
            this.viewerHeight = uxDocumentViewerConfig.viewerHeight;
            this.startPage = uxDocumentViewerConfig.startPage;
            this.startZoom = uxDocumentViewerConfig.startZoom;
            this.isPrintable = uxDocumentViewerConfig.isPrintable;
            this.isDownloadable = uxDocumentViewerConfig.isDownloadable;
            this.isOpenable = uxDocumentViewerConfig.isOpenable;
        }
        this.language = this.translateService.currentLang;
        this.languageSubscription = this.translateService.onLangChange.subscribe((event) => {
            this.resetLanguage(event.lang);
        });
    }
    ngOnInit() {
        if (this.documentSrc) {
            this.loadDocument();
        }
    }
    ngOnChanges(changes) {
        if (changes.documentSrc
            && changes.documentSrc.currentValue
            && changes.documentSrc.currentValue !== changes.documentSrc.previousValue) {
            this.loadDocument();
        }
    }
    ngOnDestroy() {
        if (this.languageSubscription) {
            this.languageSubscription.unsubscribe();
        }
    }
    /**
     * If the URL is passed to PDFjs directly, the library is included in an iframe and does its own XMLHttpRequest
     * calls, it is a problem to intercept those calls to enrich them with OpenID headers. In case of Linearized PDF
     * documents there is also a lot of calls to the back-end, requesting partial content. This means every viewed
     * document would need potentially dozens of calls to get the header data and then the content as people scroll
     * up and down.
     * As a temporary solution, until something more robust is provided, the document is loaded up front and the
     * blob is passed to the library.
     */
    loadDocument() {
        this.httpClient
            .get(this.documentSrc, { responseType: 'blob' })
            .subscribe((response) => {
            this.documentBlob = response;
            if (this.viewer) {
                this.viewer.pdfSrc = response;
                this.viewer.refresh();
            }
            this.cdRef.markForCheck();
        }, (error) => {
            console.error('ux-document-viewer error while loading document contents', error);
            this.asService.growlError('Document viewer could not load the requested document.');
        });
    }
    resetLanguage(language) {
        this.language = language;
        if (this.viewer) {
            this.viewer.locale = this.language;
            this.viewer.refresh();
        }
    }
}
/** @nocollapse */ UxDocumentViewerComponent.ɵfac = function UxDocumentViewerComponent_Factory(t) { return new (t || UxDocumentViewerComponent)(i0.ɵɵdirectiveInject(DYNAMIC_COMPONENT_CONFIG, 8), i0.ɵɵdirectiveInject(i1.HttpClient), i0.ɵɵdirectiveInject(i0.ChangeDetectorRef), i0.ɵɵdirectiveInject(i2.TranslateService), i0.ɵɵdirectiveInject(i3.UxAppShellService)); };
/** @nocollapse */ UxDocumentViewerComponent.ɵcmp = i0.ɵɵdefineComponent({ type: UxDocumentViewerComponent, selectors: [["ux-document-viewer"]], viewQuery: function UxDocumentViewerComponent_Query(rf, ctx) { if (rf & 1) {
        i0.ɵɵviewQuery(_c0, true);
    } if (rf & 2) {
        var _t;
        i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.viewer = _t.first);
    } }, inputs: { documentSrc: "documentSrc", viewerWidth: "viewerWidth", viewerHeight: "viewerHeight", startPage: "startPage", startZoom: "startZoom", isPrintable: "isPrintable", isDownloadable: "isDownloadable", isOpenable: "isOpenable", e2eAttr: "e2eAttr" }, features: [i0.ɵɵNgOnChangesFeature], decls: 3, vars: 13, consts: [[1, "ux-document-viewer"], [3, "pdfSrc", "viewerFolder", "page", "zoom", "print", "download", "openFile", "locale"], ["viewer", ""]], template: function UxDocumentViewerComponent_Template(rf, ctx) { if (rf & 1) {
        i0.ɵɵelementStart(0, "div", 0);
        i0.ɵɵelement(1, "ng2-pdfjs-viewer", 1, 2);
        i0.ɵɵelementEnd();
    } if (rf & 2) {
        i0.ɵɵstyleProp("width", ctx.viewerWidth)("height", ctx.viewerHeight);
        i0.ɵɵattribute("data-e2e", ctx.e2eAttr);
        i0.ɵɵadvance(1);
        i0.ɵɵproperty("pdfSrc", ctx.documentBlob)("viewerFolder", "/assets/pdfjs")("page", ctx.startPage)("zoom", ctx.startZoom)("print", ctx.isPrintable)("download", ctx.isDownloadable)("openFile", ctx.isOpenable)("locale", ctx.language);
    } }, directives: [i5.PdfJsViewerComponent], encapsulation: 2 });
/*@__PURE__*/ (function () { i0.ɵsetClassMetadata(UxDocumentViewerComponent, [{
        type: Component,
        args: [{
                selector: 'ux-document-viewer',
                templateUrl: './ux-document-viewer.component.html',
            }]
    }], function () { return [{ type: UxDocumentViewerConfig, decorators: [{
                type: Optional
            }, {
                type: Inject,
                args: [DYNAMIC_COMPONENT_CONFIG]
            }] }, { type: i1.HttpClient }, { type: i0.ChangeDetectorRef }, { type: i2.TranslateService }, { type: i3.UxAppShellService }]; }, { viewer: [{
            type: ViewChild,
            args: ['viewer']
        }], documentSrc: [{
            type: Input
        }], viewerWidth: [{
            type: Input
        }], viewerHeight: [{
            type: Input
        }], startPage: [{
            type: Input
        }], startZoom: [{
            type: Input
        }], isPrintable: [{
            type: Input
        }], isDownloadable: [{
            type: Input
        }], isOpenable: [{
            type: Input
        }], e2eAttr: [{
            type: Input
        }] }); })();

class UxDocumentViewerService {
    constructor(uxDynamicModalService) {
        this.uxDynamicModalService = uxDynamicModalService;
    }
    openDocument(documentViewerConfig, uxDynamicModalConfig) {
        const bodyInjectedComponent = {
            component: UxDocumentViewerComponent,
            config: documentViewerConfig,
        };
        const defaultModalConfig = new UxDynamicModalConfig({
            isFooterVisible: false,
            hasNoBodyPadding: true,
            bodyStyleClass: 'ux-document-viewer__modal',
            bodyInjectedComponent,
        });
        let config = defaultModalConfig;
        if (uxDynamicModalConfig) {
            config = Object.assign(defaultModalConfig, uxDynamicModalConfig);
        }
        this.uxDynamicModalService.openModal(config);
    }
}
/** @nocollapse */ UxDocumentViewerService.ɵfac = function UxDocumentViewerService_Factory(t) { return new (t || UxDocumentViewerService)(i0.ɵɵinject(i1$1.UxDynamicModalService)); };
/** @nocollapse */ UxDocumentViewerService.ɵprov = i0.ɵɵdefineInjectable({ token: UxDocumentViewerService, factory: UxDocumentViewerService.ɵfac });
/*@__PURE__*/ (function () { i0.ɵsetClassMetadata(UxDocumentViewerService, [{
        type: Injectable
    }], function () { return [{ type: i1$1.UxDynamicModalService }]; }, null); })();

class UxDocumentViewerDirective {
    constructor(uxDocumentViewerService) {
        this.uxDocumentViewerService = uxDocumentViewerService;
    }
    onClick(e) {
        const documentViewerConfig = this.uxDocumentViewerConfig ?
            Object.assign(new UxDocumentViewerConfig(this.uxDocumentViewerConfig), { documentSrc: this.href }) :
            new UxDocumentViewerConfig({
                documentSrc: this.href,
            });
        this.uxDocumentViewerService.openDocument(documentViewerConfig, this.uxDocumentViewerModalConfig);
        consumeEvent(e);
    }
}
/** @nocollapse */ UxDocumentViewerDirective.ɵfac = function UxDocumentViewerDirective_Factory(t) { return new (t || UxDocumentViewerDirective)(i0.ɵɵdirectiveInject(UxDocumentViewerService)); };
/** @nocollapse */ UxDocumentViewerDirective.ɵdir = i0.ɵɵdefineDirective({ type: UxDocumentViewerDirective, selectors: [["", "uxDocumentViewer", ""]], hostBindings: function UxDocumentViewerDirective_HostBindings(rf, ctx) { if (rf & 1) {
        i0.ɵɵlistener("click", function UxDocumentViewerDirective_click_HostBindingHandler($event) { return ctx.onClick($event); });
    } }, inputs: { href: "href", uxDocumentViewerConfig: "uxDocumentViewerConfig", uxDocumentViewerModalConfig: "uxDocumentViewerModalConfig" } });
/*@__PURE__*/ (function () { i0.ɵsetClassMetadata(UxDocumentViewerDirective, [{
        type: Directive,
        args: [{
                selector: '[uxDocumentViewer]'
            }]
    }], function () { return [{ type: UxDocumentViewerService }]; }, { href: [{
            type: Input
        }], uxDocumentViewerConfig: [{
            type: Input
        }], uxDocumentViewerModalConfig: [{
            type: Input
        }], onClick: [{
            type: HostListener,
            args: ['click', ['$event']]
        }] }); })();

class UxDocumentViewerComponentModule {
}
/** @nocollapse */ UxDocumentViewerComponentModule.ɵmod = i0.ɵɵdefineNgModule({ type: UxDocumentViewerComponentModule });
/** @nocollapse */ UxDocumentViewerComponentModule.ɵinj = i0.ɵɵdefineInjector({ factory: function UxDocumentViewerComponentModule_Factory(t) { return new (t || UxDocumentViewerComponentModule)(); }, providers: [
        UxDocumentViewerService,
    ], imports: [[
            CommonModule,
            PdfJsViewerModule,
        ]] });
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(UxDocumentViewerComponentModule, { declarations: [UxDocumentViewerComponent,
        UxDocumentViewerDirective], imports: [CommonModule,
        PdfJsViewerModule], exports: [UxDocumentViewerComponent,
        UxDocumentViewerDirective] }); })();
/*@__PURE__*/ (function () { i0.ɵsetClassMetadata(UxDocumentViewerComponentModule, [{
        type: NgModule,
        args: [{
                imports: [
                    CommonModule,
                    PdfJsViewerModule,
                ],
                declarations: [
                    UxDocumentViewerComponent,
                    UxDocumentViewerDirective,
                ],
                exports: [
                    UxDocumentViewerComponent,
                    UxDocumentViewerDirective,
                ],
                providers: [
                    UxDocumentViewerService,
                ],
                entryComponents: [
                    UxDocumentViewerComponent,
                ],
            }]
    }], null, null); })();

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

export { UxDocumentViewerComponent, UxDocumentViewerComponentModule, UxDocumentViewerConfig, UxDocumentViewerDirective, UxDocumentViewerService };
//# sourceMappingURL=eui-externals-ux-document-viewer.js.map
