"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _core = require("@aries-framework/core");
var _reactHooks = require("@aries-framework/react-hooks");
var _ariesBifoldVerifier = require("@hyperledger/aries-bifold-verifier");
var _react = _interopRequireWildcard(require("react"));
var _reactI18next = require("react-i18next");
var _reactNative = require("react-native");
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _MaterialIcons = _interopRequireDefault(require("react-native-vector-icons/MaterialIcons"));
var _EmptyList = _interopRequireDefault(require("../components/misc/EmptyList"));
var _store = require("../contexts/store");
var _theme = require("../contexts/theme");
var _navigators = require("../types/navigators");
var _helpers = require("../utils/helpers");
var _testable = require("../utils/testable");
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 getPresentationStateLabel = record => {
  switch (record.state) {
    case _core.ProofState.RequestSent:
      return 'Verifier.RequestSent';
    case _core.ProofState.PresentationReceived:
      return 'Verifier.PresentationReceived';
    case _core.ProofState.Declined:
    case _core.ProofState.Abandoned:
      return 'Verifier.ProofRequestRejected';
    case _core.ProofState.Done:
      return record.isVerified ? 'Verifier.PresentationReceived' : 'Verifier.PresentationFailed';
    default:
      return '';
  }
};
const ProofRequestUsageHistoryRecord = _ref => {
  let {
    record,
    navigation
  } = _ref;
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const {
    ListItems,
    ColorPallet
  } = (0, _theme.useTheme)();
  const [store] = (0, _store.useStore)();
  const connection = record.connectionId ? (0, _reactHooks.useConnectionById)(record.connectionId) : undefined;
  const theirLabel = (0, _react.useMemo)(() => (0, _helpers.getConnectionName)(connection, store.preferences.alternateContactNames), [connection, store.preferences.alternateContactNames]);
  const style = _reactNative.StyleSheet.create({
    card: {
      ...ListItems.requestTemplateBackground,
      flexGrow: 1,
      flexDirection: 'row',
      borderRadius: 8,
      paddingHorizontal: 10,
      marginBottom: 10
    },
    leftContainer: {
      flexDirection: 'column',
      marginVertical: 10
    },
    cardRow: {
      flexDirection: 'row',
      marginVertical: 2
    },
    valueLabel: {
      color: ColorPallet.grayscale.black
    },
    valueText: {
      ...ListItems.requestTemplateTitle,
      fontSize: 16,
      marginLeft: 4
    },
    rightContainer: {
      flexGrow: 1,
      flexDirection: 'column',
      alignItems: 'flex-end',
      justifyContent: 'flex-end'
    },
    icon: {
      ...ListItems.requestTemplateIcon,
      fontSize: 36
    },
    date: {
      ...ListItems.requestTemplateDate,
      fontSize: 10
    }
  });
  const presentationReceived = (0, _react.useMemo)(() => (0, _ariesBifoldVerifier.isPresentationReceived)(record), [record]);
  const onDetails = (0, _react.useCallback)(() => {
    navigation.navigate(_navigators.Screens.ProofDetails, {
      recordId: record.id,
      isHistory: true
    });
  }, [navigation, record]);
  return /*#__PURE__*/_react.default.createElement(_reactNative.TouchableOpacity, {
    style: style.card,
    onPress: onDetails,
    disabled: !presentationReceived,
    accessibilityLabel: t('Screens.ProofDetails'),
    testID: (0, _testable.testIdWithKey)('ProofDetails')
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.leftContainer
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.cardRow
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: style.valueLabel
  }, t('Verifier.PresentationFrom'), ":"), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: style.valueText
  }, theirLabel || t('Verifier.ConnectionlessPresentation'))), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.cardRow
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: style.valueLabel
  }, t('Verifier.PresentationState'), ":"), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: style.valueText
  }, t(getPresentationStateLabel(record))))), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.rightContainer
  }, presentationReceived && /*#__PURE__*/_react.default.createElement(_MaterialIcons.default, {
    style: style.icon,
    name: 'chevron-right'
  }), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: style.date
  }, (0, _helpers.formatTime)(record.createdAt, {
    shortMonth: true
  }))));
};
const ProofRequestUsageHistory = _ref2 => {
  let {
    route,
    navigation
  } = _ref2;
  const {
    templateId
  } = route === null || route === void 0 ? void 0 : route.params;
  const style = _reactNative.StyleSheet.create({
    container: {
      flexGrow: 1,
      margin: 20,
      elevation: 5
    }
  });
  const proofs = (0, _ariesBifoldVerifier.useProofsByTemplateId)(templateId);
  return /*#__PURE__*/_react.default.createElement(_reactNativeSafeAreaContext.SafeAreaView, {
    style: style.container,
    edges: ['left', 'right']
  }, /*#__PURE__*/_react.default.createElement(_reactNative.FlatList, {
    data: proofs,
    keyExtractor: proof => proof.id,
    renderItem: _ref3 => {
      let {
        item
      } = _ref3;
      return /*#__PURE__*/_react.default.createElement(ProofRequestUsageHistoryRecord, {
        record: item,
        navigation: navigation
      });
    },
    ListEmptyComponent: () => /*#__PURE__*/_react.default.createElement(_EmptyList.default, null)
  }));
};
var _default = ProofRequestUsageHistory;
exports.default = _default;
//# sourceMappingURL=ProofRequestUsageHistory.js.map