export function normalizeMiniAppUrl(url: string) {
  if (typeof window === 'undefined') return url;

  try {
    const parsed = new URL(url, window.location.href);
    if (parsed.pathname.startsWith('/mini-apps/')) {
      return `${window.location.origin}${parsed.pathname}${parsed.search}${parsed.hash}`;
    }
    return parsed.toString();
  } catch {
    return url;
  }
}

export function resolveMiniAppTargetOrigin(iframeSrc: string | null | undefined, fallback: string) {
  if (!iframeSrc) return fallback;
  try {
    return new URL(iframeSrc, fallback).origin;
  } catch {
    return fallback;
  }
}
