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

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

    callback();
  }

  function setupDragAndDrop() {
    var moveForm = global.document.getElementById("mc-menu-move-form");
    if (!moveForm) {
      return;
    }

    var draggedItemId = "";
    var draggedMenuId = "";

    global.document.querySelectorAll("[data-menu-draggable]").forEach(function bindDrag(item) {
      item.addEventListener("dragstart", function onDragStart(event) {
        draggedItemId = item.getAttribute("data-menu-item-id") || "";
        draggedMenuId = item.getAttribute("data-menu-id") || "";
        item.classList.add("mc-menu-node__row--dragging");
        if (event.dataTransfer) {
          event.dataTransfer.effectAllowed = "move";
          event.dataTransfer.setData("text/plain", draggedItemId);
        }
      });

      item.addEventListener("dragend", function onDragEnd() {
        item.classList.remove("mc-menu-node__row--dragging");
        draggedItemId = "";
        draggedMenuId = "";
        global.document.querySelectorAll(".mc-menu-drop-target--active").forEach(function clear(target) {
          target.classList.remove("mc-menu-drop-target--active");
        });
      });
    });

    global.document.querySelectorAll("[data-menu-id][data-parent-menu-item-id][data-after-menu-item-id]").forEach(function bindTarget(target) {
      target.addEventListener("dragover", function onDragOver(event) {
        if (!draggedItemId || draggedMenuId !== (target.getAttribute("data-menu-id") || "")) {
          return;
        }

        event.preventDefault();
        target.classList.add("mc-menu-drop-target--active");
      });

      target.addEventListener("dragleave", function onDragLeave() {
        target.classList.remove("mc-menu-drop-target--active");
      });

      target.addEventListener("drop", function onDrop(event) {
        if (!draggedItemId || draggedMenuId !== (target.getAttribute("data-menu-id") || "")) {
          return;
        }

        event.preventDefault();
        moveForm.querySelector('input[name="menu_id"]').value = target.getAttribute("data-menu-id") || "";
        moveForm.querySelector('input[name="menu_item_id"]').value = draggedItemId;
        moveForm.querySelector('input[name="parent_menu_item_id"]').value = target.getAttribute("data-parent-menu-item-id") || "";
        moveForm.querySelector('input[name="after_menu_item_id"]').value = target.getAttribute("data-after-menu-item-id") || "";
        moveForm.submit();
      });
    });
  }

  function setupBrowserSearch() {
    var search = global.document.querySelector("[data-menu-page-search]");
    var tree = global.document.querySelector("[data-menu-browser-tree]");
    var results = global.document.querySelector("[data-menu-browser-results]");
    var empty = global.document.querySelector("[data-menu-browser-empty]");

    if (!search || !tree || !results) {
      return;
    }

    var rows = Array.prototype.slice.call(global.document.querySelectorAll("[data-menu-browser-result]"));

    function render() {
      var term = (search.value || "").trim().toLowerCase();
      if (term === "") {
        tree.hidden = false;
        results.hidden = true;
        if (empty) {
          empty.hidden = true;
        }
        rows.forEach(function showAll(row) {
          row.hidden = false;
        });
        return;
      }

      tree.hidden = true;
      results.hidden = false;

      var visibleCount = 0;
      rows.forEach(function filterRow(row) {
        var haystack = (row.getAttribute("data-search-text") || "").toLowerCase();
        var matches = haystack.indexOf(term) !== -1;
        row.hidden = !matches;
        if (matches) {
          visibleCount += 1;
        }
      });

      if (empty) {
        empty.hidden = visibleCount !== 0;
      }
    }

    search.addEventListener("input", render);
    render();
  }

  function setupMenuManager() {
    setupDragAndDrop();
    setupBrowserSearch();
  }

  onReady(setupMenuManager);
})(window);
