{"version":3,"file":"Webcam.mjs","names":[],"sources":["../../../../src/components/Webcam/Webcam.tsx"],"sourcesContent":["import type {\n  GetVideoFrameCanvasOptions,\n  GetWebcamSnapshotOptions,\n  MediaTrackConstraintsOptions\n} from '@webcam/core';\nimport type { ComponentProps, ReactNode, RefObject } from 'react';\n\nimport { getVideoFrameCanvas, getWebcamSnapshot } from '@webcam/core';\nimport React, { useRef } from 'react';\n\nimport { useWebcam } from './hooks';\n\nexport type WebcamRenderProps = (options: {\n  getCanvas: (options?: GetVideoFrameCanvasOptions) => HTMLCanvasElement | undefined;\n  getSnapshot: (options?: GetWebcamSnapshotOptions) => string | undefined;\n  element: HTMLVideoElement | null;\n}) => ReactNode;\n\ninterface BaseWebcamProps\n  extends Omit<ComponentProps<'video'>, 'children' | 'onError'>, MediaTrackConstraintsOptions {\n  audioConstraints?: MediaTrackConstraints;\n  children?: ReactNode | WebcamRenderProps;\n  mirrored?: boolean;\n  ref?: RefObject<HTMLVideoElement>;\n  requestTimeLimit?: number;\n  videoConstraints?: MediaTrackConstraints;\n  onError?: (error: Error) => void;\n  onRequest?: () => void;\n  onStart?: (stream: MediaStream) => void;\n  onStop?: (stream?: MediaStream) => void;\n}\n\ninterface WebcamPropsWithInternalStream extends BaseWebcamProps {\n  stream?: undefined;\n}\n\ninterface WebcamPropsWithExternalStream extends BaseWebcamProps {\n  stream?: MediaStream;\n}\n\nexport type WebcamProps = WebcamPropsWithExternalStream | WebcamPropsWithInternalStream;\n\n/**\n * Renders the Webcam component and handles the requesting and displaying of the media stream\n *\n * @param {WebcamProps} props - The props for the Webcam component\n * @return {ReactElement} The rendered Webcam component\n */\nexport const Webcam = ({\n  ref,\n  stream,\n  cameraResolutionMode,\n  cameraResolutionType,\n  videoConstraints,\n  audioConstraints,\n  requestTimeLimit,\n  frontCamera,\n  mainCamera,\n  mirrored = true,\n  muted = true,\n  style = {},\n  children,\n  onLoadedMetadata,\n  onRequest,\n  onError,\n  onStart,\n  onStop,\n  ...props\n}: WebcamProps) => {\n  const internalVideoRef = useRef<HTMLVideoElement | null>(null);\n  const videoRef = ref ?? internalVideoRef;\n\n  useWebcam(videoRef, {\n    stream,\n    requestTimeLimit,\n    onRequest,\n    onError,\n    onStart,\n    onStop,\n    videoConstraints,\n    audioConstraints,\n    cameraResolutionMode,\n    cameraResolutionType,\n    frontCamera,\n    mainCamera,\n    muted\n  });\n\n  return (\n    <>\n      <video\n        autoPlay\n        playsInline\n        ref={videoRef}\n        style={{\n          ...style,\n          ...(mirrored && {\n            transform: `${style.transform ? `${style.transform} ` : ''}scaleX(-1)`\n          })\n        }}\n        controls={false}\n        muted={muted}\n        onLoadedMetadata={(event) => {\n          onLoadedMetadata?.(event);\n          event.currentTarget.play();\n        }}\n        {...props}\n      />\n      {typeof children === 'function'\n        ? children({\n            element: videoRef.current,\n            getCanvas: (options?: GetVideoFrameCanvasOptions) =>\n              videoRef.current\n                ? getVideoFrameCanvas(videoRef.current, { ...options, mirrored })\n                : undefined,\n            getSnapshot: (options?: GetWebcamSnapshotOptions) =>\n              videoRef.current\n                ? getWebcamSnapshot(videoRef.current, { ...options, mirrored })\n                : undefined\n          })\n        : children}\n    </>\n  );\n};\n"],"mappings":";;;;;AAgDA,IAAa,KAAU,EACrB,QACA,WACA,yBACA,yBACA,qBACA,qBACA,qBACA,gBACA,eACA,cAAW,IACX,WAAQ,IACR,WAAQ,CAAC,GACT,aACA,qBACA,cACA,YACA,YACA,WACA,GAAG,QACc;CACjB,IAAM,IAAmB,EAAgC,IAAI,GACvD,IAAW,KAAO;CAkBxB,OAhBA,EAAU,GAAU;EAClB;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CAAC,GAGC,kBAAA,GAAA,EAAA,UAAA,CACE,kBAAC,SAAD;EACE,UAAA;EACA,aAAA;EACA,KAAK;EACL,OAAO;GACL,GAAG;GACH,GAAI,KAAY,EACd,WAAW,GAAG,EAAM,YAAY,GAAG,EAAM,UAAU,KAAK,GAAG,YAC7D;EACF;EACA,UAAU;EACH;EACP,mBAAmB,MAAU;GAE3B,AADA,IAAmB,CAAK,GACxB,EAAM,cAAc,KAAK;EAC3B;EACA,GAAI;CACL,CAAA,GACA,OAAO,KAAa,aACjB,EAAS;EACP,SAAS,EAAS;EAClB,YAAY,MACV,EAAS,UACL,EAAoB,EAAS,SAAS;GAAE,GAAG;GAAS;EAAS,CAAC,IAC9D,KAAA;EACN,cAAc,MACZ,EAAS,UACL,EAAkB,EAAS,SAAS;GAAE,GAAG;GAAS;EAAS,CAAC,IAC5D,KAAA;CACR,CAAC,IACD,CACJ,EAAA,CAAA;AAEN"}