export const ANJUNA_DRAG_CLASS = 'anj-dragging';
export class DragController {
    constructor(element, data) {
        this.element = element;
        this.data = data;
        this.dragStartListener = (e) => this.onDragStart(e);
        this.dragEndListener = (e) => this.onDragEnd(e);
    }
    connect() {
        this.element.setAttribute('draggable', 'true');
        this.element.addEventListener('dragstart', this.dragStartListener);
        this.element.addEventListener('dragend', this.dragEndListener);
        return this;
    }
    disconnect() {
        this.element.removeAttribute('draggable');
        this.element.removeEventListener('dragstart', this.dragStartListener);
        this.element.removeEventListener('dragend', this.dragEndListener);
        return undefined;
    }
    onDragStart(event) {
        this.element.classList.add(ANJUNA_DRAG_CLASS);
        if (event.dataTransfer) {
            event.dataTransfer.effectAllowed = 'move';
            if (this.data) {
                event.dataTransfer.setData('data', JSON.stringify(this.getData()));
            }
        }
    }
    onDragEnd(event) {
        event.preventDefault();
        this.element.classList.remove(ANJUNA_DRAG_CLASS);
    }
    getData() {
        return typeof this.data === 'function' ? this.data() : this.data;
    }
}
