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

export const ORGFRONT_AUTH_CALLBACK_PATH = "/auth/callback";

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

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

export function buildOrgFrontAuthRedirectUris(
  publicOrigin: string,
): OrgFrontAuthRedirectUris {
  return {
    redirectUri: `${publicOrigin}${ORGFRONT_AUTH_CALLBACK_PATH}`,
    postLogoutRedirectUri: publicOrigin,
    popupRedirectUri: `${publicOrigin}${ORGFRONT_AUTH_CALLBACK_PATH}`,
  };
}
