(function (root, factory) {
  if (typeof module !== 'undefined' && module.exports) module.exports = factory;
  else root.createMobileTodayCommandBar = factory;
})(typeof window !== 'undefined' ? window : this, function createMobileTodayCommandBar({
  more,
  sheet,
  close,
  firstAction,
  endAction,
  existingEndControl,
  actionButtons = [],
  hud,
  dock,
  style,
  ResizeObserverImpl = typeof ResizeObserver === 'undefined' ? null : ResizeObserver,
}) {
  if (!more || !sheet) return null;

  const dismiss = () => {
    if (sheet.open) sheet.close();
  };
  const open = () => {
    if (sheet.open) return;
    more.setAttribute('aria-expanded', 'true');
    sheet.showModal();
    firstAction?.focus();
  };

  more.setAttribute('aria-expanded', 'false');
  more.addEventListener('click', open);
  close?.addEventListener('click', dismiss);
  actionButtons.forEach(button => button?.addEventListener('click', dismiss));
  endAction?.addEventListener('click', () => {
    dismiss();
    existingEndControl?.click();
  });
  sheet.addEventListener('cancel', event => {
    event.preventDefault();
    dismiss();
  });
  sheet.addEventListener('close', () => {
    more.setAttribute('aria-expanded', 'false');
    more.focus();
  });

  const measure = () => {
    if (!style || !hud || !dock) return;
    style.setProperty('--mobile-today-clearance', `${hud.offsetHeight + dock.offsetHeight + 16}px`);
  };
  const observer = ResizeObserverImpl && hud && dock ? new ResizeObserverImpl(measure) : null;
  observer?.observe(hud);
  observer?.observe(dock);
  measure();

  return { open, close:dismiss, measure, destroy:() => observer?.disconnect() };
});
