"use strict";

import { forwardRef, useImperativeHandle, useRef } from 'react';
import { findNodeHandle } from 'react-native';
import CanvasNativeComponent from './CanvasViewNativeComponent';
import CanvasModule from "./NativeCanvasModule.js";
import { jsx as _jsx } from "react/jsx-runtime";
// Install the JSI API once per JS runtime (idempotent native side too).
let installed = false;
function ensureInstalled() {
  if (installed) return true;
  installed = CanvasModule.install();
  return installed;
}

// Canvas-local touch coordinate (logical px). Hit-testing is the user's job
// (DESIGN §3) — the canvas doesn't know what shapes are drawn in it.

// Our onTouchStart/Move/End take canvas-local {x, y}, replacing the View
// responder props of the same names (hence the Omit).

export const Canvas = /*#__PURE__*/forwardRef(function CanvasComponent({
  onPress,
  onTouchStart,
  onTouchMove,
  onTouchEnd,
  ...rest
}, ref) {
  const nativeRef = useRef(null);
  useImperativeHandle(ref, () => ({
    getContext() {
      if (!ensureInstalled() || !globalThis.__rncanvasGetContext) return null;
      const tag = findNodeHandle(nativeRef.current);
      if (tag == null) return null;
      return globalThis.__rncanvasGetContext(tag);
    },
    getTag() {
      if (!ensureInstalled()) return null;
      return findNodeHandle(nativeRef.current) ?? null;
    }
  }), []);
  return /*#__PURE__*/_jsx(CanvasNativeComponent, {
    ref: nativeRef,
    ...rest,
    onCanvasPress: onPress ? e => onPress({
      x: e.nativeEvent.x,
      y: e.nativeEvent.y
    }) : undefined,
    onCanvasTouchStart: onTouchStart ? e => onTouchStart({
      x: e.nativeEvent.x,
      y: e.nativeEvent.y
    }) : undefined,
    onCanvasTouchMove: onTouchMove ? e => onTouchMove({
      x: e.nativeEvent.x,
      y: e.nativeEvent.y
    }) : undefined,
    onCanvasTouchEnd: onTouchEnd ? e => onTouchEnd({
      x: e.nativeEvent.x,
      y: e.nativeEvent.y
    }) : undefined
  });
});
//# sourceMappingURL=Canvas.js.map