(function () {
  'use strict';

  if (window.custausuReviewNoticeReady) {
    return;
  }

  window.custausuReviewNoticeReady = true;

  function saveSnooze(notice) {
    if (!notice || notice.getAttribute('data-custausu-review-snooze-sent') === '1') {
      return;
    }

    var endpoint = notice.getAttribute('data-custausu-review-action-url') || '';
    var nonce = notice.getAttribute('data-custausu-review-nonce') || '';

    if (!endpoint || !nonce || !window.fetch || !window.FormData) {
      return;
    }

    notice.setAttribute('data-custausu-review-snooze-sent', '1');

    var data = new FormData();
    data.append('action', 'custausu_review_notice_action');
    data.append('review_action', 'snooze');
    data.append('_wpnonce', nonce);
    data.append('custausu_review_ajax', '1');

    try {
      fetch(endpoint, {
        method: 'POST',
        credentials: 'same-origin',
        keepalive: true,
        body: data
      }).catch(function () {});
    } catch (e) {}
  }

  document.addEventListener('click', function (event) {
    var close = event.target && event.target.closest
      ? event.target.closest('[data-custausu-review-notice="1"] .notice-dismiss')
      : null;

    if (!close) {
      return;
    }

    saveSnooze(close.closest('[data-custausu-review-notice="1"]'));
  }, true);
})();
