document.addEventListener("DOMContentLoaded", function () {
  var ftmCustomProfilePictureButton = document.getElementById(
    "ftm_custom_profile_picture_button"
  );
  var ftmCustomProfilePictureUrl = document.getElementById(
    "ftm_custom_profile_picture_url"
  );
  var ftmCustomProfilePicturePreview = document.getElementById(
    "ftm_custom_profile_picture_preview"
  );

  ftmCustomProfilePictureButton.addEventListener("click", function (e) {
    e.preventDefault();

    var image = wp
      .media({
        title: "Upload Image",
        multiple: false,
      })
      .open()
      .on("select", function () {
        var uploaded_image = image.state().get("selection").first();
        var image_url = uploaded_image.toJSON().url;
        console.log(image_url);
        ftmCustomProfilePictureUrl.value = image_url;
        ftmCustomProfilePicturePreview.src = image_url;
      });
  });
});
