(function () {
  "use strict";

  function renderFallback() {
    document.querySelectorAll(".gmaps").forEach(function (node) {
      node.innerHTML = '<div class="map-empty-state">Google Maps API anahtari tanimlanmadi.</div>';
    });
  }

  function loadGoogleMaps() {
    var apiKey = window.KA_APEX_GOOGLE_MAPS_API_KEY;

    if (!apiKey) {
      renderFallback();
      return;
    }

    var script = document.createElement("script");
    script.src = "https://maps.googleapis.com/maps/api/js?key=" + encodeURIComponent(apiKey);
    script.async = true;
    script.defer = true;
    script.onload = function () {
      if (typeof window.initMap === "function") {
        window.initMap();
      }
    };
    script.onerror = renderFallback;
    document.head.appendChild(script);
  }

  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", loadGoogleMaps);
    return;
  }

  loadGoogleMaps();
})();
