"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactHooks = require("@aries-framework/react-hooks");
var _react = _interopRequireWildcard(require("react"));
var _reactI18next = require("react-i18next");
var _reactNative = require("react-native");
var _store = require("../../contexts/store");
var _theme = require("../../contexts/theme");
var _credentials = require("../../hooks/credentials");
var _proofs = require("../../hooks/proofs");
var _chat = require("../../types/chat");
var _navigators = require("../../types/navigators");
var _helpers = require("../../utils/helpers");
var _testable = require("../../utils/testable");
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 ContactListItem = _ref => {
  let {
    contact,
    navigation
  } = _ref;
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const {
    TextTheme,
    ColorPallet,
    ListItems
  } = (0, _theme.useTheme)();
  const basicMessages = (0, _reactHooks.useBasicMessagesByConnectionId)(contact.id);
  const credentials = (0, _credentials.useCredentialsByConnectionId)(contact.id);
  const proofs = (0, _proofs.useProofsByConnectionId)(contact.id);
  const [message, setMessage] = (0, _react.useState)({
    text: '',
    createdAt: contact.createdAt
  });
  const [store] = (0, _store.useStore)();
  const styles = _reactNative.StyleSheet.create({
    container: {
      flexDirection: 'row',
      padding: 16,
      backgroundColor: ColorPallet.brand.secondaryBackground
    },
    avatarContainer: {
      alignItems: 'center',
      justifyContent: 'center',
      width: 50,
      height: 50,
      borderRadius: 25,
      borderColor: ListItems.avatarCircle.borderColor,
      borderWidth: 1,
      marginRight: 16
    },
    avatarPlaceholder: {
      ...TextTheme.headingFour,
      textAlign: 'center'
    },
    avatarImage: {
      width: 30,
      height: 30
    },
    contactNameContainer: {
      flex: 1,
      paddingVertical: 4
    },
    nameAndTimeContainer: {
      flexDirection: 'row',
      justifyContent: 'space-between',
      flex: 1
    },
    contactNameText: {
      ...TextTheme.labelTitle
    },
    timeContainer: {
      paddingVertical: 4,
      alignSelf: 'center'
    },
    timeText: {
      color: TextTheme.normal.color
    }
  });
  (0, _react.useEffect)(() => {
    const transformedMessages = basicMessages.map(record => {
      return {
        text: record.content,
        createdAt: record.updatedAt || record.createdAt
      };
    });
    transformedMessages.push(...credentials.map(record => {
      const role = (0, _helpers.getCredentialEventRole)(record);
      const userLabel = role === _chat.Role.me ? `${t('Chat.UserYou')} ` : '';
      const actionLabel = t((0, _helpers.getCredentialEventLabel)(record));
      return {
        text: `${userLabel}${actionLabel}.`,
        createdAt: record.updatedAt || record.createdAt
      };
    }));
    transformedMessages.push(...proofs.map(record => {
      const role = (0, _helpers.getProofEventRole)(record);
      const userLabel = role === _chat.Role.me ? `${t('Chat.UserYou')} ` : '';
      const actionLabel = t((0, _helpers.getProofEventLabel)(record));
      return {
        text: `${userLabel}${actionLabel}.`,
        createdAt: record.updatedAt || record.createdAt
      };
    }));

    // don't show a message snippet for the initial connection
    const connectedMessage = {
      text: '',
      createdAt: contact.createdAt
    };
    setMessage([...transformedMessages.sort((a, b) => b.createdAt - a.createdAt), connectedMessage][0]);
  }, [basicMessages, credentials, proofs]);
  const navigateToContact = (0, _react.useCallback)(() => {
    var _navigation$getParent;
    (_navigation$getParent = navigation.getParent()) === null || _navigation$getParent === void 0 ? void 0 : _navigation$getParent.navigate(_navigators.Stacks.ContactStack, {
      screen: _navigators.Screens.Chat,
      params: {
        connectionId: contact.id
      }
    });
  }, [contact]);
  const contactLabel = (0, _react.useMemo)(() => (0, _helpers.getConnectionName)(contact, store.preferences.alternateContactNames), [contact, store.preferences.alternateContactNames]);
  const contactLabelAbbr = (0, _react.useMemo)(() => contactLabel === null || contactLabel === void 0 ? void 0 : contactLabel.charAt(0).toUpperCase(), [contact, store.preferences.alternateContactNames]);
  return /*#__PURE__*/_react.default.createElement(_reactNative.TouchableOpacity, {
    onPress: navigateToContact,
    testID: (0, _testable.testIdWithKey)('Contact'),
    accessibilityLabel: t('ContactDetails.AContact')
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.container
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.avatarContainer
  }, contact.imageUrl ? /*#__PURE__*/_react.default.createElement(_reactNative.View, null, /*#__PURE__*/_react.default.createElement(_reactNative.Image, {
    style: styles.avatarImage,
    source: {
      uri: contact.imageUrl
    }
  })) : /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: styles.avatarPlaceholder
  }, contactLabelAbbr)), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      flex: 1
    }
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.nameAndTimeContainer
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.contactNameContainer
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: styles.contactNameText
  }, contactLabel)), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.timeContainer
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: styles.timeText
  }, (0, _helpers.formatTime)(message.createdAt, {
    shortMonth: true,
    trim: true
  })))), /*#__PURE__*/_react.default.createElement(_reactNative.View, null, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: TextTheme.normal,
    numberOfLines: 1,
    ellipsizeMode: 'tail'
  }, message.text)))));
};
var _default = ContactListItem;
exports.default = _default;
//# sourceMappingURL=ContactListItem.js.map