var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _DragAndDropController_instances, _DragAndDropController_component, _DragAndDropController_handleDrop, _DragAndDropController_host, _DragAndDropController_disposeEventListeners, _DragAndDropController_onComponentDragEnter, _DragAndDropController_onComponentDrop, _DragAndDropController_onWindowDragEnter, _DragAndDropController_onWindowDragLeave, _DragAndDropController_onWindowDragOver;
/**
 * This entire file was copied from shreddit
 * I removed some parts of it that were too "shreddit post creation" focused, but kept the original code style
 * Eventually we will remove it in favour of faceplate-ui image upload component, but for now we have this
 */
export var DragState;
(function (DragState) {
    DragState["IDLE"] = "IDLE";
    DragState["IN_WINDOW"] = "IN_WINDOW";
    DragState["IN_FRAME"] = "IN_FRAME";
})(DragState || (DragState = {}));
export class DragAndDropController {
    constructor(host, handleDrop) {
        _DragAndDropController_instances.add(this);
        _DragAndDropController_component.set(this, null);
        _DragAndDropController_handleDrop.set(this, void 0);
        _DragAndDropController_host.set(this, void 0);
        this.dragState = DragState.IDLE;
        _DragAndDropController_onComponentDragEnter.set(this, (e) => {
            e.preventDefault();
            e.stopPropagation();
            this.dragState = DragState.IN_FRAME;
            __classPrivateFieldGet(this, _DragAndDropController_host, "f").requestUpdate();
        });
        _DragAndDropController_onComponentDrop.set(this, (e) => {
            e.preventDefault();
            if (e.dataTransfer) {
                const files = getFilesFromDataTransfer(e.dataTransfer);
                __classPrivateFieldGet(this, _DragAndDropController_handleDrop, "f").call(this, files);
            }
            this.dragState = DragState.IDLE;
            __classPrivateFieldGet(this, _DragAndDropController_host, "f").requestUpdate();
        });
        _DragAndDropController_onWindowDragEnter.set(this, (e) => {
            e.preventDefault();
            this.dragState = DragState.IN_WINDOW;
            __classPrivateFieldGet(this, _DragAndDropController_host, "f").requestUpdate();
        });
        _DragAndDropController_onWindowDragLeave.set(this, (e) => {
            e.preventDefault();
            if (!e.relatedTarget || e.relatedTarget === document.body) {
                this.dragState = DragState.IDLE;
                __classPrivateFieldGet(this, _DragAndDropController_host, "f").requestUpdate();
            }
        });
        _DragAndDropController_onWindowDragOver.set(this, (e) => {
            e.preventDefault();
        });
        __classPrivateFieldSet(this, _DragAndDropController_handleDrop, handleDrop, "f");
        __classPrivateFieldSet(this, _DragAndDropController_host, host, "f");
        host.addController(this);
        // Bind event handlers to ensure the correct 'this' context
        __classPrivateFieldSet(this, _DragAndDropController_onComponentDragEnter, __classPrivateFieldGet(this, _DragAndDropController_onComponentDragEnter, "f").bind(this), "f");
        __classPrivateFieldSet(this, _DragAndDropController_onComponentDrop, __classPrivateFieldGet(this, _DragAndDropController_onComponentDrop, "f").bind(this), "f");
        __classPrivateFieldSet(this, _DragAndDropController_onWindowDragEnter, __classPrivateFieldGet(this, _DragAndDropController_onWindowDragEnter, "f").bind(this), "f");
        __classPrivateFieldSet(this, _DragAndDropController_onWindowDragLeave, __classPrivateFieldGet(this, _DragAndDropController_onWindowDragLeave, "f").bind(this), "f");
        __classPrivateFieldSet(this, _DragAndDropController_onWindowDragOver, __classPrivateFieldGet(this, _DragAndDropController_onWindowDragOver, "f").bind(this), "f");
    }
    hostDisconnected() {
        __classPrivateFieldGet(this, _DragAndDropController_instances, "m", _DragAndDropController_disposeEventListeners).call(this);
    }
    initEventListeners(component) {
        __classPrivateFieldSet(this, _DragAndDropController_component, component, "f");
        this.dragState = DragState.IDLE;
        __classPrivateFieldGet(this, _DragAndDropController_host, "f").requestUpdate();
        // Component-specific events
        __classPrivateFieldGet(this, _DragAndDropController_component, "f").addEventListener('dragover', __classPrivateFieldGet(this, _DragAndDropController_onComponentDragEnter, "f"));
        __classPrivateFieldGet(this, _DragAndDropController_component, "f").addEventListener('drop', __classPrivateFieldGet(this, _DragAndDropController_onComponentDrop, "f"));
        // Window-specific events
        window.addEventListener('dragover', __classPrivateFieldGet(this, _DragAndDropController_onWindowDragEnter, "f"));
        window.addEventListener('dragleave', __classPrivateFieldGet(this, _DragAndDropController_onWindowDragLeave, "f"));
        window.addEventListener('drop', __classPrivateFieldGet(this, _DragAndDropController_onWindowDragLeave, "f"));
        window.addEventListener('dragover', __classPrivateFieldGet(this, _DragAndDropController_onWindowDragOver, "f"));
    }
}
_DragAndDropController_component = new WeakMap(), _DragAndDropController_handleDrop = new WeakMap(), _DragAndDropController_host = new WeakMap(), _DragAndDropController_onComponentDragEnter = new WeakMap(), _DragAndDropController_onComponentDrop = new WeakMap(), _DragAndDropController_onWindowDragEnter = new WeakMap(), _DragAndDropController_onWindowDragLeave = new WeakMap(), _DragAndDropController_onWindowDragOver = new WeakMap(), _DragAndDropController_instances = new WeakSet(), _DragAndDropController_disposeEventListeners = function _DragAndDropController_disposeEventListeners() {
    if (__classPrivateFieldGet(this, _DragAndDropController_component, "f")) {
        __classPrivateFieldGet(this, _DragAndDropController_component, "f").removeEventListener('dragover', __classPrivateFieldGet(this, _DragAndDropController_onComponentDragEnter, "f"));
        __classPrivateFieldGet(this, _DragAndDropController_component, "f").removeEventListener('drop', __classPrivateFieldGet(this, _DragAndDropController_onComponentDrop, "f"));
    }
    // Remove window-specific event listeners
    window.removeEventListener('dragover', __classPrivateFieldGet(this, _DragAndDropController_onWindowDragEnter, "f"));
    window.removeEventListener('dragleave', __classPrivateFieldGet(this, _DragAndDropController_onWindowDragLeave, "f"));
    window.removeEventListener('drop', __classPrivateFieldGet(this, _DragAndDropController_onWindowDragLeave, "f")); // Match with the correct event
    window.removeEventListener('dragover', __classPrivateFieldGet(this, _DragAndDropController_onWindowDragOver, "f"));
    this.dragState = DragState.IDLE;
    __classPrivateFieldGet(this, _DragAndDropController_host, "f").requestUpdate();
};
const getFilesFromDataTransfer = (dataTransfer) => {
    if (dataTransfer.items) {
        return Array.from(dataTransfer.items)
            .map((item) => (item.kind === 'file' ? item.getAsFile() : null))
            .filter(Boolean);
    }
    else if (dataTransfer.files) {
        return Array.from(dataTransfer.files);
    }
    else {
        return [];
    }
};
//# sourceMappingURL=drag-and-drop-controller.js.map