/** Export-flow demo — wire the format select to the submit button label.
 *
 * The pattern's composition rule says the submit-button verb mirrors the
 * format choice ("Download CSV" / "Download JSON" / "Download PDF"). The
 * demo backs that rule by rebinding the button text on format change. */

const FORMAT_LABELS = {
  csv:      'CSV',
  json:     'JSON',
  xlsx:     'XLSX',
  pdf:      'PDF',
  'zip-pdf':'PDF (zip)',
  ndjson:   'NDJSON',
  parquet:  'Parquet',
};

function wire(root, { selectId, submitId, verb = 'Download' }) {
  const select = root.querySelector('#' + selectId);
  const submit = root.querySelector('#' + submitId);
  if (!select || !submit) return;
  const update = () => {
    const fmt = (select.value || '').toLowerCase();
    const label = FORMAT_LABELS[fmt] || fmt.toUpperCase();
    submit.setAttribute('text', `${verb} ${label}`);
  };
  update();
  select.addEventListener('change', update);
}

export default function setup(router) {
  const root = router || document;
  wire(root, { selectId: 'export-sync-format',     submitId: 'export-sync-submit' });
  wire(root, { selectId: 'export-delivery-format', submitId: 'export-delivery-submit' });
  // Async variant uses "Request export" — no format suffix per the rule
  // ("Request" verb signals the async job; the user gets a download link
  // by email, not an immediate file).
}
