export const BRIDGE_INLINE_FORMAT_TOOLBAR_SELECTOR = '[data-bridge-inline-format-toolbar="true"]';
export const BRIDGE_INLINE_FORMAT_AI_BUTTON_SELECTOR = '[data-bridge-inline-format-ai-button="true"]';
const DEFAULT_TOOLBAR_WIDTH = 300;
// Matches the rendered toolbar box: h-8 buttons + p-2 chrome + 1px border.
const DEFAULT_TOOLBAR_HEIGHT = 50;
const VIEWPORT_PADDING = 8;
const SELECTION_GAP = 8;
export function calculateBridgeInlineFormatToolbarLayout(clientRect, options) {
    const toolbarWidth = options?.toolbarWidth ?? DEFAULT_TOOLBAR_WIDTH;
    const toolbarHeight = options?.toolbarHeight ?? DEFAULT_TOOLBAR_HEIGHT;
    const anchorCenter = clientRect.left + Math.min(clientRect.width, toolbarWidth) / 2;
    let top = clientRect.top - toolbarHeight - SELECTION_GAP;
    if (top < VIEWPORT_PADDING) {
        top = clientRect.bottom + SELECTION_GAP + 36;
    }
    top = Math.max(VIEWPORT_PADDING, Math.min(top, window.innerHeight - toolbarHeight - VIEWPORT_PADDING));
    const left = Math.max(VIEWPORT_PADDING, Math.min(anchorCenter - toolbarWidth / 2, window.innerWidth - toolbarWidth - VIEWPORT_PADDING));
    return {
        left,
        top,
        width: toolbarWidth,
        height: toolbarHeight,
    };
}
export function getInlineAiAnchorBelowToolbar(toolbarRect, aiButtonRect) {
    return {
        x: aiButtonRect?.left ?? toolbarRect.left,
        y: toolbarRect.bottom,
        align: "start",
        origin: "toolbar",
    };
}
export function getInlineAiAnchorFromBridgeToolbar(selectedRange, measuredToolbarRect) {
    if (!selectedRange?.elementKey ||
        !selectedRange.text?.trim() ||
        !selectedRange.clientRect) {
        return undefined;
    }
    if (measuredToolbarRect) {
        return getInlineAiAnchorBelowToolbar(measuredToolbarRect, getVisibleBridgeToolbarAiButtonRect());
    }
    const layout = calculateBridgeInlineFormatToolbarLayout(selectedRange.clientRect);
    return getInlineAiAnchorBelowToolbar({
        left: layout.left,
        bottom: layout.top + layout.height,
    });
}
export function getVisibleBridgeToolbarRect() {
    if (typeof document === "undefined") {
        return undefined;
    }
    const toolbar = document.querySelector(BRIDGE_INLINE_FORMAT_TOOLBAR_SELECTOR);
    if (!toolbar) {
        return undefined;
    }
    return toolbar.getBoundingClientRect();
}
export function getVisibleBridgeToolbarAiButtonRect() {
    if (typeof document === "undefined") {
        return undefined;
    }
    const button = document.querySelector(BRIDGE_INLINE_FORMAT_AI_BUTTON_SELECTOR);
    if (!button) {
        return undefined;
    }
    return button.getBoundingClientRect();
}
export function waitForVisibleBridgeToolbarRect(maxFrames = 3) {
    if (typeof window === "undefined") {
        return Promise.resolve(undefined);
    }
    const current = getVisibleBridgeToolbarRect();
    if (current) {
        return Promise.resolve(current);
    }
    return new Promise((resolve) => {
        let frame = 0;
        const check = () => {
            const rect = getVisibleBridgeToolbarRect();
            if (rect || frame >= maxFrames) {
                resolve(rect);
                return;
            }
            frame += 1;
            window.requestAnimationFrame(check);
        };
        window.requestAnimationFrame(check);
    });
}
//# sourceMappingURL=bridgeInlineFormatToolbarLayout.js.map