import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2019-2026 GoodData Corporation
import { useEffect } from "react";
import cx from "classnames";
import { useResizeHandlers } from "../../../dragAndDrop/LayoutResizeContext.js";
import { WidthResizer } from "../Resize/WidthResizer.js";
import { applySizeLimitation } from "./sizeLimiting.js";
export function WidthResizerDragPreview({ item, differenceFromInitialOffset, initialOffset, scrollCorrection, getDragLayerPosition, }) {
    const { setWidthState } = useResizeHandlers();
    const { gridColumnHeightInPx, rowIndex } = item;
    const sizeAndCoords = getSizeAndXCoords(item, initialOffset.x, differenceFromInitialOffset.x, scrollCorrection.x);
    const style = getWidthResizerStyle({
        gridColumnHeightInPx,
        initialOffset,
        limitedX: sizeAndCoords.limitedX,
        dragLayerOffset: getDragLayerPosition(),
        scrollCorrection,
    });
    useEffect(() => {
        setWidthState({
            initialIndex: sizeAndCoords.initialIndex,
            currentIndex: sizeAndCoords.currentIndex,
            limitReached: sizeAndCoords.limitReached,
        });
    }, [
        sizeAndCoords.initialIndex,
        sizeAndCoords.currentIndex,
        sizeAndCoords.isLimited,
        sizeAndCoords.limitReached,
        setWidthState,
    ]);
    return (_jsx("div", { className: cx("s-resizer-drag-preview", "resizer-drag-preview", "gd-grid-layout-resizer-drag-preview", "gd-grid-layout-width-resizer-drag-preview", {
            "gd-first-container-row-widget": rowIndex === 0,
        }), style: style, children: _jsx(WidthResizer, { status: "active" }) }));
}
export function getSizeAndXCoords(item, initialSourceClientOffsetX, differenceFromInitialOffsetX, scrollCorrectionX) {
    const { minLimit, maxLimit, currentWidth, initialLayoutDimensions, gridColumnWidth } = item;
    const deltaSize = getDiffInGridColumns(differenceFromInitialOffsetX - scrollCorrectionX, gridColumnWidth);
    const sizeLimitation = applySizeLimitation(minLimit, maxLimit, currentWidth, deltaSize);
    const deltaSizeLimited = sizeLimitation.limitedSize - currentWidth;
    const deltaXLimited = deltaSizeLimited * gridColumnWidth;
    const deltaXUnlimited = deltaSize * gridColumnWidth;
    const initialIndex = Math.round((initialSourceClientOffsetX - initialLayoutDimensions.left) / gridColumnWidth);
    const currentIndex = initialIndex + deltaSizeLimited;
    return {
        ...sizeLimitation,
        limitedX: initialSourceClientOffsetX + deltaXLimited,
        unlimitedX: initialSourceClientOffsetX + deltaXUnlimited,
        initialIndex,
        currentIndex,
    };
}
export function getDiffInGridColumns(pxDiffX, gridColumnWidth) {
    return Math.round(pxDiffX / gridColumnWidth);
}
function getWidthResizerStyle({ initialOffset, limitedX, gridColumnHeightInPx, dragLayerOffset, scrollCorrection, }) {
    return {
        position: "absolute",
        top: `${initialOffset.y - dragLayerOffset.y + scrollCorrection.y}px`,
        left: `${limitedX - dragLayerOffset.x + scrollCorrection.x}px`,
        height: `${gridColumnHeightInPx}px`,
    };
}
