/* ==========================================================
   LINKCARDS - SHARE COMPONENT
   Archivo: /m/Builder/elements/js/lc-share.js
========================================================== */

(function () {
  "use strict";

  function closeAllShareMenus(exceptComponent) {
    var components = document.querySelectorAll(".lc-share-component");

    components.forEach(function (component) {
      if (component !== exceptComponent) {
        component.classList.remove("lc-share-is-open");

        var trigger = component.querySelector(".lc-share-trigger");

        if (trigger) {
          trigger.setAttribute("aria-expanded", "false");
        }
      }
    });
  }

  function showMessage(component, text) {
    var message = component.querySelector(".lc-share-message");

    if (!message) {
      return;
    }

    message.textContent = text;
    message.classList.add("lc-share-message-show");

    clearTimeout(message.lcTimer);

    message.lcTimer = setTimeout(function () {
      message.classList.remove("lc-share-message-show");
    }, 2000);
  }

  function fallbackCopy(component, text) {
    var textarea = document.createElement("textarea");

    textarea.value = text;
    textarea.setAttribute("readonly", "");
    textarea.style.position = "fixed";
    textarea.style.top = "-9999px";

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

    try {
      document.execCommand("copy");
      showMessage(component, "Enlace copiado");
    } catch (error) {
      showMessage(component, "No se pudo copiar");
    }

    document.body.removeChild(textarea);
  }

  function copyLink(component) {
    var url = window.location.href;

    if (navigator.clipboard && window.isSecureContext) {
      navigator.clipboard.writeText(url)
        .then(function () {
          showMessage(component, "Enlace copiado");
        })
        .catch(function () {
          fallbackCopy(component, url);
        });

      return;
    }

    fallbackCopy(component, url);
  }

  function prepareLinks(component) {
    var pageUrl = window.location.href;
    var pageTitle = document.title || "Tarjeta Digital";
    var shareText = "Te comparto mi Tarjeta Digital";

    var encodedUrl = encodeURIComponent(pageUrl);
    var encodedTitle = encodeURIComponent(pageTitle);
    var encodedText = encodeURIComponent(
      shareText + " " + pageUrl
    );

    var whatsapp = component.querySelector(".lc-share-whatsapp");
    var facebook = component.querySelector(".lc-share-facebook");
    var telegram = component.querySelector(".lc-share-telegram");
    var linkedin = component.querySelector(".lc-share-linkedin");
    var pinterest = component.querySelector(".lc-share-pinterest");
    var email = component.querySelector(".lc-share-email");

    if (whatsapp) {
      whatsapp.href =
        "https://api.whatsapp.com/send?text=" + encodedText;
    }

    if (facebook) {
      facebook.href =
        "https://www.facebook.com/sharer/sharer.php?u=" +
        encodedUrl;
    }

    if (telegram) {
      telegram.href =
        "https://t.me/share/url?url=" +
        encodedUrl +
        "&text=" +
        encodeURIComponent(shareText);
    }

    if (linkedin) {
      linkedin.href =
        "https://www.linkedin.com/sharing/share-offsite/?url=" +
        encodedUrl;
    }

    if (pinterest) {
      pinterest.href =
        "https://pinterest.com/pin/create/button/?url=" +
        encodedUrl +
        "&description=" +
        encodedTitle;
    }

    if (email) {
      email.href =
        "mailto:?subject=" +
        encodedTitle +
        "&body=" +
        encodedText;
    }
 /* CERRAR DESPUÉS DE SELECCIONAR CUALQUIER OPCIÓN */
var shareOptions = component.querySelectorAll(".lc-share-option");

shareOptions.forEach(function (option) {
  option.addEventListener("click", function () {
    component.classList.remove("lc-share-is-open");

    if (trigger) {
      trigger.setAttribute("aria-expanded", "false");
    }
  });
});

/* CERRAR AL RETIRAR EL CURSOR */
component.addEventListener("mouseleave", function () {
  component.classList.remove("lc-share-is-open");

  if (trigger) {
    trigger.setAttribute("aria-expanded", "false");
  }
});

/* EVITAR QUE SE CIERRE AL PULSAR DENTRO */
component.addEventListener("click", function (event) {
  event.stopPropagation();
});
    }

    if (copyButton) {
      copyButton.addEventListener("click", function (event) {
        event.preventDefault();
        event.stopPropagation();

        copyLink(component);
        component.classList.remove("lc-share-is-open");

        if (trigger) {
          trigger.setAttribute("aria-expanded", "false");
        }
      });
    }

    if (nativeButton) {
      nativeButton.addEventListener("click", function (event) {
        event.preventDefault();
        event.stopPropagation();

        var shareData = {
          title: document.title || "Tarjeta Digital",
          text: "Te comparto mi Tarjeta Digital",
          url: window.location.href
        };

        if (navigator.share) {
          navigator.share(shareData).catch(function () {});
        } else {
          copyLink(component);
        }

        component.classList.remove("lc-share-is-open");

        if (trigger) {
          trigger.setAttribute("aria-expanded", "false");
        }
      });
    }

    component.addEventListener("click", function (event) {
      event.stopPropagation();
    });
  }

  function initializeAllShares() {
    var components = document.querySelectorAll(
      ".lc-share-component"
    );

    components.forEach(function (component) {
      initializeShare(component);
    });
  }

  document.addEventListener("click", function () {
    closeAllShareMenus();
  });

  document.addEventListener("keydown", function (event) {
    if (event.key === "Escape") {
      closeAllShareMenus();
    }
  });

  if (document.readyState === "loading") {
    document.addEventListener(
      "DOMContentLoaded",
      initializeAllShares
    );
  } else {
    initializeAllShares();
  }
})();