<script setup>
const props = defineProps({
  source: String,
  width: Number,
  height: Number,
  alt: String
})

const magnifyImg = ref(null)
const magnifyGlass = ref(null)

function magnify(zoom = 6) {
  if(magnifyImg.value && magnifyGlass.value) {
    var w, h, bw;
    magnifyGlass.value.style.visibility = 'hidden'
    //img = magnifyImg.value;
    /*create magnifier glass:*/
    //glass = magnifyGlass.value;
    //glass = document.createElement("DIV");
    //glass.setAttribute("class", "img-magnifier-glass");
    /*insert magnifier glass:*/
    //img.parentElement.insertBefore(glass, img);
    /*set background properties for the magnifier glass:*/
    magnifyGlass.value.style.backgroundImage = "url('" + props.source + "')";
    magnifyGlass.value.style.backgroundRepeat = "no-repeat";
    magnifyGlass.value.style.backgroundSize = (magnifyImg.value.width * zoom) + "px " + (magnifyImg.value.height * zoom) + "px";
    bw = 3;
    w = magnifyGlass.value.offsetWidth / 2;
    h = magnifyGlass.value.offsetHeight / 2;
    /*execute a function when someone moves the magnifier glass over the image:*/
    magnifyGlass.value.addEventListener("mousemove", moveMagnifier);
    magnifyImg.value.addEventListener("mousemove", moveMagnifier);
    /*and also for touch screens:*/
    magnifyGlass.value.addEventListener("touchmove", moveMagnifier);
    magnifyImg.value.addEventListener("touchmove", moveMagnifier);
    function moveMagnifier(e) {
      var pos, x, y;
      magnifyGlass.value.style.visibility = 'visible'
      /*prevent any other actions that may occur when moving over the image*/
      e.preventDefault();
      /*get the cursor's x and y positions:*/
      pos = getCursorPos(e);
      x = pos.x;
      y = pos.y;
      /*prevent the magnifier glass from being positioned outside the image:*/
      if (x > magnifyImg.value.width - (w / zoom)) {x = magnifyImg.value.width - (w / zoom);}
      if (x < w / zoom) {x = w / zoom;}
      if (y > magnifyImg.value.height - (h / zoom)) {y = magnifyImg.value.height - (h / zoom);}
      if (y < h / zoom) {y = h / zoom;}
      /*set the position of the magnifier glass:*/
      magnifyGlass.value.style.left = (x - w) + "px";
      magnifyGlass.value.style.top = (y - h) + -80 + "px";
      /*display what the magnifier glass "sees":*/
      magnifyGlass.value.style.backgroundPosition = "-" + ((x * zoom) - w + bw) + "px -" + ((y * zoom) - h + bw) + "px";
    }
    function getCursorPos(e) {
      var a, x = 0, y = 0;
      e = e || window.event;
      /*get the x and y positions of the image:*/
      a = magnifyImg.value.getBoundingClientRect();
      /*calculate the cursor's x and y coordinates, relative to the image:*/
      x = e.pageX - a.left;
      y = e.pageY - a.top;
      /*consider any page scrolling:*/
      x = x - window.pageXOffset;
      y = y - window.pageYOffset;
      return {x : x, y : y};
    }
  }
}
onMounted(() => magnify(6))
const hideGlass = () => {
  if(magnifyGlass.value) {
    magnifyGlass.value.style.visibility = 'hidden'
  }
}
</script>

<template>
  <div class="img-magnifier-container" @mouseleave="hideGlass">
    <div ref="magnifyGlass" class="img-magnifier-glass"></div>
    <img ref="magnifyImg" :src="source" :width="width" :height="height" :alt="alt">
  </div>
</template>

<style scoped>
.img-magnifier-container {
  position: relative;
  border: 1px solid #e2e2e2;
}

.img-magnifier-glass {
  position: absolute;
  border: 1px solid #000;
  /*border-radius: 50%;*/
  cursor: none;
  pointer-events: none;
  /*Set the size of the magnifier glass:*/
  width: 100px;
  height: 100px;
  z-index: 3000;
}
</style>