/* Add custom JS code below */ 

(() => {
  "use strict";

  const AMOUNTS = [10, 20, 50, 100];
  const CARD_SELECTOR = "body";
  const INPUT_SELECTOR = 'input[name="donating_amount"]';

  function setDonationAmount(input, amount) {
    // Use the native setter to notify reactive UI frameworks.
    const setter = Object.getOwnPropertyDescriptor(
      HTMLInputElement.prototype,
      "value"
    )?.set;

    if (setter) {
      setter.call(input, String(amount));
    } else {
      input.value = String(amount);
    }

    input.dispatchEvent(new Event("input", {
      bubbles: true,
      composed: true
    }));

    input.dispatchEvent(new Event("change", {
      bubbles: true,
      composed: true
    }));
  }

  function updateSelection(wrapper, value) {
    wrapper.querySelectorAll(".rasikhat-amount-btn")
      .forEach(button => {
        const selected = button.dataset.amount === value;
        button.classList.toggle("is-selected", selected);
        button.setAttribute("aria-pressed", String(selected));
      });
  }

  function addAmountButtons(input) {
    if (!input.isConnected) return;

    const card = input.closest(CARD_SELECTOR);
    if (!card) return;

    const parent = input.parentElement;
    if (!parent) return;

    if (parent.querySelector(".rasikhat-amounts")) return;

    const wrapper = document.createElement("div");
    wrapper.className = "rasikhat-amounts";
    wrapper.setAttribute("dir", "rtl");

    const title = document.createElement("div");
    title.className = "rasikhat-amounts-title";
    title.textContent = "اختر مبلغ التبرع";
    wrapper.appendChild(title);

    const options = document.createElement("div");
    options.className = "rasikhat-amounts-options";

    AMOUNTS.forEach(amount => {
      const button = document.createElement("button");
      button.type = "button";
      button.className = "rasikhat-amount-btn";
      button.dataset.amount = String(amount);
      button.textContent = `${amount} ر.س`;
      button.setAttribute("aria-pressed", "false");

      button.addEventListener("click", () => {
        setDonationAmount(input, amount);
        updateSelection(wrapper, String(amount));
      });

      options.appendChild(button);
    });

    wrapper.appendChild(options);
    parent.insertBefore(wrapper, input);

    input.addEventListener("input", () => {
      updateSelection(wrapper, input.value.trim());
    });

    updateSelection(wrapper, input.value.trim());
  }

  function initialize() {
    document.querySelectorAll(
      `${CARD_SELECTOR} ${INPUT_SELECTOR}`
    ).forEach(addAmountButtons);
  }

  let scheduled = false;

  function scheduleInitialize() {
    if (scheduled) return;
    scheduled = true;

    requestAnimationFrame(() => {
      scheduled = false;
      initialize();
    });
  }

  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", initialize, {
      once: true
    });
  } else {
    initialize();
  }

  const observer = new MutationObserver(scheduleInitialize);
  observer.observe(document.documentElement, {
    childList: true,
    subtree: true
  });
})();