import * as i2 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { Injectable, EventEmitter, Component, ViewEncapsulation, Input, Output, ContentChild, Directive, NgModule } from '@angular/core';
import { Destroy, DejaConnectionPositionPair, MediaModule } from '@deja-js/component/core';
import * as i3 from '@deja-js/component/overlay';
import { DejaOverlayModule } from '@deja-js/component/overlay';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { Position, Rect } from '@deja-js/component/core/graphics';
import { from, tap, fromEvent, takeUntil, debounceTime, map, filter, delay, switchMap, of } 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
 */
/**
 * Service to pass some params through tooltip module
 */
class DejaTooltipService {
    constructor() {
        /** Tooltip params */
        this.params = {};
    }
}
DejaTooltipService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTooltipService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
DejaTooltipService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTooltipService });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTooltipService, decorators: [{
            type: Injectable
        }] });

/*
 *  @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
 */
/**
 * Customizable tooltip component for Angular
 */
class DejaTooltipComponent extends Destroy {
    /**
     * Constructor
     * Subscribe to mouseover to know when tooltip must disappear.
     */
    constructor(elementRef, tooltipService) {
        super();
        this.tooltipService = tooltipService;
        /** Event Emmited when hide action is called */
        this.hide = new EventEmitter();
        this.overlayVisible = false;
        /**
         * This position config ensures that the top "start" corner of the overlay
         * is aligned with with the top "start" of the origin by default (overlapping
         * the trigger completely). If the panel cannot fit below the trigger, it
         * will fall back to a position above the trigger.
         */
        this._positions = [
            {
                originX: 'center',
                originY: 'bottom',
                overlayX: 'center',
                overlayY: 'top'
            },
            {
                originX: 'center',
                originY: 'top',
                overlayX: 'center',
                overlayY: 'bottom'
            },
            {
                originX: 'start',
                originY: 'bottom',
                overlayX: 'start',
                overlayY: 'top'
            },
            {
                originX: 'start',
                originY: 'top',
                overlayX: 'start',
                overlayY: 'bottom'
            },
            {
                originX: 'end',
                originY: 'bottom',
                overlayX: 'end',
                overlayY: 'top'
            },
            {
                originX: 'end',
                originY: 'top',
                overlayX: 'end',
                overlayY: 'bottom'
            }
        ];
        this._closeOnMoveOver = false;
        const element = elementRef.nativeElement;
        const hide$ = from(this.hide).pipe(tap(() => {
            this._model = undefined;
        }));
        fromEvent(element.ownerDocument, 'mousemove').pipe(
        // eslint-disable-next-line rxjs/no-unsafe-takeuntil
        takeUntil(hide$), debounceTime(100), map(event => new Position(event.pageX, event.pageY)), filter(position => {
            if (this._closeOnMoveOver) {
                return true;
            }
            const containerElement = document.elementFromPoint(position.left, position.top);
            let parentElement = containerElement;
            // eslint-disable-next-line no-loops/no-loops
            while (parentElement) {
                if (parentElement.className === 'cdk-overlay-pane') {
                    return false;
                }
                parentElement = parentElement.parentElement;
            }
            return true;
        }), filter(position => {
            if (this._closeOnMoveOver) {
                return true;
            }
            const ownerElement = this.params.ownerElement.nativeElement || this.params.ownerElement;
            const ownerRect = new Rect(ownerElement.getBoundingClientRect());
            return !ownerRect.containsPoint(position);
        }), delay(300), takeUntil(this.destroyed$)).subscribe(() => {
            this.hide.emit();
            this.overlayVisible = false;
        });
    }
    set closeOnMoveOver(value) {
        this._closeOnMoveOver = coerceBooleanProperty(value);
    }
    get closeOnMoveOver() {
        return this._closeOnMoveOver;
    }
    set positions(value) {
        this._positions = typeof value === 'string' ? DejaConnectionPositionPair.parse(value) : value;
    }
    get positions() {
        return this._positions;
    }
    get model() {
        return this._model;
    }
    /**
     * Init tooltip configuration
     * Check if ng-template model passed through param is an observable or a promise and resolve it before set.
     */
    ngOnInit() {
        if (!this.name) {
            throw (new Error('Name is required'));
        }
        this.params = this.tooltipService.params[this.name];
        this.ownerElement = this.params.ownerElement.nativeElement || this.params.ownerElement;
        const model$ = this.params.model;
        if (!model$) {
            this._model = undefined;
            this.overlayVisible = true;
        }
        else if (model$.subscribe) {
            model$.pipe(takeUntil(this.destroyed$)).subscribe(model => {
                this._model = model;
                this.overlayVisible = true;
            }, () => {
                this.hide.emit();
                this.overlayVisible = false;
            });
        }
        else {
            const promise = this.params.model;
            if (promise.then) {
                promise
                    .then(model => {
                    this._model = model;
                    this.overlayVisible = true;
                })
                    .catch(() => {
                    this.hide.emit();
                    this.overlayVisible = false;
                });
            }
            else {
                this._model = this.params.model;
                this.overlayVisible = true;
            }
        }
    }
}
DejaTooltipComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTooltipComponent, deps: [{ token: i0.ElementRef }, { token: DejaTooltipService }], target: i0.ɵɵFactoryTarget.Component });
DejaTooltipComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.1.3", type: DejaTooltipComponent, selector: "deja-tooltip", inputs: { name: "name", closeOnMoveOver: "closeOnMoveOver", positions: "positions" }, outputs: { hide: "hide" }, queries: [{ propertyName: "tooltipTemplate", first: true, predicate: ["tooltipTemplate"], descendants: true }], usesInheritance: true, ngImport: i0, template: "<deja-overlay [overlayOffsetX]=\"0\" [overlayOffsetY]=\"10\" [ownerElement]=\"ownerElement\" [isVisible]=\"overlayVisible\" [positions]=\"positions\" hasBackdrop=\"false\" isMobile=\"false\">\n    <ng-template *ngIf=\"!!tooltipTemplate\" [ngTemplateOutlet]=\"tooltipTemplate\" [ngTemplateOutletContext]=\"{ $implicit: model }\"></ng-template>\n    <ng-container *ngIf=\"!tooltipTemplate\">\n        <ng-content></ng-content>\n    </ng-container>\n</deja-overlay>\n", styles: [".deja-overlay-container #tooltip{font-size:.8rem;border:1px solid #888;padding:.5rem}\n"], dependencies: [{ kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i2.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "component", type: i3.DejaOverlayComponent, selector: "deja-overlay", inputs: ["overlayBackdropClass", "overlayContainerClass", "overlayOffsetX", "overlayOffsetY", "isVisible", "hasBackdrop", "ownerElement", "positions", "positionsForMobile", "isMobile", "width", "widthForMobile"], outputs: ["visibleChange", "closed"] }], encapsulation: i0.ViewEncapsulation.None });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTooltipComponent, decorators: [{
            type: Component,
            args: [{ encapsulation: ViewEncapsulation.None, selector: 'deja-tooltip', template: "<deja-overlay [overlayOffsetX]=\"0\" [overlayOffsetY]=\"10\" [ownerElement]=\"ownerElement\" [isVisible]=\"overlayVisible\" [positions]=\"positions\" hasBackdrop=\"false\" isMobile=\"false\">\n    <ng-template *ngIf=\"!!tooltipTemplate\" [ngTemplateOutlet]=\"tooltipTemplate\" [ngTemplateOutletContext]=\"{ $implicit: model }\"></ng-template>\n    <ng-container *ngIf=\"!tooltipTemplate\">\n        <ng-content></ng-content>\n    </ng-container>\n</deja-overlay>\n", styles: [".deja-overlay-container #tooltip{font-size:.8rem;border:1px solid #888;padding:.5rem}\n"] }]
        }], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: DejaTooltipService }]; }, propDecorators: { name: [{
                type: Input
            }], hide: [{
                type: Output
            }], tooltipTemplate: [{
                type: ContentChild,
                args: ['tooltipTemplate']
            }], closeOnMoveOver: [{
                type: Input
            }], positions: [{
                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 DejaTooltipDirective extends Destroy {
    constructor(elementRef, tooltipService) {
        super();
        // eslint-disable-next-line @angular-eslint/no-input-rename
        this.delay = 600;
        // eslint-disable-next-line @angular-eslint/no-output-rename, @angular-eslint/no-output-native
        this.show = new EventEmitter();
        const element = elementRef.nativeElement;
        const leave$ = fromEvent(element, 'mouseleave');
        fromEvent(element, 'mouseenter').pipe(switchMap(e => of(e).pipe(delay(this.delay), takeUntil(leave$))), takeUntil(this.destroyed$)).subscribe(() => {
            tooltipService.params[this.name] = {
                model: this.model,
                ownerElement: elementRef,
                positions: this.positions
            };
            this.show.emit();
        });
    }
}
DejaTooltipDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTooltipDirective, deps: [{ token: i0.ElementRef }, { token: DejaTooltipService }], target: i0.ɵɵFactoryTarget.Directive });
DejaTooltipDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "14.1.3", type: DejaTooltipDirective, selector: "[deja-tooltip]", inputs: { delay: ["tooltip-delay", "delay"], model: ["tooltip-model", "model"], name: ["deja-tooltip", "name"], positions: ["tooltip-positions", "positions"] }, outputs: { show: "tooltip-show" }, usesInheritance: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTooltipDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[deja-tooltip]'
                }]
        }], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: DejaTooltipService }]; }, propDecorators: { delay: [{
                type: Input,
                args: ['tooltip-delay']
            }], model: [{
                type: Input,
                args: ['tooltip-model']
            }], name: [{
                type: Input,
                args: ['deja-tooltip']
            }], positions: [{
                type: Input,
                args: ['tooltip-positions']
            }], show: [{
                type: Output,
                args: ['tooltip-show']
            }] } });

/*
 *  @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 DejaTooltipModule {
}
DejaTooltipModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTooltipModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
DejaTooltipModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.1.3", ngImport: i0, type: DejaTooltipModule, declarations: [DejaTooltipComponent,
        DejaTooltipDirective], imports: [CommonModule,
        DejaOverlayModule,
        MediaModule], exports: [DejaTooltipComponent,
        DejaTooltipDirective] });
DejaTooltipModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTooltipModule, providers: [
        DejaTooltipService
    ], imports: [CommonModule,
        DejaOverlayModule,
        MediaModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTooltipModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [
                        DejaTooltipComponent,
                        DejaTooltipDirective
                    ],
                    exports: [
                        DejaTooltipComponent,
                        DejaTooltipDirective
                    ],
                    imports: [
                        CommonModule,
                        DejaOverlayModule,
                        MediaModule
                    ],
                    providers: [
                        DejaTooltipService
                    ]
                }]
        }] });

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

export { DejaTooltipComponent, DejaTooltipDirective, DejaTooltipModule, DejaTooltipService };
//# sourceMappingURL=deja-js-component-tooltip.mjs.map
