function backToTopWidget({ status, position }) {
  if (typeof status === 'undefined') {
    return;
  }

  const div = document.createElement('div');
  div.classList.add('scroll-to-top', position);
  div.style.zIndex = 100;

  const template = `
    <button type="button" class="btn-default btn-medium-square">
      <i class="fas fa-angle-up"></i>
    </button>
  `;

  div.innerHTML = template;

  const button = div.querySelector('button');
  button.addEventListener('click', () => {
    window.scroll({
      top: 0,
      behavior: 'smooth',
    });
  });

  document.body.append(div);

  const halfWindowHeight = window.innerHeight / 2;
  window.addEventListener('scroll', () => {
    if (window.scrollY > halfWindowHeight) {
      div.style.display = 'block';
    } else {
      div.style.display = 'none';
    }
  });
}

export default backToTopWidget;
