import { TemplatePortal } from '@angular/cdk/portal';
import * as i0 from '@angular/core';
import { ElementRef, Directive, Host, SkipSelf, HostListener, NgModule } from '@angular/core';
import * as i1 from '@angular/cdk/overlay';
import { OverlayModule } from '@angular/cdk/overlay';
import { CommonModule } from '@angular/common';
import { BsHasOverlayModule } from '@mintplayer/ng-bootstrap/has-overlay';

class BsContextMenuDirective {
    constructor(overlay, templateRef, viewContainerRef, element) {
        this.overlay = overlay;
        this.templateRef = templateRef;
        this.viewContainerRef = viewContainerRef;
        this.element = element;
        this.overlayRef = null;
        this.templatePortal = null;
        this.element.nativeElement.oncontextmenu = (ev) => {
            ev.preventDefault();
            this.checkAndCloseExisting(ev);
            const target = {
                getBoundingClientRect: () => {
                    return ({
                        width: 0,
                        height: 0,
                        top: ev.clientY,
                        left: ev.clientX,
                        bottom: ev.clientY,
                        right: ev.clientX,
                    });
                },
            };
            const element = new ElementRef(target);
            this.overlayRef = this.overlay.create({
                hasBackdrop: false,
                scrollStrategy: this.overlay.scrollStrategies.close(),
                positionStrategy: this.overlay.position()
                    .flexibleConnectedTo(element)
                    .withPositions([
                    { originX: "end", originY: "top", overlayX: "start", overlayY: "top" },
                    { originX: "end", originY: "bottom", overlayX: "start", overlayY: "bottom" },
                    { originX: "start", originY: "top", overlayX: "end", overlayY: "top" },
                    { originX: "start", originY: "bottom", overlayX: "end", overlayY: "bottom" },
                ])
            });
            this.templatePortal = new TemplatePortal(this.templateRef, this.viewContainerRef);
            const view = this.overlayRef.attach(this.templatePortal);
            view.rootNodes.forEach(node => node.classList.add('position-static'));
        };
    }
    clickAnywhere(ev) {
        this.checkAndCloseExisting(ev);
    }
    onBlur() {
        this.close();
    }
    close() {
        if (this.overlayRef) {
            this.overlayRef.detach();
            this.overlayRef.dispose();
            this.overlayRef = null;
        }
    }
    checkAndCloseExisting(ev) {
        if (this.overlayRef && !this.overlayRef.overlayElement.contains(ev.target)) {
            this.close();
        }
    }
}
BsContextMenuDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsContextMenuDirective, deps: [{ token: i1.Overlay }, { token: i0.TemplateRef }, { token: i0.ViewContainerRef }, { token: i0.ElementRef, host: true, skipSelf: true }], target: i0.ɵɵFactoryTarget.Directive });
BsContextMenuDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.2.7", type: BsContextMenuDirective, selector: "[bsContextMenu]", host: { listeners: { "document:click": "clickAnywhere($event)", "window:blur": "onBlur()" } }, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsContextMenuDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[bsContextMenu]'
                }]
        }], ctorParameters: function () {
        return [{ type: i1.Overlay }, { type: i0.TemplateRef }, { type: i0.ViewContainerRef }, { type: i0.ElementRef, decorators: [{
                        type: Host
                    }, {
                        type: SkipSelf
                    }] }];
    }, propDecorators: { clickAnywhere: [{
                type: HostListener,
                args: ['document:click', ['$event']]
            }], onBlur: [{
                type: HostListener,
                args: ['window:blur']
            }] } });

class BsContextMenuModule {
}
BsContextMenuModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsContextMenuModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
BsContextMenuModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.2.7", ngImport: i0, type: BsContextMenuModule, declarations: [BsContextMenuDirective], imports: [CommonModule,
        BsHasOverlayModule,
        OverlayModule], exports: [BsContextMenuDirective,
        BsHasOverlayModule] });
BsContextMenuModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsContextMenuModule, imports: [CommonModule,
        BsHasOverlayModule,
        OverlayModule, BsHasOverlayModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.7", ngImport: i0, type: BsContextMenuModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [
                        BsContextMenuDirective
                    ],
                    imports: [
                        CommonModule,
                        BsHasOverlayModule,
                        OverlayModule
                    ],
                    exports: [
                        BsContextMenuDirective,
                        BsHasOverlayModule
                    ]
                }]
        }] });

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

export { BsContextMenuDirective, BsContextMenuModule };
//# sourceMappingURL=mintplayer-ng-bootstrap-context-menu.mjs.map
