import { Component, ChangeDetectionStrategy, ChangeDetectorRef, Inject, Optional, Input, HostBinding, HostListener } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CdkDrag } from '@angular/cdk/drag-drop';
import { PackageManager } from '@anglr/dynamic';
import { FirstUppercaseLocalizeSAPipe, LOGGER } from '@anglr/common';
import { Subscription } from 'rxjs';
import { RelationsNodeManager } from '../../../services';
import * as i0 from "@angular/core";
import * as i1 from "@anglr/dynamic";
import * as i2 from "../../../services";
import * as i3 from "@angular/cdk/drag-drop";
import * as i4 from "@angular/common";
/**
 * Component displaying nodes palette item
 */
export class NodesPaletteItemSAComponent {
    //######################### constructor #########################
    constructor(_changeDetector, packageManager, _metadataManager, _cdkDrag, _logger) {
        this._changeDetector = _changeDetector;
        this.packageManager = packageManager;
        this._metadataManager = _metadataManager;
        this._cdkDrag = _cdkDrag;
        this._logger = _logger;
        //######################### protected properties #########################
        /**
         * Subscriptions created during initialization
         */
        this.initSubscriptions = new Subscription();
        /**
         * Indication whether node is already used
         */
        this.used = false;
    }
    //######################### public methods - implementation of OnInit #########################
    /**
     * Initialize component
     */
    async ngOnInit() {
        for (const nodeId in this._metadataManager.nodes) {
            if (this.data?.itemSource?.name === nodeId) {
                this._setUsed(true);
                break;
            }
        }
        //TODO initsubscription add used change
        this.initSubscriptions.add(this._metadataManager.nodeRegisterChange.subscribe(registeredNode => {
            if (registeredNode.id === this.data?.itemSource?.name) {
                this._setUsed(true);
            }
        }));
        this.initSubscriptions.add(this._metadataManager.nodeUnregisterChange.subscribe(unregisteredNode => {
            if (unregisteredNode.id === this.data?.itemSource?.name) {
                this._setUsed(false);
            }
        }));
    }
    //######################### public methods - implementation of OnDestroy #########################
    /**
     * Called when component is destroyed
     */
    ngOnDestroy() {
        this.initSubscriptions.unsubscribe();
    }
    //######################### protected methods #########################
    /**
     * Sets used indication for node
     * @param used
     */
    _setUsed(used) {
        this.used = used;
        if (this._cdkDrag) {
            this._cdkDrag.disabled = used;
        }
        this._changeDetector.markForCheck();
    }
    /**
     * Focus to relations node
     * @param item
     * @returns
     */
    focusNode() {
        if (!this.used ||
            !this.data?.metadata?.singleton) {
            return;
        }
        this._metadataManager.setActiveNode(this.data?.itemSource?.name);
    }
}
NodesPaletteItemSAComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: NodesPaletteItemSAComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i1.PackageManager }, { token: i2.RelationsNodeManager }, { token: i3.CdkDrag, optional: true }, { token: LOGGER, optional: true }], target: i0.ɵɵFactoryTarget.Component });
NodesPaletteItemSAComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.0.4", type: NodesPaletteItemSAComponent, isStandalone: true, selector: "nodes-palette-item", inputs: { data: "data" }, host: { listeners: { "dblclick": "focusNode()" }, properties: { "class.used": "this.used" } }, ngImport: i0, template: "<div class=\"editor-component-name\">{{(data.metadata.singleton ? (data.metadata.displayName || data.itemSource.name) : (data.metadata.metaInfo?.name ?? data.itemSource.name))|firstUppercaseLocalize}}</div>\n<div class=\"editor-component-description\" *ngIf=\"data.metadata.metaInfo?.description\">{{data.metadata.metaInfo?.description|firstUppercaseLocalize}}</div>", dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i4.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "pipe", type: FirstUppercaseLocalizeSAPipe, name: "firstUppercaseLocalize" }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: NodesPaletteItemSAComponent, decorators: [{
            type: Component,
            args: [{ selector: 'nodes-palette-item', standalone: true, imports: [
                        CommonModule,
                        FirstUppercaseLocalizeSAPipe,
                    ], changeDetection: ChangeDetectionStrategy.OnPush, template: "<div class=\"editor-component-name\">{{(data.metadata.singleton ? (data.metadata.displayName || data.itemSource.name) : (data.metadata.metaInfo?.name ?? data.itemSource.name))|firstUppercaseLocalize}}</div>\n<div class=\"editor-component-description\" *ngIf=\"data.metadata.metaInfo?.description\">{{data.metadata.metaInfo?.description|firstUppercaseLocalize}}</div>" }]
        }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }, { type: i1.PackageManager }, { type: i2.RelationsNodeManager }, { type: i3.CdkDrag, decorators: [{
                    type: Optional
                }] }, { type: undefined, decorators: [{
                    type: Inject,
                    args: [LOGGER]
                }, {
                    type: Optional
                }] }], propDecorators: { used: [{
                type: HostBinding,
                args: ['class.used']
            }], data: [{
                type: Input
            }], focusNode: [{
                type: HostListener,
                args: ['dblclick']
            }] } });
//# sourceMappingURL=nodesPaletteItem.component.js.map