/* Add custom JS code below */

(function () {
  /* ================== تشغيل الكود في الهوم فقط ================== */
  function isHomePageOnly() {
    try {
      if (
        window.salla &&
        salla.config &&
        typeof salla.config.get === "function"
      ) {
        var pageSlug = salla.config.get("page.slug");

        if (pageSlug) {
          pageSlug = String(pageSlug).toLowerCase();

          return (
            pageSlug === "index" ||
            pageSlug === "home" ||
            pageSlug === "homepage"
          );
        }
      }
    } catch (e) {}

    var path = window.location.pathname.replace(/\/+$/, "");

    return (
      path === "" ||
      path === "/" ||
      path === "/ar" ||
      path === "/en"
    );
  }

  function removeCustomSections() {
    var oldVideoSection = document.querySelector(".full-width-video-section");
    if (oldVideoSection) oldVideoSection.remove();
  }

  function runHomeCustomCode() {
    if (!isHomePageOnly()) {
      removeCustomSections();
      return;
    }

    addHeroVideoSection();
  }

  /* ================== فيديو الهيرو ================== */
  function addHeroVideoSection() {
    // يمنع تكرار السكشن لو الكود اشتغل أكثر من مرة
    var oldVideoSection = document.querySelector(".full-width-video-section");
    if (oldVideoSection) oldVideoSection.remove();

    var section = document.createElement("section");
    section.classList.add("full-width-video-section");

    var video = document.createElement("video");
    video.src = "https://github.com/salahreda-1/videos/raw/refs/heads/main/3493bf6765186223b0e72e79564b9ee351558b661-ezgif.com-gif-to-mp4-converter.mp4";

    video.autoplay = true;
    video.muted = true;
    video.loop = true;
    video.playsInline = true;

    video.setAttribute("autoplay", "");
    video.setAttribute("muted", "");
    video.setAttribute("loop", "");
    video.setAttribute("playsinline", "");

    video.style.width = "100%";
    video.style.height = "100%";
    video.style.objectFit = "cover";
    video.style.display = "block";

    section.appendChild(video);

    section.style.width = "100%";
    section.style.height = "100%";
    section.style.overflow = "hidden";
    section.style.margin = "0";
    section.style.padding = "0";

    // يضيف الفيديو كأول سكشن في محتوى الصفحة بعد الهيدر
    var mainContent =
      document.querySelector("main") ||
      document.querySelector(".main-content") ||
      document.querySelector(".app-inner") ||
      document.body;

    if (mainContent.firstChild) {
      mainContent.insertBefore(section, mainContent.firstChild);
    } else {
      mainContent.appendChild(section);
    }

    video.play().catch(function () {});
  }

  /* ================== تشغيل آمن بعد تحميل الصفحة ================== */
  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", runHomeCustomCode);
  } else {
    runHomeCustomCode();
  }

  window.addEventListener("load", runHomeCustomCode);

  setTimeout(runHomeCustomCode, 700);
  setTimeout(runHomeCustomCode, 1500);
  setTimeout(runHomeCustomCode, 3000);
})();