import { hasPredicates, isParameterizable } from '@hyperledger/aries-bifold-verifier';
import { MetaOverlay, OverlayType } from '@hyperledger/aries-oca';
import React, { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { FlatList, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import Icon from 'react-native-vector-icons/MaterialIcons';
import EmptyList from '../components/misc/EmptyList';
import { TOKENS, useContainer } from '../container-api';
import { useStore } from '../contexts/store';
import { useTheme } from '../contexts/theme';
import { useTemplates } from '../hooks/proof-request-templates';
import { Screens } from '../types/navigators';
import { testIdWithKey } from '../utils/testable';
const ProofRequestsCard = _ref => {
  let {
    navigation,
    template,
    connectionId
  } = _ref;
  const {
    t
  } = useTranslation();
  const {
    i18n
  } = useTranslation();
  const {
    ListItems
  } = useTheme();
  const bundleResolver = useContainer().resolve(TOKENS.UTIL_OCA_RESOLVER);
  const style = StyleSheet.create({
    card: {
      ...ListItems.requestTemplateBackground,
      flexDirection: 'row',
      borderRadius: 8,
      padding: 12,
      marginBottom: 10
    },
    textContainer: {
      flex: 1
    },
    templateTitle: {
      ...ListItems.requestTemplateTitle,
      fontSize: 16,
      marginBottom: 4
    },
    templateDetails: {
      ...ListItems.requestTemplateDetails,
      fontSize: 16,
      marginBottom: 4
    },
    templateZkpLabel: {
      ...ListItems.requestTemplateZkpLabel,
      fontSize: 12
    },
    iconContainer: {
      alignSelf: 'center'
    },
    icon: {
      ...ListItems.requestTemplateIcon,
      fontSize: 36
    }
  });
  const [meta, setMeta] = useState(undefined);
  useEffect(() => {
    bundleResolver.resolve({
      identifiers: {
        templateId: template.id
      },
      language: i18n.language
    }).then(bundle => {
      const metaOverlay = (bundle === null || bundle === void 0 ? void 0 : bundle.metaOverlay) || new MetaOverlay({
        capture_base: '',
        type: OverlayType.Meta10,
        name: template.name,
        description: template.description,
        language: i18n.language,
        credential_help_text: '',
        credential_support_url: '',
        issuer: '',
        issuer_description: '',
        issuer_url: ''
      });
      setMeta(metaOverlay);
    });
  }, [template]);
  return meta ? /*#__PURE__*/React.createElement(TouchableOpacity, {
    style: style.card,
    onPress: () => navigation.navigate(Screens.ProofRequestDetails, {
      templateId: template.id,
      connectionId
    }),
    accessibilityLabel: t('Screens.ProofRequestDetails'),
    testID: testIdWithKey('ProofRequestsCard')
  }, /*#__PURE__*/React.createElement(View, {
    style: style.textContainer
  }, /*#__PURE__*/React.createElement(Text, {
    style: style.templateTitle,
    numberOfLines: 1
  }, meta.name), /*#__PURE__*/React.createElement(Text, {
    style: style.templateDetails,
    numberOfLines: 2
  }, meta.description), hasPredicates(template) && /*#__PURE__*/React.createElement(Text, {
    style: style.templateZkpLabel
  }, t('Verifier.ZeroKnowledgeProof')), isParameterizable(template) && /*#__PURE__*/React.createElement(Text, {
    style: style.templateZkpLabel
  }, t('Verifier.Parameterizable'))), /*#__PURE__*/React.createElement(View, {
    style: style.iconContainer
  }, /*#__PURE__*/React.createElement(Icon, {
    style: style.icon,
    name: 'chevron-right'
  }))) : null;
};
const ListProofRequests = _ref2 => {
  let {
    navigation,
    route
  } = _ref2;
  const {
    t
  } = useTranslation();
  const {
    ColorPallet
  } = useTheme();
  const [store] = useStore();
  const style = StyleSheet.create({
    container: {
      flexGrow: 1,
      margin: 24,
      elevation: 5
    }
  });
  const {
    connectionId
  } = route === null || route === void 0 ? void 0 : route.params;

  // if useDevVerifierTemplates not set then exclude dev templates
  const proofRequestTemplates = useTemplates().filter(tem => store.preferences.useDevVerifierTemplates || !tem.devOnly);
  return /*#__PURE__*/React.createElement(SafeAreaView, {
    style: style.container,
    edges: ['left', 'right']
  }, /*#__PURE__*/React.createElement(FlatList, {
    style: {
      backgroundColor: ColorPallet.brand.primaryBackground
    },
    data: proofRequestTemplates,
    keyExtractor: records => records.id,
    renderItem: _ref3 => {
      let {
        item
      } = _ref3;
      return /*#__PURE__*/React.createElement(ProofRequestsCard, {
        template: item,
        connectionId: connectionId,
        navigation: navigation
      });
    },
    ListEmptyComponent: () => /*#__PURE__*/React.createElement(EmptyList, {
      message: t('Verifier.EmptyList')
    })
  }));
};
export default ListProofRequests;
//# sourceMappingURL=ListProofRequests.js.map