import { Component, ChangeDetectionStrategy, ContentChild, TemplateRef, ViewContainerRef } from '@angular/core';
import { DndService } from '@ng-dnd/core';
import { Subscription } from 'rxjs';
import { DndCorePreviewTemplateDirective } from '../../directives';
import * as i0 from "@angular/core";
import * as i1 from "@ng-dnd/core";
/**
 * Component used for displaying drag preview
 */
export class DndCorePreviewComponent {
    //######################### constructor #########################
    constructor(viewContainer, dnd) {
        this.viewContainer = viewContainer;
        this.dnd = dnd;
        /**
         * Subscriptions created during initialization
         */
        this.initSubscriptions = new Subscription();
    }
    //######################### public methods - implementation of OnInit #########################
    /**
     * Initialize component
     */
    ngOnInit() {
        this.initSubscriptions.add(this.dnd
            .dragLayer(this.initSubscriptions)
            .listen(monitor => ({
            dragging: monitor.isDragging(),
            item: monitor.getItem(),
            coordinates: monitor.getClientOffset(),
            type: monitor.getItemType(),
        }))
            .subscribe(itm => {
            var _a, _b, _c;
            if (!itm.dragging) {
                this.removeDragPreview();
                return;
            }
            if (!itm.item) {
                return;
            }
            (_a = this.dragPreviewElement) !== null && _a !== void 0 ? _a : (this.dragPreviewElement = this.renderDragPreview(itm.item));
            if (this.dragPreviewElement) {
                this.dragPreviewElement.style.position = 'fixed';
                this.dragPreviewElement.style.top = `${(_b = itm.coordinates) === null || _b === void 0 ? void 0 : _b.y}px`;
                this.dragPreviewElement.style.left = `${(_c = itm.coordinates) === null || _c === void 0 ? void 0 : _c.x}px`;
                this.dragPreviewElement.style.zIndex = '21312';
            }
        }));
    }
    //######################### public methods - implementation of OnDestroy #########################
    /**
     * Called when component is destroyed
     */
    ngOnDestroy() {
        this.initSubscriptions.unsubscribe();
        this.removeDragPreview();
    }
    //######################### protected methods #########################
    /**
     * Renders drag preview and returns html element
     */
    renderDragPreview(dragData) {
        var _a;
        if (!this.dndCorePreviewTemplate) {
            return null;
        }
        this.dragPreviewView = this.viewContainer.createEmbeddedView(this.dndCorePreviewTemplate, { $implicit: dragData });
        this.dragPreviewElement = this.dragPreviewView.rootNodes[0];
        this.dragPreviewView.detectChanges();
        return (_a = this.dragPreviewElement) !== null && _a !== void 0 ? _a : null;
    }
    /**
     * Removes rendered drag preview
     */
    removeDragPreview() {
        var _a, _b;
        (_a = this.dragPreviewElement) === null || _a === void 0 ? void 0 : _a.remove();
        (_b = this.dragPreviewView) === null || _b === void 0 ? void 0 : _b.destroy();
        this.dragPreviewElement = null;
        this.dragPreviewView = null;
    }
}
DndCorePreviewComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: DndCorePreviewComponent, deps: [{ token: i0.ViewContainerRef }, { token: i1.DndService }], target: i0.ɵɵFactoryTarget.Component });
DndCorePreviewComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.0.4", type: DndCorePreviewComponent, selector: "dnd-core-drag-preview", queries: [{ propertyName: "dndCorePreviewTemplate", first: true, predicate: DndCorePreviewTemplateDirective, descendants: true, read: (TemplateRef), static: true }], ngImport: i0, template: '', isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: DndCorePreviewComponent, decorators: [{
            type: Component,
            args: [{
                    selector: 'dnd-core-drag-preview',
                    template: '',
                    changeDetection: ChangeDetectionStrategy.OnPush
                }]
        }], ctorParameters: () => [{ type: i0.ViewContainerRef }, { type: i1.DndService }], propDecorators: { dndCorePreviewTemplate: [{
                type: ContentChild,
                args: [DndCorePreviewTemplateDirective, { static: true, read: (TemplateRef) }]
            }] } });
//# sourceMappingURL=dndCorePreview.component.js.map