import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2021-2026 GoodData Corporation
import { useEffect, useState } from "react";
import { fluidLayoutDescriptor } from "@gooddata/sdk-ui-ext";
import { useResizeHandlers } from "../../../dragAndDrop/LayoutResizeContext.js";
import { HeightResizer } from "../Resize/HeightResizer.js";
import { getLimitedSize } from "./sizeLimiting.js";
export function HeightResizerDragPreview({ item, initialOffset, differenceFromInitialOffset, scrollCorrection, getDragLayerPosition, }) {
    const { toggleHeightLimitReached } = useResizeHandlers();
    const [hasReachedLimit, setReachedLimit] = useState("none");
    useEffect(() => {
        toggleHeightLimitReached(hasReachedLimit);
    }, [hasReachedLimit, toggleHeightLimitReached]);
    const dragLayerOffset = getDragLayerPosition();
    const currentUnlimitedHeightGR = getNewHeightGR(item.widgetHeights, differenceFromInitialOffset.y, scrollCorrection.y);
    useEffect(() => {
        const hasNowReachedLimit = hasHeightReachedLimit(currentUnlimitedHeightGR, item.minLimit, item.maxLimit);
        if (hasNowReachedLimit !== hasReachedLimit) {
            setReachedLimit(hasNowReachedLimit);
        }
    }, [currentUnlimitedHeightGR, item.minLimit, item.maxLimit, hasReachedLimit]);
    const top = getLimitedYCoord(item, initialOffset.y, differenceFromInitialOffset.y, scrollCorrection.y);
    const style = {
        top: `${top - dragLayerOffset.y + scrollCorrection.y}px`,
        left: `${initialOffset.x - dragLayerOffset.x + scrollCorrection.x}px`,
        width: `${item.initialLayoutDimensions.width}px`,
    };
    return (_jsx("div", { className: "height-resizer-drag-preview s-height-resizer-drag-preview gd-grid-layout-resizer-drag-preview", style: style, children: _jsx(HeightResizer, { status: "active" }) }));
}
function getPrimaryHeightGR(heightsGR) {
    heightsGR = heightsGR || [10];
    return Math.max(...heightsGR);
}
function hasHeightReachedLimit(heightGR, min, max) {
    if (heightGR < min)
        return "min";
    if (heightGR > max)
        return "max";
    return "none";
}
function getNewHeightGR(widgetHeights, offsetYPX, scrollingCorrectionY) {
    const primaryHeightGR = getPrimaryHeightGR(widgetHeights);
    const totalDelta = offsetYPX - scrollingCorrectionY;
    const deltaHeightGR = fluidLayoutDescriptor.toGridHeight(totalDelta);
    return primaryHeightGR + deltaHeightGR;
}
export function getLimitedYCoord(item, initialSourceClientOffsetY, differenceFromInitialOffsetY, scrollingCorrectionY) {
    const { minLimit, maxLimit } = item;
    const deltaSize = fluidLayoutDescriptor.toGridHeight(differenceFromInitialOffsetY - scrollingCorrectionY);
    const curPrimaryHeightGR = getPrimaryHeightGR(item.widgetHeights);
    const newSizeLimited = getLimitedSize(minLimit, maxLimit, curPrimaryHeightGR, deltaSize);
    const deltaY = fluidLayoutDescriptor.toHeightInPx(newSizeLimited - curPrimaryHeightGR);
    return initialSourceClientOffsetY + deltaY;
}
