{"version":3,"file":"Webcam.cjs","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":"gMAgDA,IAAa,GAAU,CACrB,MACA,SACA,uBACA,uBACA,mBACA,mBACA,mBACA,cACA,aACA,WAAW,GACX,QAAQ,GACR,QAAQ,CAAC,EACT,WACA,mBACA,YACA,UACA,UACA,SACA,GAAG,KACc,CACjB,IAAM,GAAA,EAAA,EAAA,OAAA,CAAmD,IAAI,EACvD,EAAW,GAAO,EAkBxB,OAhBA,EAAA,UAAU,EAAU,CAClB,SACA,mBACA,YACA,UACA,UACA,SACA,mBACA,mBACA,uBACA,uBACA,cACA,aACA,OACF,CAAC,GAGC,EAAA,EAAA,KAAA,CAAA,EAAA,SAAA,CAAA,SAAA,EACE,EAAA,EAAA,IAAA,CAAC,QAAD,CACE,SAAA,GACA,YAAA,GACA,IAAK,EACL,MAAO,CACL,GAAG,EACH,GAAI,GAAY,CACd,UAAW,GAAG,EAAM,UAAY,GAAG,EAAM,UAAU,GAAK,GAAG,WAC7D,CACF,EACA,SAAU,GACH,QACP,iBAAmB,GAAU,CAC3B,IAAmB,CAAK,EACxB,EAAM,cAAc,KAAK,CAC3B,EACA,GAAI,CACL,CAAA,EACA,OAAO,GAAa,WACjB,EAAS,CACP,QAAS,EAAS,QAClB,UAAY,GACV,EAAS,SAAA,EAAA,EAAA,oBAAA,CACe,EAAS,QAAS,CAAE,GAAG,EAAS,UAAS,CAAC,EAC9D,IAAA,GACN,YAAc,GACZ,EAAS,SAAA,EAAA,EAAA,kBAAA,CACa,EAAS,QAAS,CAAE,GAAG,EAAS,UAAS,CAAC,EAC5D,IAAA,EACR,CAAC,EACD,CACJ,CAAA,CAAA,CAEN"}