"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.OpenIDCredScreenMode = void 0;
var _react = _interopRequireWildcard(require("react"));
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 _testable = require("../../../utils/testable");
var _theme2 = require("../../../contexts/theme");
var _error = require("../../../types/error");
var _constants = require("../../../constants");
var _reactHooks = require("@credo-ts/react-hooks");
var _RecordRemove = _interopRequireDefault(require("../../../components/record/RecordRemove"));
var _OpenIDCredentialRecordProvider = require("../context/OpenIDCredentialRecordProvider");
var _containerApi = require("../../../container-api");
var _Record = _interopRequireDefault(require("../../../components/record/Record"));
var _oca = require("../../../utils/oca");
var _CredentialDetailSecondaryHeader = _interopRequireDefault(require("../../../components/views/CredentialDetailSecondaryHeader"));
var _CredentialCardLogo = _interopRequireDefault(require("../../../components/views/CredentialCardLogo"));
var _CredentialDetailPrimaryHeader = _interopRequireDefault(require("../../../components/views/CredentialDetailPrimaryHeader"));
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 paddingHorizontal = 24;
const paddingVertical = 16;
const OpenIDCredentialDetails = ({
  navigation,
  route
}) => {
  var _overlay$brandingOver;
  const {
    credentialId
  } = route.params;
  const [credential, setCredential] = (0, _react.useState)(undefined);
  const [credentialDisplay, setCredentialDisplay] = (0, _react.useState)();
  const {
    t,
    i18n
  } = (0, _reactI18next.useTranslation)();
  const {
    ColorPallet
  } = (0, _theme2.useTheme)();
  const {
    agent
  } = (0, _reactHooks.useAgent)();
  const {
    removeCredential
  } = (0, _OpenIDCredentialRecordProvider.useOpenIDCredentials)();
  const [bundleResolver] = (0, _containerApi.useServices)([_containerApi.TOKENS.UTIL_OCA_RESOLVER]);
  const [isRemoveModalDisplayed, setIsRemoveModalDisplayed] = (0, _react.useState)(false);
  const [overlay, setOverlay] = (0, _react.useState)({
    bundle: undefined,
    presentationFields: [],
    metaOverlay: undefined,
    brandingOverlay: undefined
  });
  const styles = _reactNative.StyleSheet.create({
    container: {
      backgroundColor: (_overlay$brandingOver = overlay.brandingOverlay) === null || _overlay$brandingOver === void 0 ? void 0 : _overlay$brandingOver.primaryBackgroundColor,
      display: 'flex'
    }
  });
  (0, _react.useEffect)(() => {
    if (!agent) return;
    const fetchCredential = async () => {
      try {
        const credentialExchangeRecord = await agent.w3cCredentials.getCredentialRecordById(credentialId);
        setCredential(credentialExchangeRecord);
      } catch (error) {
        // credential not found for id, display an error
        _reactNative.DeviceEventEmitter.emit(_constants.EventTypes.ERROR_ADDED, new _error.BifoldError(t('Error.Title1033'), t('Error.Message1033'), t('CredentialDetails.CredentialNotFound'), 1033));
      }
    };
    fetchCredential();
  }, [credentialId, agent, t]);
  (0, _react.useEffect)(() => {
    if (!credential) return;
    try {
      const credDisplay = (0, _display.getCredentialForDisplay)(credential);
      setCredentialDisplay(credDisplay);
    } catch (error) {
      _reactNative.DeviceEventEmitter.emit(_constants.EventTypes.ERROR_ADDED, new _error.BifoldError(t('Error.Title1033'), t('Error.Message1033'), t('CredentialDetails.CredentialNotFound'), 1033));
    }
  }, [credential, t]);
  (0, _react.useEffect)(() => {
    if (!credentialDisplay || !bundleResolver || !i18n || !credentialDisplay.display) {
      return;
    }
    const resolveOverlay = async () => {
      const resolvedOverlay = await (0, _oca.buildOverlayFromW3cCredential)({
        credentialDisplay,
        language: i18n.language,
        resolver: bundleResolver
      });
      setOverlay(resolvedOverlay);
    };
    resolveOverlay();
  }, [credentialDisplay, bundleResolver, i18n]);
  const toggleDeclineModalVisible = () => setIsRemoveModalDisplayed(!isRemoveModalDisplayed);
  const handleDeclineTouched = async () => {
    handleRemove();
  };
  const handleRemove = async () => {
    if (!credential) return;
    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 header = () => {
    if (!credentialDisplay) return null;
    return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      style: styles.container
    }, /*#__PURE__*/_react.default.createElement(_CredentialDetailSecondaryHeader.default, {
      overlay: overlay
    }), /*#__PURE__*/_react.default.createElement(_CredentialCardLogo.default, {
      overlay: overlay
    }), /*#__PURE__*/_react.default.createElement(_CredentialDetailPrimaryHeader.default, {
      overlay: overlay
    }));
  };
  const footer = () => {
    if (!credentialDisplay) return null;
    return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      style: {
        marginBottom: 50
      }
    }, /*#__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]
    }, credentialDisplay.display.issuer.name || t('ContactDetails.AContact')))), /*#__PURE__*/_react.default.createElement(_RecordRemove.default, {
      onRemove: toggleDeclineModalVisible
    }));
  };
  return /*#__PURE__*/_react.default.createElement(_reactNativeSafeAreaContext.SafeAreaView, {
    style: {
      flexGrow: 1
    },
    edges: ['left', 'right']
  }, /*#__PURE__*/_react.default.createElement(_Record.default, {
    fields: overlay.presentationFields || [],
    hideFieldValues: true,
    header: header,
    footer: footer
  }), /*#__PURE__*/_react.default.createElement(_CommonRemoveModal.default, {
    usage: _remove.ModalUsage.CredentialRemove,
    visible: isRemoveModalDisplayed,
    onSubmit: handleDeclineTouched,
    onCancel: toggleDeclineModalVisible
  }));
};
var _default = exports.default = OpenIDCredentialDetails;
//# sourceMappingURL=OpenIDCredentialDetails.js.map