"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _reactHooks = require("@credo-ts/react-hooks");
var _reactI18next = require("react-i18next");
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _navigators = require("../../../types/navigators");
var _theme = require("../../../theme");
var _displayProof = require("../displayProof");
var _testable = require("../../../utils/testable");
var _utils = require("../utils/utils");
var _CredentialRowCard = require("../components/CredentialRowCard");
var _CommonRemoveModal = _interopRequireDefault(require("../../../components/modals/CommonRemoveModal"));
var _remove = require("../../../types/remove");
var _Button = _interopRequireWildcard(require("../../../components/buttons/Button"));
var _theme2 = require("../../../contexts/theme");
var _error = require("../../../types/error");
var _constants = require("../../../constants");
var _resolverProof = require("../resolverProof");
var _ProofRequestAccept = _interopRequireDefault(require("../../../screens/ProofRequestAccept"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const styles = _reactNative.StyleSheet.create({
  pageContent: {
    flexGrow: 1,
    justifyContent: 'space-between',
    padding: 10
  },
  credentialsList: {
    marginTop: 20,
    justifyContent: 'space-between'
  },
  headerTextContainer: {
    paddingVertical: 16
  },
  headerText: {
    ..._theme.ListItems.recordAttributeText,
    flexShrink: 1
  },
  footerButton: {
    paddingTop: 10
  }
});
const OpenIDProofPresentation = ({
  navigation,
  route: {
    params: {
      credential
    }
  }
}) => {
  //   console.log('DUMp Record:', JSON.stringify(credential))

  const [declineModalVisible, setDeclineModalVisible] = (0, _react.useState)(false);
  const [buttonsVisible, setButtonsVisible] = (0, _react.useState)(true);
  const [acceptModalVisible, setAcceptModalVisible] = (0, _react.useState)(false);
  const {
    ColorPallet
  } = (0, _theme2.useTheme)();
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const {
    agent
  } = (0, _reactHooks.useAgent)();
  const toggleDeclineModalVisible = () => setDeclineModalVisible(!declineModalVisible);
  const submission = (0, _react.useMemo)(() => credential && credential.credentialsForRequest ? (0, _displayProof.formatDifPexCredentialsForRequest)(credential.credentialsForRequest) : undefined, [credential]);
  const selectedCredentials = (0, _react.useMemo)(() => submission === null || submission === void 0 ? void 0 : submission.entries.reduce((acc, entry) => {
    if (entry.isSatisfied) {
      //TODO: Support multiplae credentials
      return {
        ...acc,
        [entry.inputDescriptorId]: entry.credentials[0].id
      };
    }
    return acc;
  }, {}), [submission]);
  const {
    verifierName
  } = (0, _react.useMemo)(() => {
    return {
      verifierName: credential === null || credential === void 0 ? void 0 : credential.verifierHostName
    };
  }, [credential]);
  const handleAcceptTouched = async () => {
    try {
      if (!agent || !credential.credentialsForRequest || !selectedCredentials) {
        return;
      }
      await (0, _resolverProof.shareProof)({
        agent,
        authorizationRequest: credential.authorizationRequest,
        credentialsForRequest: credential.credentialsForRequest,
        selectedCredentials
      });
      setAcceptModalVisible(true);
    } catch (err) {
      setButtonsVisible(true);
      const error = new _error.BifoldError(t('Error.Title1027'), t('Error.Message1027'), (err === null || err === void 0 ? void 0 : err.message) ?? err, 1027);
      _reactNative.DeviceEventEmitter.emit(_constants.EventTypes.ERROR_ADDED, error);
    }
  };
  const handleDeclineTouched = async () => {
    var _navigation$getParent;
    toggleDeclineModalVisible();
    (_navigation$getParent = navigation.getParent()) === null || _navigation$getParent === void 0 || _navigation$getParent.navigate(_navigators.TabStacks.HomeStack, {
      screen: _navigators.Screens.Home
    });
  };
  const renderHeader = () => {
    return /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.headerTextContainer
    }, /*#__PURE__*/React.createElement(_reactNative.Text, {
      style: styles.headerText,
      testID: (0, _testable.testIdWithKey)('HeaderText')
    }, /*#__PURE__*/React.createElement(_reactNative.Text, {
      style: _theme.TextTheme.title
    }, "You have received an information request", verifierName ? ` from ${verifierName}` : '', ".")));
  };
  const renderBody = () => {
    if (!submission) return null;
    return /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.credentialsList
    }, submission.entries.map((s, i) => {
      //TODO: Support multiplae credentials
      const selectedCredential = s.credentials[0];
      return /*#__PURE__*/React.createElement(_reactNative.View, {
        key: i
      }, /*#__PURE__*/React.createElement(_CredentialRowCard.OpenIDCredentialRowCard, {
        name: s.name,
        issuer: verifierName,
        onPress: () => {}
      }), s.isSatisfied && selectedCredential !== null && selectedCredential !== void 0 && selectedCredential.requestedAttributes ? /*#__PURE__*/React.createElement(_reactNative.View, {
        style: {
          marginTop: 16,
          gap: 8
        }
      }, s.description && /*#__PURE__*/React.createElement(_reactNative.Text, {
        style: _theme.TextTheme.labelSubtitle
      }, s.description), /*#__PURE__*/React.createElement(_reactNative.View, {
        style: {
          flexDirection: 'row',
          flexWrap: 'wrap'
        }
      }, selectedCredential.requestedAttributes.map(a => /*#__PURE__*/React.createElement(_reactNative.View, {
        key: a,
        style: {
          flexBasis: '50%'
        }
      }, /*#__PURE__*/React.createElement(_reactNative.Text, {
        style: _theme.TextTheme.normal
      }, "\u2022 ", (0, _utils.sanitizeString)(a)))))) : /*#__PURE__*/React.createElement(_reactNative.Text, {
        style: _theme.TextTheme.title
      }, "This credential is not present in your wallet."));
    }));
  };
  const footerButton = (title, buttonPress, buttonType, testID, accessibilityLabel) => {
    return /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.footerButton
    }, /*#__PURE__*/React.createElement(_Button.default, {
      title: title,
      accessibilityLabel: accessibilityLabel,
      testID: testID,
      buttonType: buttonType,
      onPress: buttonPress,
      disabled: !buttonsVisible
    }));
  };
  const footer = () => {
    return /*#__PURE__*/React.createElement(_reactNative.View, {
      style: {
        paddingHorizontal: 25,
        paddingVertical: 16,
        paddingBottom: 26,
        backgroundColor: ColorPallet.brand.secondaryBackground
      }
    }, footerButton(t('Global.Accept'), handleAcceptTouched, _Button.ButtonType.Primary, (0, _testable.testIdWithKey)('AcceptCredentialOffer'), t('Global.Accept')), footerButton(t('Global.Decline'), toggleDeclineModalVisible, _Button.ButtonType.Secondary, (0, _testable.testIdWithKey)('DeclineCredentialOffer'), t('Global.Decline')));
  };
  return /*#__PURE__*/React.createElement(_reactNativeSafeAreaContext.SafeAreaView, {
    style: {
      flexGrow: 1
    },
    edges: ['bottom', 'left', 'right']
  }, /*#__PURE__*/React.createElement(_reactNative.ScrollView, null, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.pageContent
  }, renderHeader(), (submission === null || submission === void 0 ? void 0 : submission.purpose) && /*#__PURE__*/React.createElement(_reactNative.Text, {
    style: _theme.TextTheme.labelSubtitle
  }, submission.purpose), renderBody())), footer(), /*#__PURE__*/React.createElement(_ProofRequestAccept.default, {
    visible: acceptModalVisible,
    proofId: '',
    confirmationOnly: true
  }), /*#__PURE__*/React.createElement(_CommonRemoveModal.default, {
    usage: _remove.ModalUsage.ProofRequestDecline,
    visible: declineModalVisible,
    onSubmit: handleDeclineTouched,
    onCancel: toggleDeclineModalVisible
  }));
};
var _default = exports.default = OpenIDProofPresentation;
//# sourceMappingURL=OpenIDProofPresentation.js.map