import type {Locale} from './catalog';
import type {CustomerNotification} from './dashboard';

const copy={
 fa:{activityReview:'بررسی فعالیت',adminNote:'یادداشت مدیر',activityContext:'این پیام مربوط به بررسی یک فعالیت است؛ وضعیت احراز هویت در پروفایل نمایش داده می‌شود.',title:'صندوق پیام‌ها',description:'پیام‌های مدیریت، نتیجه بررسی احراز هویت و اطلاع‌رسانی‌های حساب شما در این بخش نگهداری می‌شوند.',all:'همه پیام‌ها',unread:'خوانده‌نشده',markAll:'خواندن همه',markRead:'خوانده شد',read:'خوانده‌شده',refresh:'به‌روزرسانی',empty:'هنوز پیامی ندارید',emptyUnread:'همه پیام‌ها را خوانده‌اید',sender:'پلانک',identity:'مشاهده پرونده احراز هویت',account:'مشاهده حساب',ticket:'مشاهده تیکت',wallet:'مشاهده کیف پول',tokenize:'مشاهده درخواست دارایی',info:'اطلاع‌رسانی',success:'انجام‌شده',warning:'نیاز به پیگیری',error:'نیاز به اصلاح',newMessages:'پیام خوانده‌نشده',readError:'وضعیت پیام ذخیره نشد. دوباره تلاش کنید.'},
 en:{activityReview:'Activity review',adminNote:'Admin note',activityContext:'This message concerns one activity review. Your identity verification status is shown in your profile.',title:'Inbox',description:'Messages from the Planck team, identity review results and account updates are kept here.',all:'All messages',unread:'Unread',markAll:'Mark all as read',markRead:'Mark as read',read:'Read',refresh:'Refresh',empty:'No messages yet',emptyUnread:'You are all caught up',sender:'Planck',identity:'View identity review',account:'View account',ticket:'View ticket',wallet:'View wallet',tokenize:'View asset request',info:'Update',success:'Completed',warning:'Follow-up needed',error:'Action needed',newMessages:'unread messages',readError:'Could not update the message. Please try again.'},
 ar:{activityReview:'مراجعة نشاط',adminNote:'ملاحظة الإدارة',activityContext:'تتعلق هذه الرسالة بمراجعة نشاط واحد. تظهر حالة التحقق من هويتك في ملفك الشخصي.',title:'صندوق الرسائل',description:'رسائل فريق بلانك ونتائج مراجعة الهوية وتحديثات حسابك محفوظة هنا.',all:'كل الرسائل',unread:'غير المقروءة',markAll:'قراءة الكل',markRead:'تمييز كمقروءة',read:'مقروءة',refresh:'تحديث',empty:'لا توجد رسائل بعد',emptyUnread:'قرأت جميع الرسائل',sender:'بلانك',identity:'عرض مراجعة الهوية',account:'عرض الحساب',ticket:'عرض التذكرة',wallet:'عرض المحفظة',tokenize:'عرض طلب الأصل',info:'إشعار',success:'مكتمل',warning:'تحتاج إلى متابعة',error:'تحتاج إلى تصحيح',newMessages:'رسائل غير مقروءة',readError:'تعذر تحديث الرسالة. حاول مجدداً.'},
};
export function notificationCopy(locale:Locale){return copy[locale]||copy.fa}

// Notification actions may only navigate to known customer-facing screens.
export function notificationAction(notification:Pick<CustomerNotification,'action_url'>):string|null{
 const href=notification.action_url;
 if(!href||!href.startsWith('/')||href.startsWith('//')||href.includes('\\'))return null;
 for(let index=0;index<href.length;index++){const code=href.charCodeAt(index);if(code<=32||code===127)return null}
 try{
  const url=new URL(href,'https://planck.invalid');
  if(url.origin!=='https://planck.invalid')return null;
  const allowed=/^\/account(?:\/(?:profile|banks|security|settings|alerts|referrals|api|notifications|tickets))?\/?$/.test(url.pathname)
   ||/^\/wallet(?:\/(?:overview|assets|deposit|withdraw|transactions|orders|credit|staking|instant-credit))?\/?$/.test(url.pathname)
   ||url.pathname==='/tokenize';
  return allowed?url.pathname+url.search+url.hash:null;
 }catch{return null}
}
export function notificationPresentation(notification:CustomerNotification,locale:Locale){
 const t=notificationCopy(locale),href=notificationAction(notification);
 const activityReview=typeof notification.event_key==='string'&&/^activity-review:[1-9]\d*:[1-9]\d*$/.test(notification.event_key);
 const severity=notification.severity&&['info','success','warning','error'].includes(notification.severity)?notification.severity:'info';
 const actionLabel=href?.startsWith('/account/tickets')?t.ticket:href?.startsWith('/wallet')?t.wallet:href?.startsWith('/tokenize')?t.tokenize:href?.startsWith('/account/profile')||href==='/account'?t.identity:t.account;
 return {severity,label:t[severity],scope:activityReview?t.activityReview:null,noteLabel:activityReview?t.adminNote:null,context:activityReview?t.activityContext:null,sender:notification.sender_name?.trim()||t.sender,href,actionLabel};
}
export function notificationInboxHref(id:string){return `/account/notifications#message-${encodeURIComponent(id)}`}
export function notificationDate(value:string,locale:Locale){return Number.isNaN(Date.parse(value))?'—':new Intl.DateTimeFormat(locale,{dateStyle:'medium',timeStyle:'short'}).format(new Date(value))}
