export type ClaimantIdentifier = {
  canonical: string;
  display: string;
  kind: "phone" | "wechat";
};

function stripLabel(value: string) {
  return value
    .replace(/^(?:微信号|微信|wechat|手机号|手机)\s*[:：]?\s*/i, "")
    .trim();
}

export function parseClaimantIdentifier(value: unknown): ClaimantIdentifier | null {
  const input = stripLabel(String(value ?? "").trim());
  if (!input) return null;

  const phone = input.replace(/[\s-]/g, "").replace(/^\+?86/, "");
  if (/^1[3-9]\d{9}$/.test(phone)) {
    return {
      canonical: `phone:${phone}`,
      display: phone,
      kind: "phone",
    };
  }

  if (/^[A-Za-z][A-Za-z0-9_-]{5,31}$/.test(input)) {
    return {
      canonical: `wechat:${input.toLowerCase()}`,
      display: input,
      kind: "wechat",
    };
  }

  return null;
}
