// (C) 2025 GoodData Corporation
import { ADDITIONAL_ARROW_EDGE_OFFSET, ARROW_WIDTH, DEFAULT_BORDER_RADIUS, oppositeSides, } from "./constants.js";
const parsePlacement = (placement) => {
    const [basicPlacement, axisPlacement] = placement.split("-");
    return { basicPlacement, axisPlacement };
};
export const getOppositeBasicPlacement = (arrowPlacement, behaviour) => {
    const parsedPlacement = parsePlacement(arrowPlacement);
    const { basicPlacement, axisPlacement } = parsedPlacement;
    const opposite = oppositeSides[basicPlacement];
    if (behaviour === "popover") {
        return `${opposite}-${axisPlacement}`;
    }
    return opposite;
};
export const getFlipFallbackOrder = (arrowPlacement, behaviour) => {
    const basicPlacement = parsePlacement(arrowPlacement).basicPlacement;
    const oppositePlacement = getOppositeBasicPlacement(arrowPlacement, behaviour);
    const remaining = Object.values(oppositeSides).filter((side) => side !== basicPlacement && side !== oppositePlacement);
    return [basicPlacement, oppositePlacement, ...remaining];
};
export const getDimensionsFromRect = (rect) => {
    return { width: rect?.width ?? 0, height: rect?.height ?? 0 };
};
export const getDimensionsFromRef = (ref) => {
    const rect = ref?.current?.getBoundingClientRect?.();
    return getDimensionsFromRect(rect ?? null);
};
//get border radius from style variable, in case of scoped theme get directly from theme
const getBorderRadius = (theme) => {
    const borderRadius = theme?.modal?.borderRadius ??
        getComputedStyle(document.documentElement).getPropertyValue("--gd-modal-borderRadius").trim();
    const numericValue = parseInt(borderRadius, 10);
    return isNaN(numericValue) ? DEFAULT_BORDER_RADIUS : numericValue;
};
const getArrowEdgeOffset = (theme) => {
    const tooltipBorderRadius = getBorderRadius(theme);
    return tooltipBorderRadius + ADDITIONAL_ARROW_EDGE_OFFSET;
};
export const computeTooltipShift = (arrowPlacement, floatingDimensions, isEnabled, theme) => {
    const { basicPlacement, axisPlacement } = parsePlacement(arrowPlacement);
    const borderArrowOffset = getArrowEdgeOffset(theme) + ARROW_WIDTH / 2;
    if (isEnabled) {
        let shiftProp = "";
        let shiftValue = 0;
        if (["top", "bottom"].includes(basicPlacement)) {
            shiftProp = "x";
            shiftValue = floatingDimensions.width / 2 - borderArrowOffset;
        }
        else if (["left", "right"].includes(basicPlacement)) {
            shiftProp = "y";
            shiftValue = floatingDimensions.height / 2 - borderArrowOffset;
        }
        if (axisPlacement === "start") {
            return { [shiftProp]: shiftValue };
        }
        else if (axisPlacement === "end") {
            return { [shiftProp]: -shiftValue };
        }
    }
    return {};
};
export const computeArrowOffset = (arrowPlacement, floatingDimensions, isEnabled, theme) => {
    if (isEnabled) {
        const arrowEdgeOffset = getArrowEdgeOffset(theme);
        const { basicPlacement, axisPlacement } = parsePlacement(arrowPlacement);
        const sideLength = ["top", "bottom"].includes(basicPlacement)
            ? floatingDimensions.width
            : floatingDimensions.height;
        if (axisPlacement === "start") {
            return arrowEdgeOffset;
        }
        else if (axisPlacement === "end") {
            // Return null if dimensions not yet available to avoid negative offset
            if (sideLength <= 0) {
                return null;
            }
            return sideLength - (ARROW_WIDTH + arrowEdgeOffset);
        }
    }
    return null;
};
