/**
 * Seekmodo connector documentation — tab navigation.
 * Self-contained; no external dependencies.
 */
document.addEventListener("DOMContentLoaded", function () {
  var sections = Array.prototype.slice.call(
    document.querySelectorAll(".doc-section")
  );
  var links = Array.prototype.slice.call(
    document.querySelectorAll(".doc-nav a[data-target]")
  );
  var sectionIds = [];
  var navToggle = document.getElementById("doc-nav-toggle");
  var navPanel = document.querySelector(".doc-nav-panel");

  if (!sections.length || !links.length) {
    return;
  }

  document.body.classList.add("has-doc-tabs");

  sections.forEach(function (section) {
    section.setAttribute("role", "tabpanel");
    section.setAttribute("aria-hidden", "true");
    section.style.display = "none";
  });

  links.forEach(function (link) {
    var target = link.getAttribute("data-target");
    if (target && sectionIds.indexOf(target) === -1) {
      sectionIds.push(target);
    }
    link.setAttribute("role", "tab");
    link.setAttribute("aria-controls", target);
    link.setAttribute("aria-selected", "false");
    link.setAttribute("tabindex", "-1");
  });

  function showSection(sectionId, options) {
    var opts = Object.assign({ updateHash: true }, options || {});
    if (sectionIds.indexOf(sectionId) === -1) {
      return;
    }

    sections.forEach(function (section) {
      var isActive = section.id === sectionId;
      section.classList.toggle("is-active", isActive);
      section.style.display = isActive ? "" : "none";
      section.setAttribute("aria-hidden", isActive ? "false" : "true");
    });

    links.forEach(function (link) {
      var matches = link.getAttribute("data-target") === sectionId;
      link.classList.toggle("active", matches);
      link.setAttribute("aria-selected", matches ? "true" : "false");
      link.setAttribute("tabindex", matches ? "0" : "-1");
    });

    if (opts.updateHash && window.location.hash !== "#" + sectionId) {
      history.replaceState(null, "", "#" + sectionId);
    }

    if (navPanel) {
      navPanel.classList.remove("is-open");
    }
  }

  links.forEach(function (link) {
    link.addEventListener("click", function (event) {
      event.preventDefault();
      showSection(link.getAttribute("data-target"));
    });
  });

  if (navToggle && navPanel) {
    navToggle.addEventListener("click", function () {
      navPanel.classList.toggle("is-open");
      navToggle.setAttribute(
        "aria-expanded",
        navPanel.classList.contains("is-open") ? "true" : "false"
      );
    });
  }

  var initial =
    window.location.hash && window.location.hash.substring(1);
  if (!initial || sectionIds.indexOf(initial) === -1) {
    initial = sectionIds[0];
  }
  showSection(initial, { updateHash: false });

  window.addEventListener("hashchange", function () {
    var hash = window.location.hash.substring(1);
    if (hash && sectionIds.indexOf(hash) !== -1) {
      showSection(hash, { updateHash: false });
    }
  });
});
