document.addEventListener('DOMContentLoaded', () => {
  const parentUntil = (node, cls) => {
    const nodep = node.parentElement
    return nodep.classList.contains(cls) ? nodep : parentUntil(nodep, cls)
  }   
  const autor = document.querySelector('[data-send-autoresponders]')
  const autorie = document.querySelector('[data-send-autoresponders-if-exists]')
  const autoriep = parentUntil(autorie, 'field')

  const check = () => {
    autorie.removeAttribute('disabled')
    if (Boolean(~~autorie.dataset.sendAutorespondersIfExists)) {
      autorie.setAttribute('checked', true)
    }
    autoriep.classList.remove('disabled')
  }

  const uncheck = () => {
    autorie.setAttribute('disabled', true)
    autorie.removeAttribute('checked')
    autoriep.classList.add('disabled')
  }

  autor.addEventListener('change', () => {
    return autor.checked ? check() : uncheck()
  })
})
