import * as i0 from '@angular/core';
import { Input, Directive, HostListener, Attribute, inject, NgModule } from '@angular/core';
import * as i1 from '@esfaenza/extensions';
import { AppEmbeddingExtensions } from '@esfaenza/extensions';
import 'jquery-slimscroll';
import * as i2 from '@angular/router';
import { RouterLinkWithHref } from '@angular/router';
import { EMBEDDED_ROUTE, ROUTE } from '@esfaenza/core/domain';
import { take } from 'rxjs';
import * as i3 from '@angular/common';
import { ModalDirective } from 'ngx-bootstrap/modal';

// Angular
class Draggable {
    constructor(_elementRef, zone, emb) {
        this._elementRef = _elementRef;
        this.zone = zone;
        this.emb = emb;
    }
    ngOnInit() {
        this.zone.runOutsideAngular(() => {
            var handle = this.handlerSelector || '.modal-header';
            jQuery(this._elementRef.nativeElement).children(':first-child').draggable({
                handle,
                helper: function (_, __) {
                    var oBody = jQuery(document).find('body');
                    var containmentDiv = jQuery('<div id="div_containment"></div>');
                    containmentDiv
                        .css('z-index', "-99999")
                        .css('top', "0")
                        .css('left', "0")
                        .css('width', "100%")
                        .css('height', "100%")
                        .css('position', 'absolute');
                    containmentDiv.appendTo(oBody);
                    return this;
                },
                stop: function (_, __) {
                    jQuery(document).filter('body').find('#div_containment').remove();
                },
                containment: this.emb.Embedded ? "#div_containment" : null
            });
        });
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: Draggable, deps: [{ token: i0.ElementRef }, { token: i0.NgZone }, { token: i1.AppEmbeddingExtensions }], target: i0.ɵɵFactoryTarget.Directive }); }
    static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.25", type: Draggable, isStandalone: true, selector: "[emaDraggable]", inputs: { handlerSelector: ["emaDraggable", "handlerSelector"] }, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: Draggable, decorators: [{
            type: Directive,
            args: [{
                    selector: "[emaDraggable]",
                    standalone: true
                }]
        }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i0.NgZone }, { type: i1.AppEmbeddingExtensions }], propDecorators: { handlerSelector: [{
                type: Input,
                args: ['emaDraggable']
            }] } });

// Angular
class SlimScroll {
    constructor(_elementRef, zone) {
        this._elementRef = _elementRef;
        this.zone = zone;
    }
    ngOnChanges(changes) {
        this._scroll();
    }
    _scroll() {
        this._destroy();
        this._init();
    }
    //Stacco slimscroll da Angular dato che registra dei listener sull'oggetto in cui viene creato che spammerebbero la change detection
    _init() {
        this.zone.runOutsideAngular(() => {
            jQuery(this._elementRef.nativeElement).slimScroll(this.slimScrollOptions);
        });
    }
    _destroy() {
        this.zone.runOutsideAngular(() => {
            jQuery(this._elementRef.nativeElement).slimScroll({ destroy: true });
        });
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: SlimScroll, deps: [{ token: i0.ElementRef }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Directive }); }
    static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.25", type: SlimScroll, isStandalone: true, selector: "[slim-scroll]", inputs: { slimScrollOptions: "slimScrollOptions" }, usesOnChanges: true, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: SlimScroll, decorators: [{
            type: Directive,
            args: [{
                    selector: "[slim-scroll]",
                    standalone: true
                }]
        }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i0.NgZone }], propDecorators: { slimScrollOptions: [{
                type: Input
            }] } });

class esRouterLinkWithHref extends RouterLinkWithHref {
    constructor(emb, router, route, tabIndexAttribute, renderer, el, locationStrategy) {
        super(router, route, tabIndexAttribute, renderer, el, locationStrategy);
        this.emb = emb;
    }
    set esRouterLink(commandsOrUrlTree) {
        // UrlTree non è supportato because fuck you
        this.emb.EmbeddingSource.pipe(take(1)).subscribe(s => {
            this.EmbeddingSource = s;
            if (this.emb.Embedded && !s)
                console.error("Link creato in contesto di embedding ma senza embedding source!!");
            let BasePath = "/" + (this.emb.Embedded ? EMBEDDED_ROUTE + (!!s ? "/" + s : "") : ROUTE);
            if (commandsOrUrlTree != null) {
                this.routerLinkInput = Array.isArray(commandsOrUrlTree) ? commandsOrUrlTree : [commandsOrUrlTree];
                let route = this.routerLinkInput[0];
                if (!route.startsWith("/" + EMBEDDED_ROUTE) && !route.startsWith("/" + ROUTE))
                    this.routerLinkInput[0] = BasePath + this.routerLinkInput[0];
                this.setTabIndexIfNotOnNativeEl('0');
            }
            else {
                this.routerLinkInput = null;
                this.setTabIndexIfNotOnNativeEl(null);
            }
        });
    }
    onClick(button, ctrlKey, shiftKey, altKey, metaKey) {
        if (this.emb.Embedded) {
            let url = this.urlTree.toString().replace(EMBEDDED_ROUTE + (!!this.EmbeddingSource ? "/" + this.EmbeddingSource : ""), ROUTE + "/embed/" + myconfig.AppData.ModuleId);
            this.emb.MainWindow.postMessage({ type: "navigate", url, newTab: ctrlKey, replaceState: !ctrlKey, current: this.router.url }, "*");
            if (!ctrlKey)
                return super.onClick(button, ctrlKey, shiftKey, altKey, metaKey);
            else
                return false;
        }
        else
            return super.onClick(button, ctrlKey, shiftKey, altKey, metaKey);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: esRouterLinkWithHref, deps: [{ token: i1.AppEmbeddingExtensions }, { token: i2.Router }, { token: i2.ActivatedRoute }, { token: 'tabindex', attribute: true }, { token: i0.Renderer2 }, { token: i0.ElementRef }, { token: i3.LocationStrategy }], target: i0.ɵɵFactoryTarget.Directive }); }
    static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.25", type: esRouterLinkWithHref, isStandalone: true, selector: "a[esRouterLink],area[esRouterLink]", inputs: { esRouterLink: "esRouterLink" }, host: { listeners: { "click": "onClick($event.button,$event.ctrlKey,$event.shiftKey,$event.altKey,$event.metaKey)" } }, usesInheritance: true, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: esRouterLinkWithHref, decorators: [{
            type: Directive,
            args: [{
                    selector: 'a[esRouterLink],area[esRouterLink]',
                    standalone: true
                }]
        }], ctorParameters: () => [{ type: i1.AppEmbeddingExtensions }, { type: i2.Router }, { type: i2.ActivatedRoute }, { type: undefined, decorators: [{
                    type: Attribute,
                    args: ['tabindex']
                }] }, { type: i0.Renderer2 }, { type: i0.ElementRef }, { type: i3.LocationStrategy }], propDecorators: { esRouterLink: [{
                type: Input
            }], onClick: [{
                type: HostListener,
                args: ['click', ['$event.button', '$event.ctrlKey', '$event.shiftKey', '$event.altKey', '$event.metaKey']]
            }] } });

// Angular
class Widget {
    constructor(el) {
        this.$el = jQuery(el.nativeElement);
        jQuery.fn.widgster.Constructor.DEFAULTS.bodySelector = ".widget-body";
        /*
         When widget is closed remove its parent if it is .col-*
         */
        jQuery(document).on("close.widgster", (e) => {
            let $colWrap = jQuery(e.target).closest(' [class*="col-"]:not(.widget-container)');
            // remove colWrap only if there are no more widgets inside
            if (!$colWrap.find(".widget").not(e.target).length) {
                $colWrap.remove();
            }
        });
        jQuery(document).on("fullscreened.widgster", (e) => {
            jQuery(e.target).find("div.widget-body").addClass("scrolling");
        }).on("restored.widgster", (e) => {
            jQuery(e.target).find("div.widget-body").removeClass("scrolling");
        });
    }
    ngOnInit() {
        this.$el.widgster();
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: Widget, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive }); }
    static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.25", type: Widget, isStandalone: true, selector: "[widget]", ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: Widget, decorators: [{
            type: Directive,
            args: [{
                    selector: "[widget]",
                    standalone: true
                }]
        }], ctorParameters: () => [{ type: i0.ElementRef }] });

class ComponentHostDirective {
    constructor(viewContainerRef) {
        this.viewContainerRef = viewContainerRef;
        this.Inputs = null;
    }
    ngOnInit() {
        this.CreatedComponent = this.viewContainerRef.createComponent(this.componentHost);
        if (!this.Inputs)
            return;
        for (const key in this.Inputs)
            this.CreatedComponent.instance[key] = this.Inputs[key];
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: ComponentHostDirective, deps: [{ token: i0.ViewContainerRef }], target: i0.ɵɵFactoryTarget.Directive }); }
    static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.25", type: ComponentHostDirective, isStandalone: true, selector: "[componentHost]", inputs: { componentHost: "componentHost", Inputs: "Inputs" }, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: ComponentHostDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[componentHost]',
                    standalone: true
                }]
        }], ctorParameters: () => [{ type: i0.ViewContainerRef }], propDecorators: { componentHost: [{
                type: Input
            }], Inputs: [{
                type: Input
            }] } });

// Angular
class EsModalDirective extends ModalDirective {
    constructor() {
        super(...arguments);
        this.emb = inject(AppEmbeddingExtensions);
    }
    ngOnInit() {
        this.elementRef = this._element;
        this.rendererRef = this._renderer;
        super.ngOnInit();
    }
    show() {
        super.show();
        if (this.emb.Embedded)
            this.emb.onEmbeddedDialogShow(this);
    }
    hide() {
        super.hide();
        if (this.emb.Embedded)
            this.emb.onEmbeddedDialogHide(this);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: EsModalDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive }); }
    static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.25", type: EsModalDirective, isStandalone: true, selector: "[esModal]", exportAs: ["es-modal"], usesInheritance: true, ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: EsModalDirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[esModal]',
                    exportAs: 'es-modal',
                    standalone: true
                }]
        }] });

// Angular
const DIRECTIVES = [Draggable, SlimScroll, Widget, esRouterLinkWithHref, ComponentHostDirective, EsModalDirective];
class DirectivesModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: DirectivesModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.25", ngImport: i0, type: DirectivesModule, imports: [Draggable, SlimScroll, Widget, esRouterLinkWithHref, ComponentHostDirective, EsModalDirective], exports: [Draggable, SlimScroll, Widget, esRouterLinkWithHref, ComponentHostDirective, EsModalDirective] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: DirectivesModule }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.25", ngImport: i0, type: DirectivesModule, decorators: [{
            type: NgModule,
            args: [{ imports: [...DIRECTIVES], exports: [...DIRECTIVES] }]
        }] });

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

export { ComponentHostDirective, DirectivesModule, Draggable, EsModalDirective, SlimScroll, Widget, esRouterLinkWithHref };
//# sourceMappingURL=esfaenza-core-directives.mjs.map
