let mouseX = 0;
let mouseY = 0;
let mousePressed = false;

if (typeof window !== 'undefined') {
  const canvas = document.getElementById('canvas');

  window.addEventListener('mousemove', (e) => {
    if (canvas) {
      const rect = canvas.getBoundingClientRect();
      const scaleX = canvas.width / rect.width;
      const scaleY = canvas.height / rect.height;
      mouseX = (e.clientX - rect.left) * scaleX;
      mouseY = (e.clientY - rect.top) * scaleY;
    } else {
      mouseX = e.clientX;
      mouseY = e.clientY;
    }
  });

  window.addEventListener('mousedown', () => {
    mousePressed = true;
  });

  window.addEventListener('mouseup', () => {
    mousePressed = false;
  });
}

export function request_animation_frame(callback) {
  requestAnimationFrame(() => callback());
}

export function get_mouse_position() {
  return { x: mouseX, y: mouseY };
}

export function is_mouse_pressed() {
  return mousePressed;
}
