import { resolveCommanderBaseUrl } from "./fechamentoBaseUrl";

function commanderOrigin(): string {
  const base = resolveCommanderBaseUrl();
  return base.replace(/\/api\/?$/i, "");
}

/**
 * Resolve URL absoluta da logo do parceiro.
 * Aceita URL absoluta, protocolo relativo (//), path absoluto (/...) ou path relativo (ex.: pasta/arquivo).
 */
export function resolveParceiroFotoUrl(logoUrl: string | null | undefined): string | null {
  if (logoUrl == null) return null;
  const t = logoUrl.trim();
  if (!t) return null;
  if (/^https?:\/\//i.test(t)) return t;
  if (t.startsWith("//")) {
    return typeof window !== "undefined" && window.location.protocol === "http:" ? `http:${t}` : `https:${t}`;
  }
  const origin = commanderOrigin();
  const withLeadingSlash = t.startsWith("/") ? t : `/${t}`;
  return `${origin}${withLeadingSlash}`;
}
