(function () {
  'use strict';

  function fallbackCopy(text) {
    var ta = document.createElement('textarea');
    ta.value = text || '';
    ta.setAttribute('readonly', '');
    ta.style.position = 'fixed';
    ta.style.left = '-9999px';
    document.body.appendChild(ta);
    ta.select();

    try {
      document.execCommand('copy');
    } catch (e) {}

    document.body.removeChild(ta);
  }

  function pulse(btn, idleLabel, copiedLabel) {
    btn.textContent = copiedLabel;
    btn.disabled = true;

    setTimeout(function () {
      btn.textContent = idleLabel;
      btn.disabled = false;
    }, 900);
  }

  document.addEventListener('click', function (ev) {
    var btn = ev.target && ev.target.closest ? ev.target.closest('.custausu-copy-btn') : null;
    if (!btn) return;

    ev.preventDefault();

    var text = btn.getAttribute('data-copy') || '';
    var idleLabel = btn.getAttribute('data-copy-label') || 'Copy';
    var copiedLabel = btn.getAttribute('data-copied-label') || 'Copied!';

    if (navigator.clipboard && navigator.clipboard.writeText) {
      navigator.clipboard.writeText(text)
        .then(function () {
          pulse(btn, idleLabel, copiedLabel);
        })
        .catch(function () {
          fallbackCopy(text);
          pulse(btn, idleLabel, copiedLabel);
        });
    } else {
      fallbackCopy(text);
      pulse(btn, idleLabel, copiedLabel);
    }
  });
})();