import { message } from 'ant-design-vue' import { useSettingsStore } from '@/store/settingsStore' import { getConfigApp } from '@/utils/config' export function getReferralShareTitle(projectName?: string | null) { return projectName ?? getConfigApp('name_fa') ?? getConfigApp('name') ?? 'طلا' } export const DEFAULT_INVITE_LINK_TEXT = `⭐️الان طلا بخر؛ آخر ماه سودشو ببر! با خرید طلای آبشده در مقدارهای کم ولی مستمر، سود بیشتری ببر! این دعوت نامه از طرف "٪نام" عزیز برای شما ارسال شده تا شما هم از بازار طلای ما جا نمونید. روی لینک زیر کلیک کنید و ثبت نام خود را تکمیل کنید. ٪لینک` export function getInviteLinkTemplate(): string { try { const fromSettings = String(useSettingsStore().getSettingByKey('invite_link_text') ?? '').trim() if (fromSettings) { return fromSettings } } catch { // settings store may not be ready yet } return DEFAULT_INVITE_LINK_TEXT } /** جایگزینی ٪نام / %نام و ٪لینک / %لینک در قالب متن دعوت */ export function renderInviteLinkText( template: string, options: { userName?: string | null; link?: string | null }, ): string { const userName = String(options.userName ?? '').trim() const link = String(options.link ?? '').trim() return template .replace(/[%٪]\s*لینک/gu, link) .replace(/[%٪]\s*نام/gu, userName) } function resolveReferralRegisterLink(referralId?: string | number | null): string { const id = String(referralId ?? '').trim() if (!id) return '' const query = `register=true&id=${encodeURIComponent(id)}` let base = '' try { base = String(useSettingsStore().getSettingByKey('app_url') ?? '').trim() } catch { base = '' } if (base) { return `${base.replace(/\/+$/, '')}/login?${query}` } if (typeof window !== 'undefined' && window.location?.origin) { return `${window.location.origin}/login?${query}` } return `/login?${query}` } export function getReferralShareText(options: { userName?: string | null projectName?: string | null referralId?: string | number | null }) { const userName = options.userName ?? '' const link = resolveReferralRegisterLink(options.referralId) return renderInviteLinkText(getInviteLinkTemplate(), { userName, link }) } export async function shareReferralText(text: string, title?: string | null): Promise { const shareTitle = title ?? getReferralShareTitle() if (typeof navigator !== 'undefined' && typeof navigator.share === 'function') { try { await navigator.share({ title: shareTitle, text }) return } catch (err) { if ((err as Error)?.name === 'AbortError') { return } } } if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) { await navigator.clipboard.writeText(text) message.info('متن دعوتنامه رونوشت شد') return } message.error('اشتراک‌گذاری در این مرورگر پشتیبانی نمی‌شود') }