export function extractContactHandle(contactUrl) {
  if (!contactUrl) {
    return "";
  }

  try {
    const url = new URL(contactUrl);
    return url.searchParams.get("uid") ?? "";
  } catch {
    return "";
  }
}

export function extractContactSite(contactUrl) {
  if (!contactUrl) {
    return "";
  }

  try {
    const url = new URL(contactUrl);
    return url.searchParams.get("site") ?? "";
  } catch {
    return "";
  }
}

export function normalizeOfferRecord(rawRecord, index) {
  return {
    index,
    title: rawRecord.title?.trim() ?? "",
    offerUrl: rawRecord.offerUrl?.trim() ?? "",
    imageUrl: rawRecord.imageUrl?.trim() ?? "",
    merchantName: rawRecord.merchantName?.trim() ?? "",
    merchantUrl: rawRecord.merchantUrl?.trim() ?? "",
    rawCardText: rawRecord.rawCardText?.trim() ?? "",
    highlightTexts: Array.isArray(rawRecord.highlightTexts)
      ? rawRecord.highlightTexts.map((item) => String(item ?? "").trim()).filter(Boolean)
      : [],
    contact: {
      type: "unknown",
      url: "",
      handle: "",
      site: ""
    }
  };
}
