---
import type { Locale } from '@/data/site';

interface Props {
  success?: string | null;
  error?: string | null;
  locale: Locale;
}

const { success, error, locale } = Astro.props;

const successText: Record<string, Record<Locale, string>> = {
  created: { ar: 'تم الإنشاء بنجاح.', en: 'Created successfully.' },
  updated: { ar: 'تم التحديث بنجاح.', en: 'Updated successfully.' },
  deleted: { ar: 'تم الحذف بنجاح.', en: 'Deleted successfully.' },
  saved: { ar: 'تم حفظ التغييرات.', en: 'Your changes were saved.' },
  'test-sent': { ar: 'تم إرسال بريد الاختبار.', en: 'Test email sent.' },
};

const errorText: Record<string, Record<Locale, string>> = {
  'in-use': {
    ar: 'لا يمكن الحذف، هذا العنصر قيد الاستخدام حالياً.',
    en: 'Cannot delete — this item is currently in use.',
  },
};

const successMessage = success
  ? successText[success]?.[locale] ?? (locale === 'ar' ? 'تم الحفظ.' : 'Saved.')
  : null;
const errorMessage = error ? errorText[error]?.[locale] ?? error : null;
---

{
  successMessage && (
    <div class="alert alert-success mb-4 text-sm" role="status">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 shrink-0 stroke-current" fill="none" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
      </svg>
      <span>{successMessage}</span>
    </div>
  )
}
{
  errorMessage && (
    <div class="alert alert-error mb-4 text-sm" role="alert">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 shrink-0 stroke-current" fill="none" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v3.75m0 3.75h.008M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
      </svg>
      <span>{errorMessage}</span>
    </div>
  )
}
