"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.OpenIDCredScreenMode = void 0;
var _react = _interopRequireWildcard(require("react"));
var _navigators = require("../../../types/navigators");
var _display = require("../display");
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _CommonRemoveModal = _interopRequireDefault(require("../../../components/modals/CommonRemoveModal"));
var _remove = require("../../../types/remove");
var _reactNative = require("react-native");
var _theme = require("../../../theme");
var _reactI18next = require("react-i18next");
var _Button = _interopRequireWildcard(require("../../../components/buttons/Button"));
var _testable = require("../../../utils/testable");
var _RecordHeader = _interopRequireDefault(require("../../../components/record/RecordHeader"));
var _RecordFooter = _interopRequireDefault(require("../../../components/record/RecordFooter"));
var _theme2 = require("../../../contexts/theme");
var _OpenIDCredentialCard = _interopRequireDefault(require("../components/OpenIDCredentialCard"));
var _utils = require("../utils/utils");
var _RecordField = _interopRequireDefault(require("../../../components/record/RecordField"));
var _error = require("../../../types/error");
var _constants = require("../../../constants");
var _reactHooks = require("@credo-ts/react-hooks");
var _CredentialOfferAccept = _interopRequireDefault(require("../../../screens/CredentialOfferAccept"));
var _RecordRemove = _interopRequireDefault(require("../../../components/record/RecordRemove"));
var _OpenIDCredentialRecordProvider = require("../context/OpenIDCredentialRecordProvider");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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; }
let OpenIDCredScreenMode = exports.OpenIDCredScreenMode = /*#__PURE__*/function (OpenIDCredScreenMode) {
  OpenIDCredScreenMode[OpenIDCredScreenMode["offer"] = 0] = "offer";
  OpenIDCredScreenMode[OpenIDCredScreenMode["details"] = 1] = "details";
  return OpenIDCredScreenMode;
}({});
const OpenIDCredentialDetails = ({
  navigation,
  route
}) => {
  // FIXME: change params to accept credential id to avoid 'non-serializable' warnings
  const {
    credential,
    screenMode
  } = route.params;
  const credentialDisplay = (0, _display.getCredentialForDisplay)(credential);
  const {
    display,
    attributes
  } = credentialDisplay;
  const fields = (0, _utils.buildFieldsFromOpenIDTemplate)(attributes);
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const {
    ColorPallet
  } = (0, _theme2.useTheme)();
  const {
    agent
  } = (0, _reactHooks.useAgent)();
  const {
    storeCredential,
    removeCredential
  } = (0, _OpenIDCredentialRecordProvider.useOpenIDCredentials)();
  const [isRemoveModalDisplayed, setIsRemoveModalDisplayed] = (0, _react.useState)(false);
  const [buttonsVisible, setButtonsVisible] = (0, _react.useState)(true);
  const [acceptModalVisible, setAcceptModalVisible] = (0, _react.useState)(false);
  const styles = _reactNative.StyleSheet.create({
    headerTextContainer: {
      paddingHorizontal: 25,
      paddingVertical: 16
    },
    headerText: {
      ..._theme.TextTheme.normal,
      flexShrink: 1
    },
    footerButton: {
      paddingTop: 10
    }
  });
  const toggleDeclineModalVisible = () => setIsRemoveModalDisplayed(!isRemoveModalDisplayed);
  const handleDeclineTouched = async () => {
    var _navigation$getParent;
    toggleDeclineModalVisible();
    if (screenMode === OpenIDCredScreenMode.offer) (_navigation$getParent = navigation.getParent()) === null || _navigation$getParent === void 0 || _navigation$getParent.navigate(_navigators.TabStacks.HomeStack, {
      screen: _navigators.Screens.Home
    });else handleRemove();
  };
  const handleAcceptTouched = async () => {
    try {
      if (!agent) {
        return;
      }
      await storeCredential(credential);
      setAcceptModalVisible(true);
    } catch (err) {
      setButtonsVisible(true);
      const error = new _error.BifoldError(t('Error.Title1024'), t('Error.Message1024'), (err === null || err === void 0 ? void 0 : err.message) ?? err, 1024);
      _reactNative.DeviceEventEmitter.emit(_constants.EventTypes.ERROR_ADDED, error);
    }
  };
  const handleRemove = async () => {
    try {
      await removeCredential(credential);
      navigation.pop();
    } catch (err) {
      const error = new _error.BifoldError(t('Error.Title1025'), t('Error.Message1025'), (err === null || err === void 0 ? void 0 : err.message) ?? err, 1025);
      _reactNative.DeviceEventEmitter.emit(_constants.EventTypes.ERROR_ADDED, error);
    }
  };
  const footerButton = (title, buttonPress, buttonType, testID, accessibilityLabel) => {
    return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      style: styles.footerButton
    }, /*#__PURE__*/_react.default.createElement(_Button.default, {
      title: title,
      accessibilityLabel: accessibilityLabel,
      testID: testID,
      buttonType: buttonType,
      onPress: buttonPress,
      disabled: !buttonsVisible
    }));
  };
  const header = () => {
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, screenMode === OpenIDCredScreenMode.offer && /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      style: styles.headerTextContainer
    }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
      style: styles.headerText,
      testID: (0, _testable.testIdWithKey)('HeaderText')
    }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, null, display.issuer.name || t('ContactDetails.AContact')), ' ', t('CredentialOffer.IsOfferingYouACredential'))), credential && /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      style: {
        marginHorizontal: 15,
        marginBottom: 16
      }
    }, /*#__PURE__*/_react.default.createElement(_OpenIDCredentialCard.default, {
      credentialDisplay: display
    })));
  };
  const footer = () => {
    const paddingHorizontal = 24;
    const paddingVertical = 16;
    const paddingBottom = 26;
    return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      style: {
        marginBottom: 50
      }
    }, screenMode === OpenIDCredScreenMode.offer ? /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      style: {
        paddingHorizontal: paddingHorizontal,
        paddingVertical: paddingVertical,
        paddingBottom: paddingBottom,
        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'))) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      style: {
        backgroundColor: ColorPallet.brand.secondaryBackground,
        marginTop: paddingVertical,
        paddingHorizontal,
        paddingVertical
      }
    }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
      testID: (0, _testable.testIdWithKey)('IssuerName')
    }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
      style: [_theme.TextTheme.title]
    }, t('CredentialDetails.IssuedBy') + ' '), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
      style: [_theme.TextTheme.normal]
    }, display.issuer.name || t('ContactDetails.AContact')))), /*#__PURE__*/_react.default.createElement(_RecordRemove.default, {
      onRemove: toggleDeclineModalVisible
    })));
  };
  const body = () => {
    return /*#__PURE__*/_react.default.createElement(_reactNative.FlatList, {
      data: fields,
      keyExtractor: ({
        name
      }, index) => name || index.toString(),
      renderItem: ({
        item: attr,
        index
      }) => /*#__PURE__*/_react.default.createElement(_RecordField.default, {
        field: attr,
        hideFieldValue: false,
        shown: true,
        hideBottomBorder: index === fields.length - 1
      }),
      ListHeaderComponent: /*#__PURE__*/_react.default.createElement(_RecordHeader.default, null, header()),
      ListFooterComponent: footer ? /*#__PURE__*/_react.default.createElement(_RecordFooter.default, null, footer()) : null
    });
  };
  const screenEdges = screenMode === OpenIDCredScreenMode.offer ? ['bottom', 'left', 'right'] : ['left', 'right'];
  return /*#__PURE__*/_react.default.createElement(_reactNativeSafeAreaContext.SafeAreaView, {
    style: {
      flexGrow: 1
    },
    edges: screenEdges
  }, body(), screenMode === OpenIDCredScreenMode.offer && /*#__PURE__*/_react.default.createElement(_CredentialOfferAccept.default, {
    visible: acceptModalVisible,
    credentialId: '',
    confirmationOnly: true
  }), /*#__PURE__*/_react.default.createElement(_CommonRemoveModal.default, {
    usage: screenMode === OpenIDCredScreenMode.offer ? _remove.ModalUsage.CredentialOfferDecline : _remove.ModalUsage.CredentialRemove,
    visible: isRemoveModalDisplayed,
    onSubmit: handleDeclineTouched,
    onCancel: toggleDeclineModalVisible
  }));
};
var _default = exports.default = OpenIDCredentialDetails;
//# sourceMappingURL=OpenIDCredentialOffer.js.map