import * as i1 from '@angular/cdk/overlay';
import { CdkOverlayOrigin, CdkConnectedOverlay, OverlayModule } from '@angular/cdk/overlay';
import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { EventEmitter, ElementRef, Component, ChangeDetectionStrategy, ViewEncapsulation, Input, Output, ViewChild, NgModule } from '@angular/core';
import * as i2 from '@deja-js/component/core';
import { Destroy, DejaConnectionPositionPair, MediaModule } from '@deja-js/component/core';
import { coerceBooleanProperty, coerceNumberProperty } from '@angular/cdk/coercion';
import { takeWhile, takeUntil, timer, take } from 'rxjs';

/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
// providers: [ MediaService ],
class DejaOverlayComponent extends Destroy {
    constructor(changeDetectorRef, elementRef, overlayContainer, mediaService) {
        super();
        this.changeDetectorRef = changeDetectorRef;
        this.elementRef = elementRef;
        this.overlayContainer = overlayContainer;
        this.overlayBackdropClass = 'cdk-overlay-transparent-backdrop';
        /** Déclenché lorsque la visibilité du dialog change. */
        this.visibleChange = new EventEmitter();
        /** Déclenché lorsque l'overlay est fermé. */
        this.closed = new EventEmitter();
        /** Internal use */
        this.overlayOffsetX = 0;
        this.overlayOffsetY = 0;
        /** Renvoie une valeur qui indique si le dialog est affiché. */
        this._isVisible = false;
        this._hasBackdrop = true;
        this._width = null;
        this._widthForMobile = '100%';
        this._positions = DejaConnectionPositionPair.default;
        this._isMobile = false;
        this.disableMediaService = false;
        const containerElement = this.overlayContainer.getContainerElement();
        containerElement.addEventListener('contextmenu', (event) => {
            event.preventDefault();
            return false;
        });
        mediaService.isMobile$.pipe(takeWhile(() => !this.disableMediaService), takeUntil(this.destroyed$)).subscribe(value => {
            this._isMobile = value;
            this.updateOriginOverlay();
            this.changeDetectorRef.markForCheck();
        });
    }
    get isVisible() {
        return this._isVisible;
    }
    set isVisible(value) {
        const isVisible = coerceBooleanProperty(value);
        if (this._isVisible !== isVisible) {
            this._isVisible = isVisible;
            const containerElement = this.overlayContainer.getContainerElement();
            containerElement.className = Array.from(containerElement.classList)
                .filter(token => token.startsWith('cdk'))
                .join(' ');
            containerElement.classList.add('deja-overlay-container');
            if (this.isVisible && this.overlayContainerClass) {
                this.overlayContainerClass.split(' ').forEach(className => {
                    containerElement.classList.add(className);
                });
            }
            this.changeDetectorRef.markForCheck();
            this.visibleChange.emit(this._isVisible);
        }
    }
    set hasBackdrop(value) {
        this._hasBackdrop = coerceBooleanProperty(value);
    }
    get hasBackdrop() {
        return this._hasBackdrop;
    }
    /** Renvoie ou définit l'élement sur lequel le menu devra s'aligner */
    set ownerElement(value) {
        this._ownerElement = value;
        this.updateOriginOverlay();
    }
    get positionPairs() {
        return this.positions;
    }
    get positions() {
        if (!this.isMobile) {
            return this._positions;
        }
        else if (this._positionsForMobile) {
            return this._positionsForMobile;
        }
        else {
            return DejaConnectionPositionPair.parse('start top start top');
        }
    }
    set positions(value) {
        this._positions = typeof value === 'string' ? DejaConnectionPositionPair.parse(value) : value;
    }
    /** Si pas null, sera utilisé quand isMobile est vrai. Si null et si isMobile est vrai,
     *  alors c'est la valeur 'start top start top' qui est utilisée.
     * */
    set positionsForMobile(value) {
        this._positionsForMobile = typeof value === 'string' ? DejaConnectionPositionPair.parse(value) : value;
    }
    get isMobile() {
        return this._isMobile;
    }
    set isMobile(value) {
        this._isMobile = coerceBooleanProperty(value);
        this.updateOriginOverlay();
        this.disableMediaService = true;
    }
    get width() {
        return this._width;
    }
    set width(width) {
        this._width = coerceNumberProperty(width);
    }
    get widthForMobile() {
        return this._widthForMobile;
    }
    set widthForMobile(widthForMobile) {
        this._widthForMobile = widthForMobile;
    }
    get overlayWidth() {
        if (!this.isMobile) {
            return this._width;
        }
        else {
            return this._widthForMobile;
        }
    }
    updatePosition() {
        var _a, _b;
        (_b = (_a = this.overlay) === null || _a === void 0 ? void 0 : _a.overlayRef) === null || _b === void 0 ? void 0 : _b.updatePosition();
    }
    /** Affiche le dialog. */
    show(eventOrOffsetX, offsetY) {
        this.overlayOffsetX = offsetY !== undefined ? +eventOrOffsetX : 0;
        this.overlayOffsetY = offsetY || 0;
        const e = eventOrOffsetX;
        const target = e === null || e === void 0 ? void 0 : e.target;
        this.overlayOrigin = new CdkOverlayOrigin(new ElementRef((this.isMobile && document.body) || target || this.ownerElement || this.elementRef.nativeElement));
        this.isVisible = true;
        this.changeDetectorRef.markForCheck();
        timer(1).pipe(take(1), takeUntil(this.destroyed$)).subscribe(() => this.updatePosition());
    }
    /** Ferme le dialog. */
    close() {
        this.isVisible = false;
        this.closed.emit(true);
        this.changeDetectorRef.markForCheck();
    }
    updateOriginOverlay() {
        this.overlayOrigin = new CdkOverlayOrigin(new ElementRef((this.isMobile && document.body) || this._ownerElement || this.elementRef.nativeElement));
    }
}
DejaOverlayComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaOverlayComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i0.ElementRef }, { token: i1.OverlayContainer }, { token: i2.MediaService }], target: i0.ɵɵFactoryTarget.Component });
DejaOverlayComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.1.3", type: DejaOverlayComponent, selector: "deja-overlay", inputs: { overlayBackdropClass: "overlayBackdropClass", overlayContainerClass: "overlayContainerClass", overlayOffsetX: "overlayOffsetX", overlayOffsetY: "overlayOffsetY", isVisible: "isVisible", hasBackdrop: "hasBackdrop", ownerElement: "ownerElement", positions: "positions", positionsForMobile: "positionsForMobile", isMobile: "isMobile", width: "width", widthForMobile: "widthForMobile" }, outputs: { visibleChange: "visibleChange", closed: "closed" }, viewQueries: [{ propertyName: "overlay", first: true, predicate: CdkConnectedOverlay, descendants: true, static: true }], usesInheritance: true, ngImport: i0, template: "<ng-template #overlayCmp cdk-connected-overlay [cdkConnectedOverlayHasBackdrop]=\"hasBackdrop\" [cdkConnectedOverlayBackdropClass]=\"overlayBackdropClass\" [cdkConnectedOverlayOpen]=\"!!isVisible\" [cdkConnectedOverlayOffsetY]=\"overlayOffsetY\" [cdkConnectedOverlayOffsetX]=\"overlayOffsetX\" [cdkConnectedOverlayOrigin]=\"overlayOrigin\" (backdropClick)=\"close()\" (detach)=\"close()\" [cdkConnectedOverlayPositions]=\"positionPairs\" [cdkConnectedOverlayWidth]=\"overlayWidth\">\n    <ng-content></ng-content>\n</ng-template>\n", styles: ["@media print{.deja-overlay-container{display:none}}\n"], dependencies: [{ kind: "directive", type: i1.CdkConnectedOverlay, selector: "[cdk-connected-overlay], [connected-overlay], [cdkConnectedOverlay]", inputs: ["cdkConnectedOverlayOrigin", "cdkConnectedOverlayPositions", "cdkConnectedOverlayPositionStrategy", "cdkConnectedOverlayOffsetX", "cdkConnectedOverlayOffsetY", "cdkConnectedOverlayWidth", "cdkConnectedOverlayHeight", "cdkConnectedOverlayMinWidth", "cdkConnectedOverlayMinHeight", "cdkConnectedOverlayBackdropClass", "cdkConnectedOverlayPanelClass", "cdkConnectedOverlayViewportMargin", "cdkConnectedOverlayScrollStrategy", "cdkConnectedOverlayOpen", "cdkConnectedOverlayDisableClose", "cdkConnectedOverlayTransformOriginOn", "cdkConnectedOverlayHasBackdrop", "cdkConnectedOverlayLockPosition", "cdkConnectedOverlayFlexibleDimensions", "cdkConnectedOverlayGrowAfterOpen", "cdkConnectedOverlayPush"], outputs: ["backdropClick", "positionChange", "attach", "detach", "overlayKeydown", "overlayOutsideClick"], exportAs: ["cdkConnectedOverlay"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaOverlayComponent, decorators: [{
            type: Component,
            args: [{ changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, selector: 'deja-overlay', template: "<ng-template #overlayCmp cdk-connected-overlay [cdkConnectedOverlayHasBackdrop]=\"hasBackdrop\" [cdkConnectedOverlayBackdropClass]=\"overlayBackdropClass\" [cdkConnectedOverlayOpen]=\"!!isVisible\" [cdkConnectedOverlayOffsetY]=\"overlayOffsetY\" [cdkConnectedOverlayOffsetX]=\"overlayOffsetX\" [cdkConnectedOverlayOrigin]=\"overlayOrigin\" (backdropClick)=\"close()\" (detach)=\"close()\" [cdkConnectedOverlayPositions]=\"positionPairs\" [cdkConnectedOverlayWidth]=\"overlayWidth\">\n    <ng-content></ng-content>\n</ng-template>\n", styles: ["@media print{.deja-overlay-container{display:none}}\n"] }]
        }], ctorParameters: function () { return [{ type: i0.ChangeDetectorRef }, { type: i0.ElementRef }, { type: i1.OverlayContainer }, { type: i2.MediaService }]; }, propDecorators: { overlayBackdropClass: [{
                type: Input
            }], overlayContainerClass: [{
                type: Input
            }], visibleChange: [{
                type: Output
            }], closed: [{
                type: Output
            }], overlayOffsetX: [{
                type: Input
            }], overlayOffsetY: [{
                type: Input
            }], overlay: [{
                type: ViewChild,
                args: [CdkConnectedOverlay, { static: true }]
            }], isVisible: [{
                type: Input
            }], hasBackdrop: [{
                type: Input
            }], ownerElement: [{
                type: Input
            }], positions: [{
                type: Input
            }], positionsForMobile: [{
                type: Input
            }], isMobile: [{
                type: Input
            }], width: [{
                type: Input
            }], widthForMobile: [{
                type: Input
            }] } });

/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
class DejaOverlayModule {
}
DejaOverlayModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaOverlayModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
DejaOverlayModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.1.3", ngImport: i0, type: DejaOverlayModule, declarations: [DejaOverlayComponent], imports: [CommonModule,
        MediaModule,
        OverlayModule], exports: [DejaOverlayComponent] });
DejaOverlayModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaOverlayModule, imports: [CommonModule,
        MediaModule,
        OverlayModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaOverlayModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [DejaOverlayComponent],
                    exports: [DejaOverlayComponent],
                    imports: [
                        CommonModule,
                        MediaModule,
                        OverlayModule
                    ]
                }]
        }] });

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

export { DejaOverlayComponent, DejaOverlayModule };
//# sourceMappingURL=deja-js-component-overlay.mjs.map
