/*
 * Author: Michael Finkenberger
 * @since V2.5.24.0
 * Last change in plugin version: -
 * Date: 21.03.2026
 * Tested with the latest plugin version
*/



const FOODLE_DEFAULT_TOOLTIP = foodle_CopyI18n.foodle_defaultTooltip;
const FOODLE_COPIED_TOOLTIP = foodle_CopyI18n.foodle_copiedTooltip;
const FOODLE_ERROR_TOOLTIP = foodle_CopyI18n.foodle_errorTooltip;

function foodle_FallbackCopy(text) {
  const textarea = document.createElement('textarea');
  textarea.value = text;
  textarea.setAttribute('readonly', '');
  textarea.style.position = 'fixed';
  textarea.style.top = '-9999px';
  textarea.style.left = '-9999px';

  document.body.appendChild(textarea);
  textarea.focus();
  textarea.select();

  let ok = false;

  try {
    ok = document.execCommand('copy');
  } catch (err) {
    ok = false;
  }

  document.body.removeChild(textarea);
  return ok;
}

function foodle_SetTooltip(el, text) {
  el.dataset.tooltip = text;
}

function foodle_CopyInline(el) {
  const text = el.textContent.trim();

  function success() {
    clearTimeout(el._foodle_TooltipTimer);
    foodle_SetTooltip(el, FOODLE_COPIED_TOOLTIP);
    el.dataset.copied = 'true';
  }

  function error() {
    clearTimeout(el._foodle_TooltipTimer);
    foodle_SetTooltip(el, FOODLE_ERROR_TOOLTIP);
    el.dataset.copied = 'true';
  }

  if (navigator.clipboard && window.isSecureContext) {
    navigator.clipboard.writeText(text).then(success).catch(function () {
      foodle_FallbackCopy(text) ? success() : error();
    });
  } else {
    foodle_FallbackCopy(text) ? success() : error();
  }
}

function foodle_HideTooltip(el) {
  el.dataset.copied = 'false';

  clearTimeout(el._foodle_TooltipTimer);
  el._foodle_TooltipTimer = setTimeout(function () {
    foodle_SetTooltip(el, FOODLE_DEFAULT_TOOLTIP);
  }, 150);
}

document.addEventListener('DOMContentLoaded', function () {
  document.querySelectorAll('.foodle-copy-inline').forEach(function (el) {
    foodle_SetTooltip(el, FOODLE_DEFAULT_TOOLTIP);

    if (!el.dataset.copied) {
      el.dataset.copied = 'false';
    }

    if (!el.hasAttribute('role')) {
      el.setAttribute('role', 'button');
    }

    if (!el.hasAttribute('tabindex')) {
      el.setAttribute('tabindex', '0');
    }
  });
});

document.addEventListener('click', function (e) {
  const el = e.target.closest('.foodle-copy-inline');
  if (!el) {
    return;
  }

  foodle_CopyInline(el);
});

document.addEventListener('mouseout', function (e) {
  const el = e.target.closest('.foodle-copy-inline');
  if (!el) {
    return;
  }

  if (!el.contains(e.relatedTarget)) {
    foodle_HideTooltip(el);
  }
});

document.addEventListener('focusout', function (e) {
  const el = e.target.closest('.foodle-copy-inline');
  if (!el) {
    return;
  }

  foodle_HideTooltip(el);
});

document.addEventListener('keydown', function (e) {
  if (e.key !== 'Enter' && e.key !== ' ') {
    return;
  }

  const el = document.activeElement;
  if (!el || !el.classList.contains('foodle-copy-inline')) {
    return;
  }

  e.preventDefault();
  foodle_CopyInline(el);
});
