(function initMyronFiles(global) {
  "use strict";

  function onReady(callback) {
    if (global.document.readyState === "loading") {
      global.document.addEventListener("DOMContentLoaded", callback, { once: true });
      return;
    }

    callback();
  }

  /**
   * Resolve the sibling status element for a copy button, if any.
   *
   * Precedence:
   *   1. button.dataset.mcCopyStatusTarget — explicit selector
   *   2. nearest [data-mc-copy-status] within an ancestor (most common: the
   *      modal body wraps the button and status span side by side)
   *   3. null
   */
  function resolveCopyStatusElement(button) {
    var explicit = button.getAttribute("data-mc-copy-status-target");
    if (explicit) {
      var resolved = global.document.querySelector(explicit);
      if (resolved) {
        return resolved;
      }
    }
    var parent = button.parentElement;
    while (parent) {
      var nested = parent.querySelector("[data-mc-copy-status]");
      if (nested) {
        return nested;
      }
      parent = parent.parentElement;
    }
    return null;
  }

  /**
   * Determine what gets copied:
   *   - data-mc-copy-value="<literal>" → copy verbatim (tokens, IDs, secrets)
   *   - data-mc-copy-url="<url>"       → URL-resolve relative to current page
   *
   * Both attributes may appear on a single button (literal wins).
   *
   * Returns null when neither attribute carries a value (no-op click).
   */
  function resolveClipboardValue(button) {
    var literal = button.getAttribute("data-mc-copy-value");
    if (literal !== null && literal !== "") {
      return literal;
    }
    var url = button.getAttribute("data-mc-copy-url");
    if (url !== null && url !== "") {
      try {
        return new global.URL(url, global.location.href).href;
      } catch (e) {
        return url;
      }
    }
    return null;
  }

  function setupCopyButtons() {
    var buttons = global.document.querySelectorAll(
      "[data-mc-copy-url],[data-mc-copy-value]"
    );
    if (!buttons.length || !global.navigator.clipboard) {
      return;
    }

    buttons.forEach(function bindButton(button) {
      button.addEventListener("click", function onClick() {
        var value = resolveClipboardValue(button);
        if (value === null) {
          return;
        }

        var persistent = button.getAttribute("data-mc-copy-feedback") === "persistent";

        global.navigator.clipboard.writeText(value).then(function onSuccess() {
          var originalLabel = button.textContent || "Copy";
          button.textContent = "Copied";

          if (persistent) {
            // APO-204 Card 2 — persistent-feedback variant.
            // Skip the auto-restore; surface an in-place "Token Copied"
            // message on a sibling [data-mc-copy-status] element so the
            // operator has a clear, non-time-bound confirmation.
            var status = resolveCopyStatusElement(button);
            if (status) {
              status.textContent = "Token Copied";
              status.removeAttribute("hidden");
              status.setAttribute("aria-hidden", "false");
            }
            return;
          }

          global.setTimeout(function restoreLabel() {
            button.textContent = originalLabel;
          }, 1200);
        });
      });
    });
  }

  onReady(setupCopyButtons);
})(window);
