import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { ExternalLink } from "lucide-react";
import { useState } from "react";
import { Button } from "../../components/ui/button";
import { Tooltip, TooltipContent, TooltipTrigger, } from "../../components/ui/tooltip";
/**
 * True when the editor is running inside an iframe (e.g. embedded in Sitecore
 * XM Cloud Pages). A cross-origin parent makes `window.top` throw on access —
 * that itself means we are embedded.
 */
function detectEmbedded() {
    if (typeof window === "undefined")
        return false;
    try {
        return window.self !== window.top;
    }
    catch {
        return true;
    }
}
/**
 * Shows an "open in new tab" button when the editor is embedded in an iframe,
 * letting the user break out into a standalone top-level browser tab. Renders
 * nothing when the editor is already the top-level document.
 */
export function OpenInNewTabButton({ floating = false }) {
    // Whether we're embedded doesn't change during a session, so compute it once.
    const [isEmbedded] = useState(detectEmbedded);
    if (!isEmbedded)
        return null;
    const label = "Open in new tab";
    const openInNewTab = () => {
        window.open(window.location.href, "_blank", "noopener,noreferrer");
    };
    const button = (_jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(Button, { size: "iconSm", variant: "ghost", onClick: openInNewTab, "aria-label": label, "data-testid": "open-in-new-tab", children: _jsx(ExternalLink, { className: "h-4 w-4" }) }) }), _jsx(TooltipContent, { children: label })] }));
    if (!floating)
        return button;
    // Pinned to the top of the scroll container; the wrapper ignores pointer events
    // so it never blocks the content beneath it, while the button itself stays clickable.
    return (_jsx("div", { className: "pointer-events-none sticky top-0 z-20 flex justify-end px-4 pt-3", children: _jsx("div", { className: "pointer-events-auto", children: button }) }));
}
//# sourceMappingURL=OpenInNewTabButton.js.map