import type { InspectBridge } from '../cli/commands/inspect/core' const APP_URL_SCHEME_RE = /^[A-Za-z][A-Za-z0-9+.-]*:/ export function normalizeAppUrlTarget(target: string): string { const trimmed = target.trim() if (!trimmed) { throw new Error('expected ') } if (APP_URL_SCHEME_RE.test(trimmed) || trimmed.startsWith('/')) { return trimmed } return `/${trimmed}` } export function isAppDeepLinkTarget(target: string): boolean { const trimmed = target.trim() if (!trimmed) return false if (trimmed.startsWith('/')) return true if (!APP_URL_SCHEME_RE.test(trimmed)) return false return !/^https?:\/\//i.test(trimmed) } export async function openAppUrl(bridge: InspectBridge, target: string): Promise { const normalized = normalizeAppUrlTarget(target) const result: unknown = await bridge.send({ type: 'openUrl', url: normalized, }) if ( result !== null && typeof result === 'object' && Reflect.get(result, 'ok') === false ) { const error = Reflect.get(result, 'error') throw new Error(typeof error === 'string' ? error : 'deep-link dispatch failed') } return normalized }