/* eslint-disable */
// Formspree wiring — one helper, six endpoints. Wired into every form
// across the site by passing form.currentTarget into submitFormspree().
//
// Endpoints map (Formspree IDs supplied by CBM):
const FORMSPREE = {
  newsletter:        'https://formspree.io/f/xrejwken',
  jobApply:          'https://formspree.io/f/xdabrzea',
  jobPost:           'https://formspree.io/f/mykoqjep',
  directorySubmit:   'https://formspree.io/f/mgodjyar',
  advertiseEnquiry:  'https://formspree.io/f/mkoylbje',
  courseCheckout:    'https://formspree.io/f/mvzljrkn',
};

// Submit a HTMLFormElement to a Formspree endpoint, optionally with extra
// metadata fields. Returns true on 2xx, false otherwise.
async function submitFormspree(endpoint, formEl, extra) {
  const data = new FormData(formEl);
  if (extra && typeof extra === 'object') {
    for (const k of Object.keys(extra)) {
      if (extra[k] != null) data.append(k, String(extra[k]));
    }
  }
  try {
    const r = await fetch(endpoint, {
      method: 'POST',
      headers: { Accept: 'application/json' },
      body: data,
    });
    return r.ok;
  } catch (err) {
    return false;
  }
}

Object.assign(window, { FORMSPREE, submitFormspree });
