(function () {
  'use strict';

  function qa(sel, root) {
    return Array.prototype.slice.call((root || document).querySelectorAll(sel));
  }

  var KEY = 'custausu_acc_state:' + location.host + ':custausu-settings:routes-templates:v1';

  function readState() {
    try {
      var raw = localStorage.getItem(KEY);
      if (!raw) return null;
      var obj = JSON.parse(raw);
      return (obj && typeof obj === 'object') ? obj : null;
    } catch (e) {
      return null;
    }
  }

  function writeState(state) {
    try {
      localStorage.setItem(KEY, JSON.stringify(state || {}));
    } catch (e) {}
  }

  function collectState() {
    var state = {};
    qa('details.custausu-acc[data-lang]').forEach(function (d) {
      var lang = d.getAttribute('data-lang') || '';
      if (!lang) return;
      state[lang] = !!d.open;
    });
    return state;
  }

  function applyState(state) {
    if (!state) return false;
    var touched = false;

    qa('details.custausu-acc[data-lang]').forEach(function (d) {
      var lang = d.getAttribute('data-lang') || '';
      if (!lang) return;

      if (Object.prototype.hasOwnProperty.call(state, lang)) {
        d.open = !!state[lang];
        touched = true;
      }
    });

    return touched;
  }

  function getAllDetails() {
    return qa('details.custausu-acc[data-lang]');
  }

  function setAll(open) {
    getAllDetails().forEach(function (d) {
      d.open = !!open;
    });
    writeState(collectState());
    updateButtonsState();
  }

  function computeAllOpenState() {
    var all = getAllDetails();
    if (!all.length) {
      return { allOpen: false, allClosed: false };
    }

    var openCount = 0;
    all.forEach(function (d) {
      if (d.open) openCount++;
    });

    return {
      allOpen: openCount === all.length,
      allClosed: openCount === 0
    };
  }

  function setBtnDisabled(btn, disabled) {
    if (!btn) return;
    btn.disabled = !!disabled;
    btn.classList.toggle('is-disabled', !!disabled);
    btn.setAttribute('aria-disabled', disabled ? 'true' : 'false');
  }

  function updateButtonsState() {
    var st = computeAllOpenState();

    qa('.custausu-acc-expand').forEach(function (b) {
      setBtnDisabled(b, st.allOpen);
    });

    qa('.custausu-acc-collapse').forEach(function (b) {
      setBtnDisabled(b, st.allClosed);
    });
  }

  function initSavedState() {
    var saved = readState();
    var applied = applyState(saved);
    if (!applied) {
      writeState(collectState());
    }
  }

  function bindDetailsToggle() {
    qa('details.custausu-acc[data-lang]').forEach(function (d) {
      d.addEventListener('toggle', function () {
        writeState(collectState());
        updateButtonsState();
      }, { passive: true });
    });
  }

  function bindToolbarButtons() {
    document.addEventListener('click', function (ev) {
      var ex = ev.target && ev.target.closest ? ev.target.closest('.custausu-acc-expand') : null;
      var co = ev.target && ev.target.closest ? ev.target.closest('.custausu-acc-collapse') : null;

      if (ex) {
        ev.preventDefault();
        var st = computeAllOpenState();
        if (!st.allOpen) setAll(true);
        return;
      }

      if (co) {
        ev.preventDefault();
        var st2 = computeAllOpenState();
        if (!st2.allClosed) setAll(false);
      }
    });
  }

  function openAlertedAccordions() {
    document.querySelectorAll('.custausu-acc[data-route-alert="1"]').forEach(function (el) {
      el.open = true;
    });
  }

  function init() {
    initSavedState();
    bindDetailsToggle();
    bindToolbarButtons();
    openAlertedAccordions();
    updateButtonsState();
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }

  window.addEventListener('load', updateButtonsState);
})();