import { ConnectionType, DidExchangeState } from '@aries-framework/core';
import { useConnections } from '@aries-framework/react-hooks';
import React, { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { FlatList, StyleSheet, View } from 'react-native';
import HeaderButton, { ButtonLocation } from '../components/buttons/HeaderButton';
import ContactListItem from '../components/listItems/ContactListItem';
import EmptyListContacts from '../components/misc/EmptyListContacts';
import { useConfiguration } from '../contexts/configuration';
import { useStore } from '../contexts/store';
import { useTheme } from '../contexts/theme';
import { Screens, Stacks } from '../types/navigators';
import { testIdWithKey } from '../utils/testable';
const ListContacts = _ref => {
  let {
    navigation
  } = _ref;
  const {
    ColorPallet
  } = useTheme();
  const {
    t
  } = useTranslation();
  const style = StyleSheet.create({
    list: {
      backgroundColor: ColorPallet.brand.secondaryBackground
    },
    itemSeparator: {
      backgroundColor: ColorPallet.brand.primaryBackground,
      height: 1,
      marginHorizontal: 16
    }
  });
  const {
    records
  } = useConnections();
  const [store] = useStore();
  const {
    contactHideList
  } = useConfiguration();
  // Filter out mediator agents and hidden contacts when not in dev mode
  let connections = records;
  if (!store.preferences.developerModeEnabled) {
    connections = records.filter(r => {
      return !r.connectionTypes.includes(ConnectionType.Mediator) && !(contactHideList !== null && contactHideList !== void 0 && contactHideList.includes((r.theirLabel || r.alias) ?? '')) && r.state === DidExchangeState.Completed;
    });
  }
  const onPressAddContact = () => {
    var _navigation$getParent;
    (_navigation$getParent = navigation.getParent()) === null || _navigation$getParent === void 0 ? void 0 : _navigation$getParent.navigate(Stacks.ConnectStack, {
      screen: Screens.Scan,
      params: {
        defaultToConnect: true
      }
    });
  };
  useEffect(() => {
    if (store.preferences.useConnectionInviterCapability) {
      navigation.setOptions({
        headerRight: () => /*#__PURE__*/React.createElement(HeaderButton, {
          buttonLocation: ButtonLocation.Right,
          accessibilityLabel: t('Contacts.AddContact'),
          testID: testIdWithKey('AddContact'),
          onPress: onPressAddContact,
          icon: "plus-circle-outline"
        })
      });
    } else {
      navigation.setOptions({
        headerRight: () => false
      });
    }
  }, [store.preferences.useConnectionInviterCapability]);
  return /*#__PURE__*/React.createElement(View, null, /*#__PURE__*/React.createElement(FlatList, {
    style: style.list,
    data: connections,
    ItemSeparatorComponent: () => /*#__PURE__*/React.createElement(View, {
      style: style.itemSeparator
    }),
    keyExtractor: connection => connection.id,
    renderItem: _ref2 => {
      let {
        item: connection
      } = _ref2;
      return /*#__PURE__*/React.createElement(ContactListItem, {
        contact: connection,
        navigation: navigation
      });
    },
    ListEmptyComponent: () => /*#__PURE__*/React.createElement(EmptyListContacts, {
      navigation: navigation
    })
  }));
};
export default ListContacts;
//# sourceMappingURL=ListContacts.js.map