"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactHooks = require("@credo-ts/react-hooks");
var _ariesBifoldVerifier = require("@hyperledger/aries-bifold-verifier");
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _VerifierCredentialCard = _interopRequireDefault(require("../../components/misc/VerifierCredentialCard"));
var _animatedComponents = require("../../contexts/animated-components");
var _theme = require("../../contexts/theme");
var _oca = require("../../utils/oca");
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 SharedDataCard = _ref => {
  let {
    sharedData
  } = _ref;
  const {
    ColorPallet
  } = (0, _theme.useTheme)();
  const [attributes, setAttributes] = (0, _react.useState)([]);
  (0, _react.useEffect)(() => {
    const attributes = (0, _oca.buildFieldsFromSharedAnonCredsProof)(sharedData.data);
    setAttributes(attributes);
  }, [sharedData]);
  return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      marginBottom: 15
    }
  }, /*#__PURE__*/_react.default.createElement(_VerifierCredentialCard.default, {
    displayItems: attributes,
    style: {
      backgroundColor: ColorPallet.brand.secondaryBackground
    },
    credDefId: sharedData.identifiers.cred_def_id,
    schemaId: sharedData.identifiers.schema_id,
    elevated: true
  }));
};
const SharedProofData = _ref2 => {
  let {
    recordId,
    onSharedProofDataLoad
  } = _ref2;
  const {
    agent
  } = (0, _reactHooks.useAgent)();
  const {
    LoadingIndicator
  } = (0, _animatedComponents.useAnimatedComponents)();
  const styles = _reactNative.StyleSheet.create({
    container: {
      flexGrow: 1
    },
    loaderContainer: {
      height: 200,
      marginTop: 80
    }
  });
  if (!agent) {
    throw new Error('Unable to fetch agent from Credo');
  }
  const [loading, setLoading] = (0, _react.useState)(true);
  const [sharedData, setSharedData] = (0, _react.useState)(undefined);
  (0, _react.useEffect)(() => {
    (0, _ariesBifoldVerifier.getProofData)(agent, recordId).then(data => {
      if (data) {
        const groupedSharedProofData = (0, _ariesBifoldVerifier.groupSharedProofDataByCredential)(data);
        const sharedData = Array.from(groupedSharedProofData.values());
        setSharedData(sharedData);
        if (!onSharedProofDataLoad) return;
        onSharedProofDataLoad(sharedData);
      }
    }).finally(() => {
      setLoading(false);
    });
  }, [recordId]);
  return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.container
  }, loading || !sharedData ? /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.loaderContainer
  }, /*#__PURE__*/_react.default.createElement(LoadingIndicator, null)) : /*#__PURE__*/_react.default.createElement(_reactNative.View, null, sharedData.map(item => /*#__PURE__*/_react.default.createElement(SharedDataCard, {
    key: item.identifiers.cred_def_id,
    sharedData: item
  }))));
};
var _default = SharedProofData;
exports.default = _default;
//# sourceMappingURL=SharedProofData.js.map