const toastMessage = (text, action = "success") => {
  if (typeof toastr !== "undefined" && toastr) {
    toastr.options = {
      closeButton: true,
      debug: false,
      newestOnTop: false,
      progressBar: false,
      positionClass: "toast-bottom-right",
      preventDuplicates: false,
      showDuration: "500",
      hideDuration: "100",
      timeOut: "1000",
      extendedTimeOut: "1000",
      showEasing: "swing",
      hideEasing: "linear",
      showMethod: "fadeIn",
      hideMethod: "fadeOut",
    };

    switch (action) {
      case "success":
        toastr.success(text);
        break;
      case "warning":
        toastr.warning(text);
        break;
      case "error":
        toastr.error(text);
        break;
      default:
        toastr.success(text);
    }
  } else {
    console.error("toastr not found");
  }
};
(function ($) {
  "use strict";
  $(function () {
    var ncpc_gotopro_notice = "ncpc_gotopro_notice"; // Clé pour le stockage local

    if (localStorage.getItem(ncpc_gotopro_notice) === "true") {
      $("#ncpc_gotopro_notice").hide(); // Cacher la notification si déjà fermée
    }
    const send_manufactrer_email = function (product_id, data) {
      $.post(
        ajaxurl,
        {
          action: "ncpc_send_manufactrer_email",
          product_id: product_id,
          order_id: data.order_id,
          recaps: data.recaps,
        },
        function (data) {
          if (data.success) {
            toastMessage(data.success);
          } else {
            toastMessage(data.error, "error");
          }
        }
      );
    };

    $(document).on("click", ".ncpc-send-email", function (e) {
      e.preventDefault();
      var product_id = $(this).data("product-id");
      var data = $(this).data("order");
      send_manufactrer_email(product_id, data);
    });

    $(".single_variation_wrap").on(
      "show_variation",
      function (event, variation) {
        // Fired when the user selects all the required dropdowns / attributes
        // and a final variation is selected / shown
        var variation_id = $("input[name='variation_id']").val();

        if (variation_id) {
          $(".ncpc-buttons-wrap-variation").hide();
          $(
            ".ncpc-buttons-wrap-variation[data-id='" + variation_id + "']"
          ).show();

          if (typeof hide_cart_button !== "undefined") {
            if (
              $(".ncpc-buttons-wrap-variation[data-id='" + variation_id + "']")
                .length > 0 &&
              hide_cart_button === true
            ) {
              $(".ncpc-buttons-wrap-variation")
                .parent()
                .find(".add_to_cart_button")
                .hide();
              $(".ncpc-buttons-wrap-variation")
                .parent()
                .find(".single_add_to_cart_button")
                .hide();
            } else {
              $(".ncpc-buttons-wrap-variation")
                .parent()
                .find(".add_to_cart_button")
                .show();
              $(".ncpc-buttons-wrap-variation")
                .parent()
                .find(".single_add_to_cart_button")
                .show();
            }
          }
          $(
            ".single-product .summary.entry-summary > .ncpc-buttons-wrap-variation"
          ).hide();
        }
      }
    );

    $(".single_variation_wrap").on(
      "hide_variation",
      function (event, variation) {
        $(".ncpc-buttons-wrap-variation").hide();
      }
    );

    $(document).on("click", ".ncpc_admin_download_image", function (e) {
      e.preventDefault();
      var imgageData = $(this).data("img-url");
      if (imgageData.search("data") == 1) {
        var preview_img = dataToBlob(imgageData).blob;
      } else {
        var preview_img = imgageData;
      }
      var downloadLink = document.createElement("a");
      downloadLink.href = preview_img;
      const lowest = 100;
      const highest = 9999999;

      let randomNumber = parseInt(Math.random() * (highest - lowest) + lowest);
      downloadLink.download = "ncpc_preview_" + randomNumber + ".png";
      document.body.appendChild(downloadLink);
      downloadLink.click();
      document.body.removeChild(downloadLink);
    });
    $(document).on("click", ".ncpc_admin_download_svg", function (e) {
      e.preventDefault();
      var svg = $(this).data("svg-url");
      const link = document.createElement("a");
      const lowest = 100;
      const highest = 9999999;

      let randomNumber = parseInt(Math.random() * (highest - lowest) + lowest);
      link.download = "ncpc_custom_text_" + randomNumber + ".svg"; // nom du fichier

      document.body.appendChild(link);
      // générer l'URL pour le blob
      link.href = svg;

      // déclencher le téléchargement
      link.click();
      document.body.removeChild(link);
    });

    function dataToBlob(dataURI) {
      var get_URL = function () {
        return window.URL || window.webkitURL || window;
      };

      var byteString = atob(dataURI.split(",")[1]),
        mimeString = dataURI.split(",")[0].split(":")[1].split(";")[0],
        arrayBuffer = new ArrayBuffer(byteString.length),
        _ia = new Uint8Array(arrayBuffer);

      for (var i = 0; i < byteString.length; i++) {
        _ia[i] = byteString.charCodeAt(i);
      }

      var dataView = new DataView(arrayBuffer);
      var blob = new Blob([dataView], { type: mimeString });
      return { blob: get_URL().createObjectURL(blob), data: dataURI };
    }

    var cartForm = $(".woocommerce-cart-form");
    cartForm.contents().each(function () {
      // Vérifier si le nœud est un nœud de texte
      if (this.nodeType === Node.TEXT_NODE) {
        // Modifier le contenu du nœud de texte
        this.textContent = "";
      }
    });
  });
})(jQuery);
