/**
 * TrackSharp Admin JavaScript
 * Extracted from inline JS in Tracksharp-tracker.php
 */
(function () {
  function moveTracksharpNotices() {
    var container = document.querySelector('.tracksharp-admin');
    if (!container || !container.parentNode) {
      return;
    }
    var parent = container.parentNode;
    document.querySelectorAll('#wpbody-content > .notice, #wpbody-content > .updated, #wpbody-content > .error').forEach(function (notice) {
      if (notice.dataset.tracksharpLocked === '1') {
        return;
      }
      notice.dataset.tracksharpLocked = '1';
      parent.insertBefore(notice, container);
    });
  }

  document.addEventListener('DOMContentLoaded', function () {
    moveTracksharpNotices();
    var wpBody = document.getElementById('wpbody-content');
    if (wpBody && window.MutationObserver) {
      var observer = new MutationObserver(function () {
        moveTracksharpNotices();
      });
      observer.observe(wpBody, { childList: true });
    }

    var attempts = 0;
    var interval = setInterval(function () {
      moveTracksharpNotices();
      attempts++;
      if (attempts >= 10) {
        clearInterval(interval);
      }
    }, 400);
  });

  document.addEventListener('click', function (e) {
    if (e.target && e.target.matches('[data-ws-toggle]')) {
      e.preventDefault();
      var id = e.target.getAttribute('data-ws-toggle');
      var el = document.getElementById(id);
      if (el) {
        el.classList.toggle('ws-hidden');
      }
    }
  });
})();
