---
import { ClientRouter } from "astro:transitions";
import type { Locale } from "../data/site";
import { site } from "../data/site";
import { getDir, switchLocalePath, toPublicPath } from '../i18n/utils';
import { GOOGLE_ADS_ID } from "../lib/analytics/google-ads";
import "@emran-alhaddad/saudi-riyal-font/index.css";
import "flag-icons/css/flag-icons.min.css";
import "../styles/global.css";


interface Props {
  locale: Locale;
  title?: string;
  description?: string;
  canonical?: string;
  ogImage?: string;
  noindex?: boolean;
  analytics?: boolean;
}

const {
  locale,
  title,
  description,
  canonical,
  ogImage = "/brand/logo-header.png",
  noindex = false,
  analytics = true,
} = Astro.props;

const publicPath = Astro.originPathname ?? Astro.url.pathname;
const publicPathname = toPublicPath(publicPath, locale);
const pageTitle = title ? `${title} | ${site.name[locale]}` : site.name[locale];
const pageDescription = description ?? site.defaultDescription[locale];
const canonicalUrl = canonical ?? new URL(publicPathname, site.url).href;
const altLocale = locale === "ar" ? "en" : "ar";
const altPath = switchLocalePath(publicPathname, altLocale);
const altUrl = new URL(altPath, site.url).href;
const dir = getDir(locale);
---

<!doctype html>
<html lang={locale} dir={dir} data-analytics={analytics ? "true" : "false"}>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="generator" content={Astro.generator} />
    <title>{pageTitle}</title>
    <meta name="description" content={pageDescription} />
    {noindex && <meta name="robots" content="noindex, nofollow" />}
    <link rel="canonical" href={canonicalUrl} />
    <link rel="alternate" hreflang={locale} href={canonicalUrl} />
    <link rel="alternate" hreflang={altLocale} href={altUrl} />
    <link
      rel="alternate"
      hreflang="x-default"
      href={new URL("/", site.url).href}
    />
    <meta property="og:type" content="website" />
    <meta property="og:url" content={canonicalUrl} />
    <meta property="og:title" content={pageTitle} />
    <meta property="og:description" content={pageDescription} />
    <meta property="og:locale" content={locale === "ar" ? "ar_SA" : "en_US"} />
    <meta property="og:image" content={new URL(ogImage, site.url).href} />
    <meta name="twitter:card" content="summary_large_image" />
    <meta name="twitter:title" content={pageTitle} />
    <meta name="twitter:description" content={pageDescription} />
    <link rel="icon" type="image/jpeg" href="/favicon.jpeg" />
    <link rel="apple-touch-icon" href="/apple-touch-icon.png" />
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link
      href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;500;600;700;800&family=IBM+Plex+Sans+Arabic:wght@300;400;500;700&family=EB+Garamond:wght@700;800&display=swap"
      rel="stylesheet"
 
    />
    <slot name="head" />
    {
      analytics && (
        <>
          <script async src={`https://www.googletagmanager.com/gtag/js?id=${GOOGLE_ADS_ID}`}></script>
          <script is:inline define:vars={{ GOOGLE_ADS_ID }}>
            window.dataLayer = window.dataLayer || [];
            window.gtag = function gtag() {
              window.dataLayer.push(arguments);
            };
            window.gtag("js", new Date());
            window.gtag("config", GOOGLE_ADS_ID);
          </script>
        </>
      )
    }
    <ClientRouter />
  </head>
  <body>
    <slot />
    <script>
      import "../scripts/gads-conversions.ts";
      import "../scripts/whatsapp-availability.ts";
    </script>
    <script>
      // Scroll-reveal: adds .is-visible to .reveal elements as they enter viewport
      const observer = new IntersectionObserver(
        (entries) =>
          entries.forEach(
            (e) => e.isIntersecting && e.target.classList.add("is-visible"),
          ),
        { threshold: 0.12 },
      );
      document.addEventListener("astro:page-load", () => {
        document
          .querySelectorAll(".reveal")
          .forEach((el) => observer.observe(el));

        document.querySelectorAll("*:not(:has(*))").forEach((element) => {
          if (/^[0-9 \+]+$/.test(element.textContent)) {
            element.classList.add("text-ltr");
          }
        });
      });
    </script>
  </body>
</html>
