"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactHooks = require("@aries-framework/react-hooks");
var _ariesBifoldVerifier = require("@hyperledger/aries-bifold-verifier");
var _legacy = require("@hyperledger/aries-oca/build/legacy");
var _react = _interopRequireWildcard(require("react"));
var _reactI18next = require("react-i18next");
var _reactNative = require("react-native");
var _animatedComponents = require("../../contexts/animated-components");
var _configuration = require("../../contexts/configuration");
var _theme = require("../../contexts/theme");
var _credential = require("../../utils/credential");
var _helpers = require("../../utils/helpers");
var _oca = require("../../utils/oca");
var _testable = require("../../utils/testable");
var _RecordField = require("../record/RecordField");
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 => {
  var _overlay$bundle, _overlay$bundle2;
  let {
    sharedData
  } = _ref;
  const {
    ColorPallet,
    TextTheme
  } = (0, _theme.useTheme)();
  const {
    OCABundleResolver
  } = (0, _configuration.useConfiguration)();
  const {
    i18n
  } = (0, _reactI18next.useTranslation)();
  const {
    width
  } = (0, _reactNative.useWindowDimensions)();
  const logoHeight = width * 0.12;
  const padding = width * 0.05;
  const borderRadius = 10;
  const styles = _reactNative.StyleSheet.create({
    container: {
      marginBottom: 20,
      backgroundColor: ColorPallet.grayscale.white,
      borderRadius: borderRadius
    },
    cardContainer: {
      flexDirection: 'row',
      minHeight: 0.33 * width
    },
    cardColorContainer: {
      width: logoHeight,
      borderTopLeftRadius: borderRadius,
      borderBottomLeftRadius: borderRadius
    },
    logoContainer: {
      top: padding,
      left: -1 * logoHeight + padding,
      width: logoHeight,
      height: logoHeight,
      backgroundColor: ColorPallet.grayscale.white,
      borderRadius: 8,
      justifyContent: 'center',
      alignItems: 'center',
      elevation: 5
    },
    cardAttributes: {
      width: '65%',
      paddingTop: 20,
      paddingBottom: 10
    },
    attributeContainer: {
      marginBottom: 10
    },
    attributeName: {
      fontSize: 16,
      fontWeight: 'normal',
      color: ColorPallet.grayscale.black
    },
    attributeValue: {
      fontSize: 18,
      fontWeight: 'bold',
      color: ColorPallet.grayscale.black
    }
  });
  const [overlay, setOverlay] = (0, _react.useState)(undefined);
  const attributeTypes = overlay === null || overlay === void 0 ? void 0 : (_overlay$bundle = overlay.bundle) === null || _overlay$bundle === void 0 ? void 0 : _overlay$bundle.captureBase.attributes;
  const attributeFormats = overlay === null || overlay === void 0 ? void 0 : (_overlay$bundle2 = overlay.bundle) === null || _overlay$bundle2 === void 0 ? void 0 : _overlay$bundle2.bundle.attributes.map(attr => {
    return {
      name: attr.name,
      format: attr.format
    };
  }).reduce((prev, curr) => {
    return {
      ...prev,
      [curr.name]: curr.format
    };
  }, {});
  (0, _react.useEffect)(() => {
    const attributes = (0, _oca.buildFieldsFromSharedAnonCredsProof)(sharedData.data);
    const params = {
      identifiers: {
        credentialDefinitionId: sharedData.identifiers.cred_def_id,
        schemaId: sharedData.identifiers.schema_id
      },
      language: i18n.language,
      attributes
    };
    OCABundleResolver.resolveAllBundles(params).then(bundle => {
      setOverlay(bundle);
    });
  }, [sharedData]);
  const CardField = _ref2 => {
    var _parsedPredicate, _parsedPredicate2;
    let {
      item
    } = _ref2;
    const {
      t
    } = (0, _reactI18next.useTranslation)();
    let parsedPredicate = undefined;
    if (item instanceof _legacy.Predicate) {
      parsedPredicate = (0, _helpers.pTypeToText)(item, t, attributeTypes);
      parsedPredicate.pValue = (0, _helpers.formatIfDate)(attributeFormats[item.name ?? ''], parsedPredicate.pValue);
    } else {
      ;
      item.value = (0, _helpers.formatIfDate)(attributeFormats[item.name ?? ''], item.value);
    }
    return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      key: item.name,
      style: [styles.attributeContainer]
    }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
      style: styles.attributeName
    }, item.label || item.name), item instanceof _legacy.Attribute && /*#__PURE__*/_react.default.createElement(_RecordField.AttributeValue, {
      style: styles.attributeValue,
      field: item,
      shown: true
    }), item instanceof _legacy.Predicate && /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
      style: styles.attributeValue
    }, `${(_parsedPredicate = parsedPredicate) === null || _parsedPredicate === void 0 ? void 0 : _parsedPredicate.pType} ${(_parsedPredicate2 = parsedPredicate) === null || _parsedPredicate2 === void 0 ? void 0 : _parsedPredicate2.pValue}`));
  };
  const CardBody = _ref3 => {
    var _overlay$presentation;
    let {
      overlay
    } = _ref3;
    return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      style: styles.cardAttributes
    }, (_overlay$presentation = overlay.presentationFields) === null || _overlay$presentation === void 0 ? void 0 : _overlay$presentation.map(item => /*#__PURE__*/_react.default.createElement(CardField, {
      item: item,
      key: item.name || item.toString()
    })));
  };
  const CardColor = _ref4 => {
    var _overlay$brandingOver;
    let {
      overlay
    } = _ref4;
    return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      testID: (0, _testable.testIdWithKey)('CardColor'),
      style: [{
        backgroundColor: (_overlay$brandingOver = overlay.brandingOverlay) === null || _overlay$brandingOver === void 0 ? void 0 : _overlay$brandingOver.primaryBackgroundColor
      }, styles.cardColorContainer]
    });
  };
  const CardLogo = _ref5 => {
    var _overlay$brandingOver2, _overlay$brandingOver3, _ref6, _overlay$metaOverlay, _overlay$metaOverlay2;
    let {
      overlay
    } = _ref5;
    return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      style: styles.logoContainer
    }, (_overlay$brandingOver2 = overlay.brandingOverlay) !== null && _overlay$brandingOver2 !== void 0 && _overlay$brandingOver2.logo ? /*#__PURE__*/_react.default.createElement(_reactNative.Image, {
      source: (0, _credential.toImageSource)((_overlay$brandingOver3 = overlay.brandingOverlay) === null || _overlay$brandingOver3 === void 0 ? void 0 : _overlay$brandingOver3.logo),
      style: {
        resizeMode: 'cover',
        width: logoHeight,
        height: logoHeight,
        borderRadius: 8
      }
    }) : /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
      style: [TextTheme.normal, {
        fontSize: 0.5 * logoHeight,
        fontWeight: 'bold',
        alignSelf: 'center',
        color: ColorPallet.grayscale.black
      }]
    }, (_ref6 = ((_overlay$metaOverlay = overlay.metaOverlay) === null || _overlay$metaOverlay === void 0 ? void 0 : _overlay$metaOverlay.issuer) ?? ((_overlay$metaOverlay2 = overlay.metaOverlay) === null || _overlay$metaOverlay2 === void 0 ? void 0 : _overlay$metaOverlay2.name) ?? 'C') === null || _ref6 === void 0 ? void 0 : _ref6.charAt(0).toUpperCase()));
  };
  return overlay ? /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.container
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    key: sharedData.identifiers.cred_def_id,
    style: styles.cardContainer
  }, /*#__PURE__*/_react.default.createElement(CardColor, {
    overlay: overlay
  }), /*#__PURE__*/_react.default.createElement(CardLogo, {
    overlay: overlay
  }), /*#__PURE__*/_react.default.createElement(CardBody, {
    overlay: overlay
  }))) : null;
};
const SharedProofData = _ref7 => {
  let {
    recordId,
    onSharedProofDataLoad
  } = _ref7;
  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 AFJ');
  }
  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);
        setSharedData(groupedSharedProofData);
        if (!onSharedProofDataLoad) return;
        onSharedProofDataLoad(Array.from(groupedSharedProofData.values()));
      }
    }).finally(() => {
      setLoading(false);
    });
  }, [recordId]);
  return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.container
  }, loading && /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.loaderContainer
  }, /*#__PURE__*/_react.default.createElement(LoadingIndicator, null)), !loading && (sharedData === null || sharedData === void 0 ? void 0 : sharedData.size) && Array.from(sharedData.values()).map(item => /*#__PURE__*/_react.default.createElement(SharedDataCard, {
    sharedData: item,
    key: item.identifiers.cred_def_id
  })));
};
var _default = SharedProofData;
exports.default = _default;
//# sourceMappingURL=SharedProofData.js.map