(function (root, factory) {
  if (typeof module !== 'undefined' && module.exports) module.exports = factory;
  else root.createCardPlanning = factory;
})(typeof self !== 'undefined' ? self : this, function createCardPlanning(root) {
  function sync(disclosure) {
    disclosure.querySelector('summary')?.setAttribute(
      'aria-expanded', String(disclosure.open)
    );
  }

  function wire() {
    const disclosures = Array.from(root.querySelectorAll('[data-card-planning]'));
    disclosures.forEach(disclosure => {
      if (disclosure.dataset.cardPlanningBound === 'true') return;
      disclosure.dataset.cardPlanningBound = 'true';
      sync(disclosure);
      disclosure.addEventListener('toggle', () => {
        if (disclosure.open) {
          disclosures.forEach(other => {
            if (other === disclosure || !other.open) return;
            other.open = false;
            sync(other);
          });
        }
        sync(disclosure);
      });
      disclosure.addEventListener('keydown', event => {
        if (event.key !== 'Escape' || !disclosure.open) return;
        disclosure.open = false;
        sync(disclosure);
        disclosure.querySelector('summary')?.focus();
        event.preventDefault();
        event.stopPropagation();
      });
    });
  }

  return { wire };
});
