"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }


var _chunkBGJWTMDWcjs = require('./chunk-BGJWTMDW.cjs');


var _chunkZ6EUSUADcjs = require('./chunk-Z6EUSUAD.cjs');

// src/components/QRReader/hooks.ts
var _react = require('react');
var useQRReader = ({
  scanDelay: delayBetweenScanAttempts = 500,
  constraints: video,
  onResult,
  videoId
}) => {
  const mountedRef = _react.useRef.call(void 0, true);
  _react.useEffect.call(void 0, () => {
    const readQRCode = () => _chunkZ6EUSUADcjs.__async.call(void 0, void 0, null, function* () {
      const { BrowserQRCodeReader } = yield Promise.resolve().then(() => _interopRequireWildcard(require(
        /* webpackChunkName: "zxing" */
        "@zxing/browser"
      )));
      const codeReader = new BrowserQRCodeReader(void 0, {
        delayBetweenScanAttempts
      });
      if (!_chunkBGJWTMDWcjs.isMediaDevicesSupported.call(void 0, ) && onResult) {
        const message = 'MediaDevices API has no support for your browser. You can fix this by running "npm i webrtc-adapter"';
        onResult(null, new Error(message), codeReader);
      }
      if (video && _chunkBGJWTMDWcjs.isValidType.call(void 0, video, "constraints", "object")) {
        codeReader.decodeFromConstraints(
          { video },
          videoId,
          (result, error, controls) => {
            if (mountedRef.current === false) {
              controls.stop();
              return;
            }
            if (typeof result !== "undefined" && onResult) {
              controls.stop();
              onResult(result, error, codeReader);
            }
          }
        ).catch((error) => {
          if (onResult) {
            onResult(null, error, codeReader);
          }
        });
      }
    });
    mountedRef.current = true;
    void readQRCode();
    return () => {
      mountedRef.current = false;
    };
  }, [delayBetweenScanAttempts, onResult, video, videoId]);
};



exports.useQRReader = useQRReader;
