import { Component, ChangeDetectionStrategy, HostListener } from '@angular/core';
import { RelationNodeEndpointBase } from '../relationsNodeEndpointBase';
import { MouseButton } from '../../misc/enums';
import { INVALIDATE_DROP } from '../../misc/constants';
import * as i0 from "@angular/core";
/**
 * Component used to display relation node input
 */
export class RelationNodeInputSAComponent extends RelationNodeEndpointBase {
    //######################### public methods - implementation of OnDestroy #########################
    /**
     * Called when component is destroyed
     */
    ngOnDestroy() {
        if (this.relation) {
            this.relation.destroy();
        }
    }
    //######################### public methods - implementation of RelationsInput #########################
    /**
     * @inheritdoc
     */
    addRelation(relation) {
        var _a, _b, _c, _d;
        if (this.relation) {
            //Same relation
            if (((_a = this.relation.start) === null || _a === void 0 ? void 0 : _a.x) === ((_b = relation.start) === null || _b === void 0 ? void 0 : _b.x) &&
                ((_c = this.relation.start) === null || _c === void 0 ? void 0 : _c.y) === ((_d = relation.start) === null || _d === void 0 ? void 0 : _d.y)) {
                return false;
            }
            this.relation.destroy();
        }
        this.relation = relation;
        return true;
    }
    /**
     * @inheritdoc
     */
    endRelation(relation) {
        if (this.addRelation(relation)) {
            this.updateRelation();
        }
    }
    //######################### public methods - overrides #########################
    /**
     * Updates node output relation
     */
    updateRelation() {
        if (!this.relation) {
            return;
        }
        this.relation.end = this.getCoordinates();
        this.relation.input = this;
        this.relation.invalidateVisuals();
    }
    //######################### protected methods - host listeners #########################
    /**
     * Mouse enter event, marks input as active
     * @param event - Mouse event that occured
     */
    _onMouseEnter(event) {
        var _a;
        if (event.buttons === MouseButton.LEFT) {
            this.relationManager.setActiveInput(this);
        }
        (_a = this.relation) === null || _a === void 0 ? void 0 : _a.highlight();
    }
    /**
     * Mouse leave event, clears marked active input
     * @param event - Mouse event that occured
     */
    _onMouseLeave(event) {
        var _a;
        if (event.buttons === MouseButton.LEFT) {
            this.relationManager.setActiveInput(null);
        }
        (_a = this.relation) === null || _a === void 0 ? void 0 : _a.cancelHighlight();
    }
    /**
     * Mouse down event, starts dragging
     * @param event - Mouse event that occured
     */
    _onMouseDown(event) {
        event.stopImmediatePropagation();
        event.preventDefault();
        this.lastMouseDownPosition =
            {
                x: event.clientX,
                y: event.clientY
            };
        this._tempRelation = this.relation;
        this.relation = null;
        this.isDragging = true;
    }
    /**
     * Mouse move event on whole window
     * @param event - Mouse event that occured
     */
    _onMouseMove(event) {
        var _a, _b;
        if (this.isDragging) {
            event.stopImmediatePropagation();
            event.preventDefault();
            if (this._tempRelation) {
                this._tempRelation.end =
                    {
                        x: this.getCoordinates().x + (event.clientX - ((_a = this.lastMouseDownPosition) === null || _a === void 0 ? void 0 : _a.x)) * 1 / this.zoomLevel,
                        y: this.getCoordinates().y + (event.clientY - ((_b = this.lastMouseDownPosition) === null || _b === void 0 ? void 0 : _b.y)) * 1 / this.zoomLevel
                    };
                this._tempRelation.invalidateVisuals();
            }
        }
    }
    /**
     * Mouse up event on whole window
     * @param event - Mouse event that occured
     */
    _onMouseUp(event) {
        var _a;
        if (this.isDragging) {
            this.isDragging = false;
            event.stopImmediatePropagation();
            event.preventDefault();
            (_a = this._tempRelation) === null || _a === void 0 ? void 0 : _a.invalidateVisuals(INVALIDATE_DROP);
        }
    }
}
RelationNodeInputSAComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: RelationNodeInputSAComponent, deps: null, target: i0.ɵɵFactoryTarget.Component });
RelationNodeInputSAComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.0.4", type: RelationNodeInputSAComponent, isStandalone: true, selector: "relation-node-input", host: { listeners: { "mouseenter": "_onMouseEnter($event)", "mouseleave": "_onMouseLeave($event)", "mousedown": "_onMouseDown($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: RelationNodeInputSAComponent, decorators: [{
            type: Component,
            args: [{
                    selector: 'relation-node-input',
                    template: '',
                    standalone: true,
                    changeDetection: ChangeDetectionStrategy.OnPush
                }]
        }], propDecorators: { _onMouseEnter: [{
                type: HostListener,
                args: ['mouseenter', ['$event']]
            }], _onMouseLeave: [{
                type: HostListener,
                args: ['mouseleave', ['$event']]
            }], _onMouseDown: [{
                type: HostListener,
                args: ['mousedown', ['$event']]
            }], _onMouseMove: [{
                type: HostListener,
                args: ['window:mousemove', ['$event']]
            }], _onMouseUp: [{
                type: HostListener,
                args: ['window:mouseup', ['$event']]
            }] } });
//# sourceMappingURL=relationsNodeInput.component.js.map