export default class SheetTabDragManager {
    constructor(sheet) {
        this.dragState = {
            isDragging: !1,
            draggedSheetKey: null,
            draggedElement: null,
            startX: 0,
            startY: 0,
            threshold: 5,
            preview: null,
            dropZones: []
        }, this.insertIndicator = null, this.sheet = sheet, this.boundMouseMove = e => this.handleGlobalMouseMove(e), 
        this.boundMouseUp = e => this.handleGlobalMouseUp(e);
    }
    handleTabMouseDown(e, sheetKey) {
        this.insertIndicator || (this.insertIndicator = this.sheet.getRootElement().querySelector(".vtable-sheet-insert-indicator")), 
        0 === e.button && (e.preventDefault(), this.dragState.startX = e.clientX, this.dragState.startY = e.clientY, 
        this.dragState.draggedSheetKey = sheetKey, this.dragState.draggedElement = e.target, 
        document.addEventListener("mousemove", this.boundMouseMove), document.addEventListener("mouseup", this.boundMouseUp));
    }
    handleGlobalMouseMove(e) {
        if (!this.dragState.draggedSheetKey || !this.dragState.draggedElement) return;
        const deltaX = Math.abs(e.clientX - this.dragState.startX), deltaY = Math.abs(e.clientY - this.dragState.startY);
        !this.dragState.isDragging && (deltaX > this.dragState.threshold || deltaY > this.dragState.threshold) && this.startDragging(), 
        this.dragState.isDragging && this.updateDragging(e);
    }
    handleGlobalMouseUp(e) {
        this.dragState.isDragging && this.endDragging(e), this.cleanupDragState(), document.removeEventListener("mousemove", (e => this.handleGlobalMouseMove(e))), 
        document.removeEventListener("mouseup", (e => this.handleGlobalMouseUp(e)));
    }
    startDragging() {
        this.dragState.draggedElement && this.dragState.draggedSheetKey && (this.dragState.isDragging = !0, 
        this.dragState.draggedElement.classList.add("dragging"), this.createDragPreview(), 
        this.calculateDropZones(), this.insertIndicator && (this.insertIndicator.style.display = "block"));
    }
    updateDragging(e) {
        if (!this.dragState.isDragging) return;
        this.updateDragPreview(e);
        const dropZone = this.getDropZoneAt(e.clientX, e.clientY);
        dropZone ? this.showInsertIndicator(dropZone) : this.hideInsertIndicator();
    }
    endDragging(e) {
        if (!this.dragState.isDragging || !this.dragState.draggedSheetKey) return;
        const dropZone = this.getDropZoneAt(e.clientX, e.clientY);
        dropZone && dropZone.targetKey !== this.dragState.draggedSheetKey && this.performTabReorder(this.dragState.draggedSheetKey, dropZone.targetKey, dropZone.position), 
        this.hideInsertIndicator(), this.dragState.draggedElement && this.dragState.draggedElement.classList.remove("dragging");
    }
    createDragPreview() {
        this.dragState.draggedElement && (this.dragState.preview = document.createElement("div"), 
        this.dragState.preview.className = "vtable-sheet-drag-preview", this.dragState.preview.innerHTML = `\n      <span class="drag-preview-text">${this.dragState.draggedElement.textContent}</span>\n    `, 
        document.body.appendChild(this.dragState.preview), this.dragState.draggedElement.style.cursor = "grabbing");
    }
    updateDragPreview(e) {
        this.dragState.preview && (this.dragState.preview.style.left = e.clientX - this.dragState.preview.offsetWidth / 2 + "px", 
        this.dragState.preview.style.top = e.clientY - this.dragState.preview.offsetHeight / 2 + "px");
    }
    cleanupDragPreview() {
        this.dragState.preview && (this.dragState.preview.remove(), this.dragState.preview = null), 
        this.dragState.draggedElement && (this.dragState.draggedElement.style.cursor = "");
    }
    calculateDropZones() {
        var _a;
        this.dragState.dropZones = [];
        const tabsContainer = null === (_a = this.sheet.getSheetTabElement()) || void 0 === _a ? void 0 : _a.querySelector(".vtable-sheet-tabs-container");
        if (!tabsContainer) return;
        tabsContainer.querySelectorAll(".vtable-sheet-tab").forEach((tab => {
            const sheetKey = tab.dataset.key;
            sheetKey && sheetKey !== this.dragState.draggedSheetKey && (this.dragState.dropZones.push({
                element: tab,
                position: "left",
                targetKey: sheetKey
            }), this.dragState.dropZones.push({
                element: tab,
                position: "right",
                targetKey: sheetKey
            }));
        }));
    }
    getDropZoneAt(x, y) {
        for (const zone of this.dragState.dropZones) {
            const rect = zone.element.getBoundingClientRect();
            if (y < rect.top || y > rect.bottom) continue;
            const midX = rect.left + rect.width / 2;
            if ("left" === zone.position && x >= rect.left && x <= midX) return zone;
            if ("right" === zone.position && x >= midX && x <= rect.right) return zone;
        }
        return null;
    }
    showInsertIndicator(dropZone) {
        var _a;
        if (!this.insertIndicator) return;
        const rect = dropZone.element.getBoundingClientRect(), containerRect = null === (_a = this.sheet.getRootElement().querySelector(".vtable-sheet-tabs-container")) || void 0 === _a ? void 0 : _a.getBoundingClientRect();
        if (!containerRect) return;
        let indicatorX;
        indicatorX = "left" === dropZone.position ? rect.left - containerRect.left - 4 : rect.right - containerRect.left - 4, 
        this.insertIndicator.style.left = `${indicatorX}px`, this.insertIndicator.style.display = "block";
    }
    hideInsertIndicator() {
        this.insertIndicator && (this.insertIndicator.style.display = "none");
    }
    performTabReorder(sourceKey, targetKey, position) {
        try {
            this.sheet.getSheetManager().reorderSheet(sourceKey, targetKey, position), this.sheet.updateSheetTabs(), 
            this.sheet.updateSheetMenu();
        } catch (error) {}
    }
    cleanupDragState() {
        this.cleanupDragPreview(), this.dragState.isDragging = !1, this.dragState.draggedSheetKey = null, 
        this.dragState.draggedElement = null, this.dragState.dropZones = [], this.dragState.startX = 0, 
        this.dragState.startY = 0;
    }
}
//# sourceMappingURL=tab-drag-manager.js.map