export function pickManagementLoginError(payload: any): string {
  const message = String(payload?.message || payload?.error || '').trim();
  if (payload?.error_code === 'WRONG_PORTAL') {
    return 'This login is only for internal management users. External users must use the public login page.';
  }
  if (message) return message;
  return 'Sign in failed.';
}

export function isExternalIdentity(payload: any): boolean {
  const audience = String(payload?.audience || payload?.user?.audience || '').trim().toLowerCase();
  if (audience === 'public' || audience === 'external') return true;

  const userType = String(
    payload?.userType ||
    payload?.user_type ||
    payload?.user?.userType ||
    payload?.user?.user_type ||
    '',
  ).trim().toLowerCase();
  if (userType === 'external_user' || userType === 'external') return true;

  const accountType = String(
    payload?.accountType ||
    payload?.account_type ||
    payload?.user?.accountType ||
    payload?.user?.account_type ||
    '',
  ).trim().toLowerCase();
  return accountType === 'external' || accountType === 'public';
}
