import { NormalizedZoomValue, PointerCoords, Zoom } from "../types";

export const getNewZoom = (
  newZoomValue: NormalizedZoomValue,
  prevZoom: Zoom,
  canvasOffset: { left: number; top: number },
  zoomOnViewportPoint: PointerCoords = { x: 0, y: 0 },
): Zoom => {

  try {
      var fn_name = "getNewZoom"
      var fn_ret
      // /*
      window.glob_dev_fns && window.glob_dev_fns[fn_name] ? fn_ret = glob_dev_fns[fn_name]({that:this,kv:{

newZoomValue,
prevZoom,
canvasOffset,
zoomOnViewportPoint,
        getNormalizedZoom,
        // NormalizedZoomValue,
// PointerCoords,
Zoom,
      // */ 
      },args:[...arguments],fn_name,arg0:arguments}) : clog("NO EXIST....")
      if (fn_ret && fn_ret.ret_flag){ return fn_ret.ret }; 
  } catch (err) { clog(`react_fn fn_name:${fn_name} err:`,err) }

  return {
    value: newZoomValue,
    translation: {
      x:
        zoomOnViewportPoint.x -
        canvasOffset.left -
        (zoomOnViewportPoint.x - canvasOffset.left - prevZoom.translation.x) *
          (newZoomValue / prevZoom.value),
      y:
        zoomOnViewportPoint.y -
        canvasOffset.top -
        (zoomOnViewportPoint.y - canvasOffset.top - prevZoom.translation.y) *
          (newZoomValue / prevZoom.value),
    },
  };
};

export const getNormalizedZoom = (zoom: number): NormalizedZoomValue => {
  // const normalizedZoom = parseFloat(zoom.toFixed(2));
  var fix = 2
  if (window?.tmp_glob_dev && "nZoomFixed" in window?.tmp_glob_dev ){
    fix = window.tmp_glob_dev.nZoomFixed
  }
  if (window.tmp_hooks){
    tmp_hooks.gnz_fix = {fix}
  } else {
    // console.log("nfix",fix)
  }

  const normalizedZoom = parseFloat(zoom.toFixed(fix));
  const clampedZoom = Math.max(0.1, Math.min(normalizedZoom, 10));
  clog("xcal_getNormalizedZoom",{clampedZoom,normalizedZoom,zoom})
  return clampedZoom as NormalizedZoomValue;
};
// ri35dn6hkn9h6h1swv3umn