import type { AdminProfile } from "@/types/api/admin-auth";

/** 按玩家 site_code 从登录态解析 admin_sites.id（结算/信用流水 API 必填）。 */
export function resolveAdminSiteId(
  siteCode: string,
  profile: AdminProfile | null | undefined,
): number | null {
  const code = siteCode.trim().toUpperCase();
  if (code === "") {
    return null;
  }

  if (profile?.agent?.site_code?.trim().toUpperCase() === code) {
    return profile.agent.admin_site_id;
  }
  if (profile?.site?.code?.trim().toUpperCase() === code) {
    return profile.site.id;
  }

  const fromAccessible = profile?.accessible_sites?.find(
    (site) => site.code.trim().toUpperCase() === code,
  );
  if (fromAccessible) {
    return fromAccessible.id;
  }

  return null;
}
