const OSS_PREFIX = 'https://xiaokang88.oss-cn-hangzhou.aliyuncs.com/';

export type InputUserLike = {
  avatar?: string;
  nick_name?: string;
};

export function normalizeInputUser(
  row: Record<string, any> | null | undefined,
): InputUserLike | null {
  if (!row) return null;
  const u = row.input_user ?? row.inputUser;
  if (!u) return null;
  if (!row.inputUser) {
    row.inputUser = u;
  }
  return u;
}

export function resolveAvatarUrl(url?: string | null): string {
  if (!url) return '';
  const s = String(url).trim();
  if (!s) return '';
  if (/^https?:\/\//i.test(s)) return s;
  if (s.startsWith('//')) return `https:${s}`;
  return `${OSS_PREFIX}${s.replace(/^\//, '')}`;
}

export function inputUserDisplayName(u: InputUserLike | null | undefined): string {
  return u?.nick_name?.trim() || '-';
}
