export interface AdminAuthRedirectUris {
  redirectUri: string;
  postLogoutRedirectUri: string;
  popupRedirectUri: string;
}

export const ADMIN_AUTH_CALLBACK_PATH = "/auth/callback";

export function resolveAdminPublicOrigin(
  configuredOrigin: string | undefined,
  browserOrigin: string,
) {
  const trimmed = configuredOrigin?.trim();
  if (!trimmed) {
    return browserOrigin;
  }

  try {
    return new URL(trimmed).origin;
  } catch {
    return browserOrigin;
  }
}

export function buildAdminAuthRedirectUris(
  publicOrigin: string,
): AdminAuthRedirectUris {
  return {
    redirectUri: `${publicOrigin}${ADMIN_AUTH_CALLBACK_PATH}`,
    postLogoutRedirectUri: publicOrigin,
    popupRedirectUri: `${publicOrigin}${ADMIN_AUTH_CALLBACK_PATH}`,
  };
}
