import { Component, ChangeDetectionStrategy, HostListener } from '@angular/core';
import { RelationNodeEndpointBase } from '../relationsNodeEndpointBase';
import { INVALIDATE_DROP } from '../../misc/constants';
import * as i0 from "@angular/core";
//TODO: optimize window events, do not register for all time
/**
 * Component used to display relation node output
 */
export class RelationNodeOutputSAComponent extends RelationNodeEndpointBase {
    constructor() {
        super(...arguments);
        //######################### protected properties #########################
        /**
         * Relations
         */
        this.ɵrelations = [];
    }
    //######################### public properties - implementation of RelationsOutput #########################
    /**
     * @inheritdoc
     */
    get relations() {
        return this.ɵrelations;
    }
    //######################### public methods - implementation of OnDestroy #########################
    /**
     * Called when component is destroyed
     */
    ngOnDestroy() {
        if (this.ɵrelations) {
            const relations = [...this.ɵrelations];
            for (const relation of relations) {
                relation.destroy();
            }
        }
    }
    //######################### public methods - implementation of RelationsOutput #########################
    /**
     * @inheritdoc
     */
    startRelation() {
        const relation = this.canvas.createRelation();
        relation.start = this.getCoordinates();
        relation.output = this;
        relation.destroying.subscribe(() => {
            const index = this.ɵrelations.indexOf(relation);
            if (index >= 0) {
                this.ɵrelations.splice(index, 1);
            }
        });
        this.ɵrelations.push(relation);
        return relation;
    }
    //######################### public methods - overrides #########################
    /**
     * Updates node output relations
     */
    updateRelation() {
        if (!this.ɵrelations) {
            return;
        }
        for (const relation of this.ɵrelations) {
            relation.start = this.getCoordinates();
            relation.invalidateVisuals();
        }
    }
    //######################### protected methods - host listeners #########################
    /**
     * Mouse down event, creates new relation
     * @param event - Mouse event that occured
     */
    _onMouseDown(event) {
        event.stopImmediatePropagation();
        event.preventDefault();
        this.isDragging = true;
        this.lastMouseDownPosition =
            {
                x: event.clientX,
                y: event.clientY
            };
        this.relation = this.startRelation();
    }
    /**
     * Mouse enter event, marks input as active
     * @param event - Mouse event that occured
     */
    _onMouseEnter(_) {
        var _a;
        (_a = this.ɵrelations) === null || _a === void 0 ? void 0 : _a.forEach(relation => relation === null || relation === void 0 ? void 0 : relation.highlight());
    }
    /**
     * Mouse leave event, clears marked active input
     * @param event - Mouse event that occured
     */
    _onMouseLeave(_) {
        var _a;
        (_a = this.ɵrelations) === null || _a === void 0 ? void 0 : _a.forEach(relation => relation === null || relation === void 0 ? void 0 : relation.cancelHighlight());
    }
    /**
     * Mouse move event - whole window
     * @param event - Mouse event that occured
     */
    _onMouseMove(event) {
        if (this.isDragging) {
            event.stopImmediatePropagation();
            event.preventDefault();
            if (this.relation) {
                this.relation.end =
                    {
                        x: this.getCoordinates().x + (event.clientX - this.lastMouseDownPosition.x) * 1 / this.zoomLevel,
                        y: this.getCoordinates().y + (event.clientY - this.lastMouseDownPosition.y) * 1 / this.zoomLevel
                    };
                this.relation.invalidateVisuals();
            }
        }
    }
    /**
     * Mouse up event - whole window
     * @param event - Mouse event that occured
     */
    _onMouseUp(event) {
        var _a;
        if (this.isDragging) {
            this.isDragging = false;
            event.stopImmediatePropagation();
            event.preventDefault();
            (_a = this.relation) === null || _a === void 0 ? void 0 : _a.invalidateVisuals(INVALIDATE_DROP);
        }
    }
}
RelationNodeOutputSAComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: RelationNodeOutputSAComponent, deps: null, target: i0.ɵɵFactoryTarget.Component });
RelationNodeOutputSAComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.0.4", type: RelationNodeOutputSAComponent, isStandalone: true, selector: "relation-node-output", host: { listeners: { "mousedown": "_onMouseDown($event)", "mouseenter": "_onMouseEnter($event)", "mouseleave": "_onMouseLeave($event)", "window:mousemove": "_onMouseMove($event)", "window:mouseup": "_onMouseUp($event)" } }, usesInheritance: true, ngImport: i0, template: '', isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: RelationNodeOutputSAComponent, decorators: [{
            type: Component,
            args: [{
                    selector: 'relation-node-output',
                    template: '',
                    standalone: true,
                    changeDetection: ChangeDetectionStrategy.OnPush
                }]
        }], propDecorators: { _onMouseDown: [{
                type: HostListener,
                args: ['mousedown', ['$event']]
            }], _onMouseEnter: [{
                type: HostListener,
                args: ['mouseenter', ['$event']]
            }], _onMouseLeave: [{
                type: HostListener,
                args: ['mouseleave', ['$event']]
            }], _onMouseMove: [{
                type: HostListener,
                args: ['window:mousemove', ['$event']]
            }], _onMouseUp: [{
                type: HostListener,
                args: ['window:mouseup', ['$event']]
            }] } });
//# sourceMappingURL=relationsNodeOutput.component.js.map