"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _core = require("@aries-framework/core");
var _reactHooks = require("@aries-framework/react-hooks");
var _react = _interopRequireWildcard(require("react"));
var _reactI18next = require("react-i18next");
var _reactNative = require("react-native");
var _reactNativeGestureHandler = require("react-native-gesture-handler");
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _MaterialIcons = _interopRequireDefault(require("react-native-vector-icons/MaterialIcons"));
var _store = require("../../contexts/store");
var _theme = require("../../contexts/theme");
var _connections = require("../../hooks/connections");
var _navigators = require("../../types/navigators");
var _helpers = require("../../utils/helpers");
var _testable = require("../../utils/testable");
var _LoadingIndicator = _interopRequireDefault(require("../animated/LoadingIndicator"));
var _QRRenderer = _interopRequireDefault(require("./QRRenderer"));
var _QRScannerTorch = _interopRequireDefault(require("./QRScannerTorch"));
var _ScanCamera = _interopRequireDefault(require("./ScanCamera"));
var _ScanTab = _interopRequireDefault(require("./ScanTab"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 NewQRView = _ref => {
  let {
    defaultToConnect,
    handleCodeScan,
    error,
    enableCameraOnError,
    navigation
  } = _ref;
  const {
    width
  } = (0, _reactNative.useWindowDimensions)();
  const qrSize = width - 40;
  const [store] = (0, _store.useStore)();
  const [torchActive, setTorchActive] = (0, _react.useState)(false);
  const [firstTabActive, setFirstTabActive] = (0, _react.useState)(!defaultToConnect);
  const [invitation, setInvitation] = (0, _react.useState)(undefined);
  const [recordId, setRecordId] = (0, _react.useState)(undefined);
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const {
    ColorPallet,
    TextTheme,
    TabTheme
  } = (0, _theme.useTheme)();
  const {
    agent
  } = (0, _reactHooks.useAgent)();
  const styles = _reactNative.StyleSheet.create({
    container: {
      flex: 1,
      backgroundColor: ColorPallet.brand.secondaryBackground
    },
    camera: {
      flex: 1,
      justifyContent: 'center',
      alignItems: 'center'
    },
    cameraViewContainer: {
      alignItems: 'center',
      flex: 1,
      width: '100%'
    },
    viewFinder: {
      width: 250,
      height: 250,
      borderRadius: 24,
      borderWidth: 2,
      borderColor: ColorPallet.grayscale.white
    },
    viewFinderContainer: {
      flex: 1,
      width: '100%',
      justifyContent: 'center',
      alignItems: 'center'
    },
    errorContainer: {
      flexDirection: 'row',
      alignItems: 'center',
      marginTop: 20,
      paddingHorizontal: 20
    },
    icon: {
      color: ColorPallet.grayscale.white,
      padding: 4
    },
    tabContainer: {
      flexDirection: 'row',
      ...TabTheme.tabBarStyle
    },
    qrContainer: {
      marginTop: 10,
      flex: 1
    },
    walletName: {
      ...TextTheme.headingTwo,
      textAlign: 'center',
      marginBottom: 20
    },
    secondaryText: {
      ...TextTheme.normal,
      textAlign: 'center'
    },
    editButton: {
      ...TextTheme.headingTwo,
      marginBottom: 20,
      marginLeft: 10,
      color: ColorPallet.brand.primary
    }
  });
  const createInvitation = (0, _react.useCallback)(async () => {
    setInvitation(undefined);
    const result = await (0, _helpers.createConnectionInvitation)(agent);
    if (result) {
      setRecordId(result.record.id);
      setInvitation(result.invitationUrl);
    }
  }, []);
  const handleEdit = () => {
    navigation.navigate(_navigators.Screens.NameWallet);
  };
  (0, _react.useEffect)(() => {
    navigation.setOptions({
      title: firstTabActive ? 'Scan QR code' : 'My QR code'
    });
    if (!firstTabActive) {
      createInvitation();
    }
  }, [firstTabActive, store.preferences.walletName]);
  const record = (0, _connections.useConnectionByOutOfBandId)(recordId || '');
  (0, _react.useEffect)(() => {
    if ((record === null || record === void 0 ? void 0 : record.state) === _core.DidExchangeState.Completed) {
      var _navigation$getParent;
      (_navigation$getParent = navigation.getParent()) === null || _navigation$getParent === void 0 ? void 0 : _navigation$getParent.navigate(_navigators.Stacks.ConnectionStack, {
        screen: _navigators.Screens.Connection,
        params: {
          connectionId: record.id
        }
      });
    }
  }, [record]);
  return /*#__PURE__*/_react.default.createElement(_reactNativeSafeAreaContext.SafeAreaView, {
    edges: ['bottom', 'left', 'right'],
    style: styles.container
  }, firstTabActive ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ScanCamera.default, {
    handleCodeScan: handleCodeScan,
    enableCameraOnError: enableCameraOnError,
    error: error,
    torchActive: torchActive
  }), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.cameraViewContainer
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.errorContainer
  }, error ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_MaterialIcons.default, {
    style: styles.icon,
    name: "cancel",
    size: 30
  }), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    testID: (0, _testable.testIdWithKey)('ErrorMessage'),
    style: [TextTheme.normal, {
      color: ColorPallet.grayscale.white
    }]
  }, error.message)) : /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [TextTheme.normal, {
      color: ColorPallet.grayscale.white
    }]
  }, t('Scan.WillScanAutomatically'))), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.viewFinderContainer
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.viewFinder
  })), /*#__PURE__*/_react.default.createElement(_QRScannerTorch.default, {
    active: torchActive,
    onPress: () => setTorchActive(!torchActive)
  }))) : /*#__PURE__*/_react.default.createElement(_reactNative.ScrollView, null, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      alignItems: 'center'
    }
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.qrContainer
  }, !invitation && /*#__PURE__*/_react.default.createElement(_LoadingIndicator.default, null), invitation && /*#__PURE__*/_react.default.createElement(_QRRenderer.default, {
    value: invitation,
    size: qrSize
  })), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      paddingHorizontal: 20,
      flex: 1
    }
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      display: 'flex',
      flexDirection: 'row',
      alignItems: 'center',
      justifyContent: 'center'
    }
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    testID: (0, _testable.testIdWithKey)('WalletName'),
    style: [styles.walletName, {
      paddingHorizontal: 20
    }]
  }, store.preferences.walletName), /*#__PURE__*/_react.default.createElement(_reactNativeGestureHandler.TouchableOpacity, {
    accessibilityLabel: t('NameWallet.EditWalletName'),
    testID: (0, _testable.testIdWithKey)('EditWalletName'),
    onPress: handleEdit
  }, /*#__PURE__*/_react.default.createElement(_MaterialIcons.default, {
    style: styles.editButton,
    name: "edit",
    size: 24
  }))), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: styles.secondaryText
  }, t('Connection.ShareQR'))))), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.tabContainer
  }, /*#__PURE__*/_react.default.createElement(_ScanTab.default, {
    title: t('Scan.ScanQRCode'),
    iconName: 'crop-free',
    onPress: () => setFirstTabActive(true),
    active: firstTabActive
  }), /*#__PURE__*/_react.default.createElement(_ScanTab.default, {
    title: t('Scan.MyQRCode'),
    iconName: 'qr-code',
    onPress: () => setFirstTabActive(false),
    active: !firstTabActive
  })));
};
var _default = NewQRView;
exports.default = _default;
//# sourceMappingURL=NewQRView.js.map