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

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

  function renumberRows(group) {
    Array.prototype.slice.call(group.querySelectorAll("[data-mc-footer-repeater-row]")).forEach(function updateRow(row, index) {
      var order = row.querySelector("[data-mc-footer-repeater-order]");
      if (order) {
        order.value = String(index + 1);
      }
      row.querySelectorAll("input, textarea, select").forEach(function updateName(field) {
        var name = field.getAttribute("name") || "";
        field.setAttribute("name", name.replace(/\]\[\d+\]\[/, "][" + index + "]["));
      });
    });
  }

  function clearRow(row) {
    row.querySelectorAll("input, textarea, select").forEach(function clearField(field) {
      if (field.type === "hidden" && field.hasAttribute("data-mc-footer-repeater-order")) {
        return;
      }
      if (field.type === "checkbox" || field.type === "radio") {
        field.checked = false;
        return;
      }
      field.value = "";
    });
    row.querySelectorAll("[data-mc-asset-picker-preview]").forEach(function clearPreview(preview) {
      preview.innerHTML = "<span>No asset selected.</span>";
    });
    row.querySelectorAll(".mc-asset-picker-field").forEach(function updateAssetIds(field, index) {
      var suffix = "footer-added-" + Date.now() + "-" + index;
      var input = field.querySelector("[data-mc-asset-picker-target]");
      var preview = field.querySelector("[data-mc-asset-picker-preview]");
      if (!input || !preview) {
        return;
      }
      input.id = suffix + "-input";
      preview.id = suffix + "-preview";
      field.querySelectorAll("[data-mc-asset-picker-target-id]").forEach(function updateTarget(button) {
        button.setAttribute("data-mc-asset-picker-target-id", input.id);
      });
      field.querySelectorAll("[data-mc-asset-picker-preview-id]").forEach(function updatePreview(button) {
        button.setAttribute("data-mc-asset-picker-preview-id", preview.id);
      });
    });
  }

  function setupZonePanels() {
    var sheet = global.document.getElementById("footer-configure-sheet");
    if (!sheet) {
      return;
    }
    var panels = Array.prototype.slice.call(sheet.querySelectorAll("[data-mc-footer-config-panel]"));
    var empty = sheet.querySelector("[data-mc-visual-section-empty]");

    function activate(variantId) {
      var matched = false;
      panels.forEach(function toggle(panel) {
        var isActive = panel.getAttribute("data-footer-variant") === variantId;
        panel.hidden = !isActive;
        panel.setAttribute("aria-hidden", isActive ? "false" : "true");
        matched = matched || isActive;
      });
      if (empty) {
        empty.hidden = matched;
        empty.setAttribute("aria-hidden", matched ? "true" : "false");
      }
    }

    activate("");
    global.document.querySelectorAll("[data-mc-footer-variant]").forEach(function bind(button) {
      button.addEventListener("click", function onClick() {
        activate(button.getAttribute("data-mc-footer-variant") || "");
      });
    });

    sheet.addEventListener("change", function onZoneToggle(event) {
      var toggle = event.target && typeof event.target.closest === "function"
        ? event.target.closest("[data-mc-footer-zone-toggle]")
        : null;
      if (!toggle) {
        return;
      }
      var section = toggle.closest("[data-mc-footer-zone-section]");
      var content = section ? section.querySelector("[data-mc-footer-zone-content]") : null;
      if (content) {
        content.hidden = !toggle.checked;
      }
    });
  }

  function setupRepeaters() {
    global.document.querySelectorAll("[data-mc-footer-repeater]").forEach(function bindRepeater(repeater) {
      var group = repeater.querySelector("[data-mc-repeatable-group]");
      if (!group) {
        return;
      }

      var add = repeater.querySelector("[data-mc-footer-repeater-add]");
      if (add) {
        add.addEventListener("click", function onAdd() {
          var rows = group.querySelectorAll("[data-mc-footer-repeater-row]");
          if (!rows.length) {
            return;
          }
          var clone = rows[rows.length - 1].cloneNode(true);
          clearRow(clone);
          group.appendChild(clone);
          renumberRows(group);
        });
      }

      repeater.addEventListener("click", function onMove(event) {
        var button = event.target && typeof event.target.closest === "function"
          ? event.target.closest("[data-mc-footer-repeater-move]")
          : null;
        if (!button) {
          return;
        }
        event.preventDefault();
        var row = button.closest("[data-mc-footer-repeater-row]");
        if (!row) {
          return;
        }
        if (button.getAttribute("data-mc-footer-repeater-move") === "up" && row.previousElementSibling) {
          group.insertBefore(row, row.previousElementSibling);
        }
        if (button.getAttribute("data-mc-footer-repeater-move") === "down" && row.nextElementSibling) {
          group.insertBefore(row.nextElementSibling, row);
        }
        renumberRows(group);
      });
    });
  }

  onReady(function setupFooterDesign() {
    setupZonePanels();
    setupRepeaters();
  });
})(window);
