/**
 * JavaScript for plugin "{$pluginSlug}"
 * Requested: {$reqPath}
 * Namespace: {$ns}
 */

(function(window, document) {
  'use strict';

  // Namespace container
  var {$ns} = {
    version: "1.0." + Math.floor(Math.random() * 50),
    settings: {
      enabled: true,
      theme: "light",
      retries: 3
    },
    state: {
      initialized: false,
      lastAction: null
    }
  };

  // Utility function
  {$ns}.log = function(msg) {
    if (window.console && console.log) {
      console.log("[{$pluginSlug}] " + msg);
    }
    {$ns}.state.lastAction = msg;
  };

  // DOM ready
  {$ns}.ready = function(fn) {
    if (document.readyState !== "loading") {
      fn();
    } else {
      document.addEventListener("DOMContentLoaded", fn);
    }
  };

  // component initializer
  {$ns}.initWidget = function(selector) {
    {$ns}.ready(function() {
      var el = document.querySelector(selector);
      if (el) {
        el.setAttribute("data-{$pluginSlug}-init", "1");
        {$ns}.log("Widget initialized on " + selector);
      } else {
        {$ns}.log("No element found for " + selector);
      }
    });
    {$ns}.state.initialized = true;
  };

  // API call
  {$ns}.fetchData = function(url) {
    return new Promise(function(resolve) {
      {$ns}.log("Fetching from " + url);
      setTimeout(function() {
        resolve({ status: "ok", data: [] });
      }, 300 + Math.random() * 500);
    });
  };

  // Expose namespace
  window.{$ns} = {$ns};

  // Auto-init example
  {$ns}.initWidget(".c-card--{$seed}");

})(window, document);