import * as i0 from '@angular/core';
import { forwardRef, PLATFORM_ID, Directive, Inject, Optional, HostBinding, HostListener, EventEmitter, ContentChild, Input, Output, NgModule } from '@angular/core';
import { BS_DEVELOPMENT } from '@mintplayer/ng-bootstrap';
import { Subject, takeUntil, take, BehaviorSubject } from 'rxjs';
import { TemplatePortal } from '@angular/cdk/portal';
import { ClickOutsideDirective, ClickOutsideModule } from '@mintplayer/ng-click-outside';
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';

// import { BsDropdownComponent } from '../dropdown/dropdown.component';
class BsDropdownMenuDirective extends ClickOutsideDirective {
    constructor(dropdown, 
    // @Inject(forwardRef(() => BsDropdownComponent)) private dropdown: BsDropdownComponent,
    viewContainerRef, templateRef, overlay, elementRef, zone, platformId, bsDevelopment) {
        super(elementRef, zone, platformId);
        this.dropdown = dropdown;
        this.viewContainerRef = viewContainerRef;
        this.templateRef = templateRef;
        this.overlay = overlay;
        this.bsDevelopment = bsDevelopment;
        this.wait = false;
        this.destroyed$ = new Subject();
        this.overlayRef = null;
        this.templatePortal = null;
        this.dropdown.isOpen$
            .pipe(takeUntil(this.destroyed$))
            .subscribe((isOpen) => {
            if (isOpen) {
                this.wait = true;
                setTimeout(() => this.wait = false, 100);
                this.overlayRef = this.overlay.create({
                    hasBackdrop: this.dropdown.hasBackdrop,
                    scrollStrategy: this.overlay.scrollStrategies.reposition(),
                    positionStrategy: this.overlay.position()
                        .flexibleConnectedTo(!this.dropdown.toggle ? dropdown.elementRef : this.dropdown.toggle.toggleButton)
                        .withPositions([
                        // element: BottomLeft - dropdown: TopLeft
                        { originX: "start", originY: "bottom", overlayX: "start", overlayY: "top", offsetY: 0 },
                        // element: TopLeft - dropdown: BottomLeft
                        { originX: "start", originY: "top", overlayX: "start", overlayY: "bottom", offsetY: 0 },
                    ]),
                });
                if (this.dropdown.hasBackdrop && this.dropdown.closeOnClickOutside) {
                    this.overlayRef.backdropClick().subscribe(() => {
                        this.dropdown.isOpen = false;
                    });
                }
                this.templatePortal = new TemplatePortal(this.templateRef, this.viewContainerRef);
                const view = this.overlayRef.attach(this.templatePortal);
                if (this.dropdown.sameDropdownWidth) {
                    const width = this.dropdown.elementRef.nativeElement.offsetWidth;
                    view.rootNodes[0].style.width = width + 'px';
                }
            }
            else {
                if (this.overlayRef) {
                    this.overlayRef.detach();
                    this.overlayRef.dispose();
                    this.overlayRef = null;
                }
            }
        });
    }
    get show() { return this.dropdown.isOpen; }
    clickedOutside(ev) {
        var _a;
        if (!this.bsDevelopment) {
            if (!this.wait) {
                if (!((_a = this.overlayRef) === null || _a === void 0 ? void 0 : _a.overlayElement.contains(ev.target))) {
                    this.doClose();
                }
            }
        }
    }
    onEscape(ev) {
        this.doClose();
    }
    doClose() {
        this.dropdown.isOpen$.pipe(take(1)).subscribe((isOpen) => {
            if (isOpen && !this.dropdown.hasBackdrop && this.dropdown.closeOnClickOutside) {
                this.dropdown.isOpen = false;
            }
        });
    }
}
BsDropdownMenuDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsDropdownMenuDirective, deps: [{ token: forwardRef(() => BsDropdownDirective) }, { token: i0.ViewContainerRef }, { token: i0.TemplateRef }, { token: i1.Overlay }, { token: i0.ElementRef }, { token: i0.NgZone }, { token: PLATFORM_ID }, { token: BS_DEVELOPMENT, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
BsDropdownMenuDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.0.4", type: BsDropdownMenuDirective, selector: "[bsDropdownMenu]", host: { listeners: { "clickOutside": "clickedOutside($event)", "document:keydown.escape": "onEscape($event)" }, properties: { "class.show": "this.show" } }, usesInheritance: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsDropdownMenuDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[bsDropdownMenu]'
                }]
        }], ctorParameters: function () {
        return [{ type: BsDropdownDirective, decorators: [{
                        type: Inject,
                        args: [forwardRef(() => BsDropdownDirective)]
                    }] }, { type: i0.ViewContainerRef }, { type: i0.TemplateRef }, { type: i1.Overlay }, { type: i0.ElementRef }, { type: i0.NgZone }, { type: undefined, decorators: [{
                        type: Inject,
                        args: [PLATFORM_ID]
                    }] }, { type: undefined, decorators: [{
                        type: Optional
                    }, {
                        type: Inject,
                        args: [BS_DEVELOPMENT]
                    }] }];
    }, propDecorators: { show: [{
                type: HostBinding,
                args: ['class.show']
            }], clickedOutside: [{
                type: HostListener,
                args: ['clickOutside', ['$event']]
            }], onEscape: [{
                type: HostListener,
                args: ['document:keydown.escape', ['$event']]
            }] } });

// import { BsDropdownComponent } from '../dropdown/dropdown.component';
class BsDropdownToggleDirective {
    constructor(dropdown, 
    // private dropdown: BsDropdownComponent,
    toggleButton) {
        this.dropdown = dropdown;
        this.toggleButton = toggleButton;
    }
    onClick() {
        this.dropdown.isOpen$.pipe(take(1)).subscribe((isOpen) => {
            this.dropdown.isOpen$.next(!isOpen);
        });
    }
}
BsDropdownToggleDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsDropdownToggleDirective, deps: [{ token: BsDropdownDirective }, { token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
BsDropdownToggleDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.0.4", type: BsDropdownToggleDirective, selector: "[bsDropdownToggle]", host: { listeners: { "click": "onClick()" } }, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsDropdownToggleDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[bsDropdownToggle]'
                }]
        }], ctorParameters: function () { return [{ type: BsDropdownDirective }, { type: i0.ElementRef }]; }, propDecorators: { onClick: [{
                type: HostListener,
                args: ['click']
            }] } });

class BsDropdownDirective {
    constructor(elementRef, bsDevelopment) {
        this.bsDevelopment = bsDevelopment;
        this.isOpen$ = new BehaviorSubject(false);
        this.toggle = null;
        this.hasBackdrop = false;
        this.sameWidth = false;
        this.closeOnClickOutside = true;
        this.sameDropdownWidth = false;
        this.isOpenChange = new EventEmitter();
        this.elementRef = elementRef;
    }
    //#region IsOpen
    get isOpen() {
        return this.isOpen$.value;
    }
    set isOpen(value) {
        if (this.isOpen$.value !== value) {
            this.isOpen$.next(value);
            this.isOpenChange.emit(value);
        }
    }
    //#endregion
    onBlur() {
        if (this.closeOnClickOutside && !this.bsDevelopment) {
            this.isOpen = false;
        }
    }
}
BsDropdownDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsDropdownDirective, deps: [{ token: i0.ElementRef }, { token: BS_DEVELOPMENT }], target: i0.ɵɵFactoryTarget.Directive });
BsDropdownDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.0.4", type: BsDropdownDirective, selector: "[bsDropdown]", inputs: { hasBackdrop: "hasBackdrop", sameWidth: "sameWidth", closeOnClickOutside: "closeOnClickOutside", sameDropdownWidth: "sameDropdownWidth", isOpen: "isOpen" }, outputs: { isOpenChange: "isOpenChange" }, host: { listeners: { "window:blur": "onBlur()" } }, queries: [{ propertyName: "menu", first: true, predicate: BsDropdownMenuDirective, descendants: true }, { propertyName: "toggle", first: true, predicate: BsDropdownToggleDirective, descendants: true }], ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsDropdownDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[bsDropdown]'
                }]
        }], ctorParameters: function () {
        return [{ type: i0.ElementRef }, { type: undefined, decorators: [{
                        type: Inject,
                        args: [BS_DEVELOPMENT]
                    }] }];
    }, propDecorators: { menu: [{
                type: ContentChild,
                args: [BsDropdownMenuDirective, { static: false }]
            }], toggle: [{
                type: ContentChild,
                args: [BsDropdownToggleDirective, { static: false }]
            }], hasBackdrop: [{
                type: Input
            }], sameWidth: [{
                type: Input
            }], closeOnClickOutside: [{
                type: Input
            }], sameDropdownWidth: [{
                type: Input
            }], isOpenChange: [{
                type: Output
            }], isOpen: [{
                type: Input
            }], onBlur: [{
                type: HostListener,
                args: ['window:blur']
            }] } });

// import { BsDropdownComponent } from './dropdown/dropdown.component';
class BsDropdownModule {
}
BsDropdownModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsDropdownModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
BsDropdownModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.0.4", ngImport: i0, type: BsDropdownModule, declarations: [BsDropdownDirective,
        BsDropdownToggleDirective,
        BsDropdownMenuDirective], imports: [CommonModule, OverlayModule, BsHasOverlayModule, ClickOutsideModule], exports: [BsDropdownDirective,
        BsDropdownToggleDirective,
        BsDropdownMenuDirective,
        // BsDropdownComponent,
        BsHasOverlayModule] });
BsDropdownModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsDropdownModule, imports: [CommonModule, OverlayModule, BsHasOverlayModule, ClickOutsideModule,
        // BsDropdownComponent,
        BsHasOverlayModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.4", ngImport: i0, type: BsDropdownModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [
                        BsDropdownDirective,
                        BsDropdownToggleDirective,
                        BsDropdownMenuDirective,
                        // BsDropdownComponent,
                    ],
                    imports: [CommonModule, OverlayModule, BsHasOverlayModule, ClickOutsideModule],
                    exports: [
                        BsDropdownDirective,
                        BsDropdownToggleDirective,
                        BsDropdownMenuDirective,
                        // BsDropdownComponent,
                        BsHasOverlayModule,
                    ],
                }]
        }] });

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

export { BsDropdownDirective, BsDropdownMenuDirective, BsDropdownModule, BsDropdownToggleDirective };
//# sourceMappingURL=mintplayer-ng-bootstrap-dropdown.mjs.map
