/** 兼容后端 camelCase / snake_case 及嵌套字段，避免推广侧拿到错误密钥 */

export function extractApiKeyFromPlanLike(record: any): string {
  if (!record || typeof record !== "object") return "";
  const cfg = record.config;
  const tu = record.textUrl ?? record.text_url;
  const candidates = [
    record.apiKey,
    record.api_key,
    tu?.apiKey,
    tu?.api_key,
    cfg?.apiKey,
    cfg?.api_key,
  ];
  for (const v of candidates) {
    if (v != null && String(v).trim() !== "") return String(v).trim();
  }
  return "";
}

export function pickTextUrl(record: any):
  | { fullUrl?: string; apiKey?: string }
  | undefined {
  const tu = record?.textUrl ?? record?.text_url;
  if (!tu || typeof tu !== "object") return undefined;
  return {
    fullUrl: tu.fullUrl ?? tu.full_url,
    apiKey: tu.apiKey ?? tu.api_key,
  };
}
