// Get the main container element for the rent option page
const irentOptionPage = document.getElementById('Irent_Option_Page');

if (irentOptionPage) {
  // Add click event listeners to icons for copying text
  ['Script', 'Quicksearch'].forEach((el) => {
    const icon = irentOptionPage.querySelector(`#Copy_${el}`); // Get the copy icon by ID
    if (icon) {
        // Add click event to call the copyToClipboard function
        icon.addEventListener('click', () => copyToClipboard(`#Input_${el}`, icon));
    }
  });

  // Function to copy text from an input to the clipboard
  const copyToClipboard = async (inputId, icon) => {
    const input = irentOptionPage.querySelector(inputId); // Get the input element by ID
    if (!input) return; // Exit if input is not found

    // Set the input value to the text you want to copy (if it’s not already set)
    const text = input.value; // Get the text content from the <input> element

    try {
        // Use modern Clipboard API if available
        if (navigator.clipboard && navigator.clipboard.writeText) {
            await navigator.clipboard.writeText(text);
        } else {
            // Fallback method
            input.select(); // Select the input's text
            document.execCommand('copy'); // Copy the selected text
            input.blur();
            window.getSelection().removeAllRanges();
        }
        // Show a tooltip with a message after successful copy
        showTooltip(icon, icon.getAttribute('data-tooltip'));

    } catch (err) {
        console.error('Error copying text: ', err);
    }
  };
 
  // Function to display a tooltip near the specified icon
  const showTooltip = (icon, message) => {
    // Create a new tooltip element
    const tooltip = document.createElement('div');
    tooltip.className = 'Irent_Tooltip'; // Set the tooltip class
    tooltip.innerText = message; // Set the tooltip message
    document.body.appendChild(tooltip); // Append the tooltip to the body

    // Get the bounding rectangle of the icon to position the tooltip
    const rect = icon.getBoundingClientRect();
    tooltip.style.left = `${(rect.left + window.scrollX + rect.width / 2) + 20}px`; // Position the tooltip horizontally
    tooltip.style.top = `${(rect.top + window.scrollY - tooltip.offsetHeight - 5) + 28}px`; // Position the tooltip vertically

    tooltip.style.opacity = 1; // Set initial opacity for fade-in effect

    // Set a timeout to fade out the tooltip and remove it after a delay
    setTimeout(() => {
        tooltip.style.opacity = 0; // Fade out the tooltip
        setTimeout(() => tooltip.remove(), 300); // Remove the tooltip after fade out
    }, 1000);
  };
}
