import type { PublicContact } from '@/services/settings' import { mailtoHref, telHref } from '@/services/settings' export type SupportLinkKind = | 'whatsapp' | 'telegram' | 'instagram' | 'phone' | 'email' export type SupportLinkItem = { kind: SupportLinkKind label: string href: string subtitle: string } function trimValue(value: string | null | undefined): string { return value?.trim() ?? '' } function isHttpUrl(value: string): boolean { return /^https?:\/\//i.test(value) } function whatsappHref(raw: string): string | undefined { if (!raw) return undefined if (isHttpUrl(raw)) return raw const digits = raw.replace(/[^\d]/g, '') if (!digits) return undefined return `https://wa.me/${digits}` } function telegramHref(raw: string): string | undefined { if (!raw) return undefined if (isHttpUrl(raw)) return raw const handle = raw.replace(/^@/, '').replace(/^https?:\/\/(t\.me|telegram\.me)\//i, '').trim() if (!handle) return undefined return `https://t.me/${handle}` } function instagramHref(raw: string): string | undefined { if (!raw) return undefined if (isHttpUrl(raw)) return raw const handle = raw.replace(/^@/, '').replace(/^instagram\.com\//i, '').trim() if (!handle) return undefined return `https://instagram.com/${handle}` } export function buildSupportLinks(contact: PublicContact | null): SupportLinkItem[] { if (!contact) return [] const items: SupportLinkItem[] = [] const wa = trimValue(contact.supportWhatsapp) const waHref = whatsappHref(wa) if (waHref) { items.push({ kind: 'whatsapp', label: 'واتساپ', href: waHref, subtitle: wa }) } const tg = trimValue(contact.supportTelegram) const tgHref = telegramHref(tg) if (tgHref) { items.push({ kind: 'telegram', label: 'تلگرام', href: tgHref, subtitle: tg }) } const ig = trimValue(contact.supportInstagram) const igHref = instagramHref(ig) if (igHref) { items.push({ kind: 'instagram', label: 'اینستاگرام', href: igHref, subtitle: ig }) } const phone = trimValue(contact.supportPhone) const phoneHref = telHref(phone) if (phoneHref) { items.push({ kind: 'phone', label: 'تماس تلفنی', href: phoneHref, subtitle: phone }) } const email = trimValue(contact.supportEmail) const emailHref = mailtoHref(email) if (emailHref) { items.push({ kind: 'email', label: 'ایمیل', href: emailHref, subtitle: email }) } return items }