"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _reactNativeOrientationLocker = require("react-native-orientation-locker");
var _reactNativeVisionCamera = require("react-native-vision-camera");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const ScanCamera = _ref => {
  let {
    handleCodeScan,
    error,
    enableCameraOnError,
    torchActive
  } = _ref;
  const [orientation, setOrientation] = (0, _react.useState)(_reactNativeOrientationLocker.OrientationType.PORTRAIT);
  const [cameraActive, setCameraActive] = (0, _react.useState)(true);
  const orientationDegrees = {
    [_reactNativeOrientationLocker.OrientationType.PORTRAIT]: '0deg',
    [_reactNativeOrientationLocker.OrientationType['LANDSCAPE-LEFT']]: '270deg',
    [_reactNativeOrientationLocker.OrientationType['PORTRAIT-UPSIDEDOWN']]: '180deg',
    [_reactNativeOrientationLocker.OrientationType['LANDSCAPE-RIGHT']]: '90deg'
  };
  const invalidQrCodes = new Set();
  const device = (0, _reactNativeVisionCamera.useCameraDevice)('back');
  const screenAspectRatio = (0, _reactNative.useWindowDimensions)().scale;
  const format = (0, _reactNativeVisionCamera.useCameraFormat)(device, [{
    fps: 20
  }, {
    videoAspectRatio: screenAspectRatio
  }, {
    videoResolution: 'max'
  }, {
    photoAspectRatio: screenAspectRatio
  }, {
    photoResolution: 'max'
  }]);
  (0, _reactNativeOrientationLocker.useOrientationChange)(orientationType => {
    setOrientation(orientationType);
  });
  const onCodeScanned = (0, _react.useCallback)(codes => {
    const value = codes[0].value;
    if (!value || invalidQrCodes.has(value)) {
      return;
    }
    if ((error === null || error === void 0 ? void 0 : error.data) === value) {
      invalidQrCodes.add(value);
      if (enableCameraOnError) {
        return setCameraActive(true);
      }
    }
    if (cameraActive) {
      _reactNative.Vibration.vibrate();
      handleCodeScan(value);
      return setCameraActive(false);
    }
  }, [cameraActive]);
  (0, _react.useEffect)(() => {
    if (error !== null && error !== void 0 && error.data && enableCameraOnError) {
      setCameraActive(true);
    }
  }, [error]);
  const codeScanner = (0, _reactNativeVisionCamera.useCodeScanner)({
    codeTypes: ['qr'],
    onCodeScanned: onCodeScanned
  });
  return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: [_reactNative.StyleSheet.absoluteFill, {
      transform: [{
        rotate: orientationDegrees[orientation] ?? '0deg'
      }]
    }]
  }, device && /*#__PURE__*/_react.default.createElement(_reactNativeVisionCamera.Camera, {
    style: _reactNative.StyleSheet.absoluteFill,
    device: device,
    torch: torchActive ? 'on' : 'off',
    isActive: cameraActive,
    codeScanner: codeScanner,
    format: format
  }));
};
var _default = ScanCamera;
exports.default = _default;
//# sourceMappingURL=ScanCamera.js.map