import { Directive, HostListener, Inject, Injector, Input, ViewContainerRef } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { POSITION } from '@anglr/common';
import { nameof } from '@jscrpt/common';
import { RelationsDebuggerVisualizerSAComponent } from '../../components';
import * as i0 from "@angular/core";
/**
 * Directive that shows relations debugger
 */
export class ShowRelationsDebuggerSADirective {
    //######################### constructor #########################
    constructor(position, viewContainer, injector, document) {
        this.position = position;
        this.viewContainer = viewContainer;
        this.injector = injector;
        this.document = document;
        /**
         * Definition of relations and their nodes, used for rendering canvas
         */
        this.relationsDefinition = [];
        //######################### public properties - inputs #########################
        /**
         * Delay between steps during automatic play
         */
        this.playDelay = 1500;
    }
    //######################### public methods - implementation of OnChanges #########################
    /**
     * Called when input value changes
     */
    ngOnChanges(changes) {
        if (nameof('playDelay') in changes) {
            if (this.visualizer) {
                this.visualizer.setInput(nameof('playDelay'), this.playDelay);
            }
        }
        if (nameof('relationsDefinition') in changes) {
            if (this.visualizer) {
                this.visualizer.setInput(nameof('relationsDefinition'), this.relationsDefinition);
            }
        }
        if (this.visualizer) {
            this.visualizer.changeDetectorRef.detectChanges();
        }
    }
    //######################### public methods - implementation of OnDestroy #########################
    /**
     * Called when component is destroyed
     */
    ngOnDestroy() {
        this.visualizer?.destroy();
        this.visualizer = null;
    }
    //######################### public methods - host #########################
    /**
     * Shows relations debugger
     * @param event - Event that occured
     */
    show(event) {
        event.preventDefault();
        event.stopPropagation();
        if (ngRelationsDebugger) {
            this.visualizer = this.viewContainer.createComponent(RelationsDebuggerVisualizerSAComponent);
            const visualizerElement = this.visualizer.hostView.rootNodes[0];
            //render to specified target element
            if (this.containerElement) {
                let element = this.document.querySelector(`body ${this.containerElement}`);
                //element does not exists
                if (!element) {
                    const [name, ...css] = this.containerElement.split('.');
                    element = this.document.createElement(name);
                    if (css?.length) {
                        for (const cssClass of css) {
                            element.classList.add(cssClass);
                        }
                    }
                    this.document.body.appendChild(element);
                }
                element.appendChild(visualizerElement);
            }
            //render directly to body
            else {
                this.document.body.appendChild(visualizerElement);
            }
            this.visualizer.instance.close.subscribe(() => this.ngOnDestroy());
            this.visualizer.setInput(nameof('playDelay'), this.playDelay);
            this.visualizer.setInput(nameof('relationsDefinition'), this.relationsDefinition);
            this.visualizer.changeDetectorRef.markForCheck();
        }
    }
}
ShowRelationsDebuggerSADirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ShowRelationsDebuggerSADirective, deps: [{ token: POSITION }, { token: i0.ViewContainerRef }, { token: i0.Injector }, { token: DOCUMENT }], target: i0.ɵɵFactoryTarget.Directive });
ShowRelationsDebuggerSADirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.0.4", type: ShowRelationsDebuggerSADirective, isStandalone: true, selector: "[showRelationsDebugger]", inputs: { containerElement: "containerElement", relationsDefinition: "relationsDefinition", playDelay: "playDelay" }, host: { listeners: { "click": "show($event)" } }, usesOnChanges: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ShowRelationsDebuggerSADirective, decorators: [{
            type: Directive,
            args: [{
                    selector: '[showRelationsDebugger]',
                    standalone: true,
                }]
        }], ctorParameters: () => [{ type: undefined, decorators: [{
                    type: Inject,
                    args: [POSITION]
                }] }, { type: i0.ViewContainerRef }, { type: i0.Injector }, { type: Document, decorators: [{
                    type: Inject,
                    args: [DOCUMENT]
                }] }], propDecorators: { containerElement: [{
                type: Input
            }], relationsDefinition: [{
                type: Input
            }], playDelay: [{
                type: Input
            }], show: [{
                type: HostListener,
                args: ['click', ['$event']]
            }] } });
//# sourceMappingURL=showRelationsDebugger.directive.js.map