import { requireOptionalNativeModule } from "expo";
import { MOBILE_THEME_IDS, type MobileThemeId } from "../../lib/mobileTheme";
const SHOWCASE_SCENES = [
"threads",
"thread",
"terminal",
"review",
"environments",
"agent-activity",
] as const;
export type ShowcaseScene = (typeof SHOWCASE_SCENES)[number];
export type ShowcaseOrientation = "portrait" | "landscape";
interface NativeShowcaseControls {
readonly getShowcasePairingUrl?: () => string | null;
readonly getShowcaseScene?: () => string | null;
readonly getShowcaseTheme?: () => string | null;
readonly getShowcaseOrientation?: () => string | null;
readonly applyShowcaseOrientation?: (orientation: ShowcaseOrientation) => Promise<void>;
readonly getInterfaceOrientation?: () => Promise<string>;
readonly prepareShowcaseCapture?: () => void;
readonly markShowcaseReady?: (scene: ShowcaseScene) => void;
}
function nativeShowcaseControls(): NativeShowcaseControls | null {
return requireOptionalNativeModule<NativeShowcaseControls>("T3NativeControls");
}
export function getNativeShowcasePairingUrls(): ReadonlyArray<string> {
try {
let raw = nativeShowcaseControls()?.getShowcasePairingUrl?.()?.trim();
if (!raw) return [];
if (raw.startsWith("json-uri:")) {
try {
raw = decodeURIComponent(raw.slice("json-uri:".length));
} catch {
return [];
}
}
try {
const parsed: unknown = JSON.parse(raw);
if (Array.isArray(parsed)) {
return parsed.filter(
(candidate): candidate is string =>
typeof candidate === "string" && candidate.trim().length > 0,
);
}
} catch {}
return [raw];
} catch {
return [];
}
}
export function getNativeShowcaseScene(): ShowcaseScene | null {
try {
const scene = nativeShowcaseControls()?.getShowcaseScene?.()?.trim();
return SHOWCASE_SCENES.find((candidate) => candidate === scene) ?? null;
} catch {
return null;
}
}
export function getNativeShowcaseTheme(): MobileThemeId | null {
try {
const theme = nativeShowcaseControls()?.getShowcaseTheme?.()?.trim();
return MOBILE_THEME_IDS.find((candidate) => candidate === theme) ?? null;
} catch {
return null;
}
}
export function prepareNativeShowcaseCapture(): void {
try {
nativeShowcaseControls()?.prepareShowcaseCapture?.();
} catch {}
}
export function getNativeShowcaseOrientation(): ShowcaseOrientation | null {
try {
const orientation = nativeShowcaseControls()?.getShowcaseOrientation?.()?.trim();
return orientation === "portrait" || orientation === "landscape" ? orientation : null;
} catch {
return null;
}
}
export async function applyNativeShowcaseOrientation(
orientation: ShowcaseOrientation,
): Promise<boolean> {
const controls = nativeShowcaseControls();
if (!controls?.applyShowcaseOrientation || !controls.getInterfaceOrientation) {
// Older binaries cannot change orientation; report success to avoid endless retries.
return true;
}
try {
await controls.applyShowcaseOrientation(orientation);
// Confirm async geometry changes so requests made before foreground activation can retry.
await new Promise((resolve) => setTimeout(resolve, 500));
return (await controls.getInterfaceOrientation()) === orientation;
} catch {
return false;
}
}
export function markNativeShowcaseReady(scene: ShowcaseScene): void {
try {
nativeShowcaseControls()?.markShowcaseReady?.(scene);
} catch {}
}