export function cn(...a) {
  return a.filter(Boolean).join(" ");
}

export function toFaDigits(v) {
  try {
    return String(v).toLocaleString("fa-IR");
  } catch {
    return v;
  }
}

export function maskEmail(email) {
  if (!email) return "-";
  const [u, d] = String(email).split("@");
  if (!d) return email;
  if (u.length <= 2) return `${u[0] || ""}***@${d}`;
  return `${u.slice(0, 2)}***@${d}`;
}

export function maskPhone(phone) {
  if (!phone) return "-";
  const p = String(phone);
  if (p.length < 7) return p;
  return `${p.slice(0, 4)}***${p.slice(-2)}`;
}

export const getErrMsg = (e, fallback = "خطا") => {
  const d = e?.response?.data;
  if (!d) return e?.message || fallback;
  if (typeof d === "string") return d;
  if (d.detail) return d.detail;
  try {
    const first = typeof d === "object" ? Object.values(d)?.flat?.()?.[0] : null;
    return first || fallback;
  } catch {
    return fallback;
  }
};

export const mapVerifyType = (type) => {
  if (type === "email") return { channel: "email", label: "ایمیل" };
  return { channel: "sms", label: "موبایل" };
};
