function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
import { useState } from 'react';
import SearchItem from '../../components/SearchItem';
import { useMentions } from 'react-native-controlled-mentions';
import { useStyles } from './styles';
import { FlatList, TextInput } from 'react-native';
import useSearch from '../useSearch';
function useMention({
  value,
  onChange,
  communityId,
  setMentionUsers,
  setMentionPosition,
  isMentionLimitReached,
  onMentionLimitReached
}) {
  const {
    styles,
    theme
  } = useStyles();
  const [cursorIndex, setCursorIndex] = useState(0);
  const triggersConfig = {
    mention: {
      trigger: '@',
      textStyle: styles.mention,
      isInsertSpaceAfterMention: true
    }
  };

  // Highlight URLs while typing in the TextInput.
  //
  // Rules for react-native-controlled-mentions patternsConfig:
  //  1. The regex MUST have exactly ONE capturing group — the library uses
  //     String.split(regex) and expects the matched text at odd indices.
  //  2. textStyle must be a plain object (not a StyleSheet ID) so the library
  //     can pass it directly as a style prop to its internal Text component.
  const patternsConfig = {
    url: {
      pattern: /(?<![a-zA-Z0-9_])((?:https?|ftp):\/\/(?:[a-zA-Z0-9.\-]+|[\d.]+)(?::\d{1,5})?(?:\/(?:[^\s<>|()]*(?:\([^\s<>|()]*\)[^\s<>|()]*)*)*)?(?<![.])(?![^\s]*\()|mailto:[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}|www\.[a-zA-Z0-9][-a-zA-Z0-9.]*\.[a-zA-Z]{2,}(?:\/(?:[^\s<>|()]*(?:\([^\s<>|()]*\)[^\s<>|()]*)*)*)?(?<![.])(?![^\s]*\())/gi,
      textStyle: {
        color: theme.colors.primary
      }
    }
  };
  const {
    textInputProps,
    triggers
  } = useMentions({
    value,
    onChange,
    triggersConfig,
    patternsConfig,
    onSelectionChange: selection => setCursorIndex(selection.start)
  });
  const renderInput = props => {
    return /*#__PURE__*/React.createElement(TextInput, _extends({}, textInputProps, props));
  };
  const renderSuggestions = ({
    type,
    style,
    bottom
  }) => {
    return /*#__PURE__*/React.createElement(Suggestions, {
      bottom: bottom,
      style: style,
      type: type,
      triggers: triggers,
      cursorIndex: cursorIndex,
      communityId: communityId,
      setMentionUsers: setMentionUsers,
      setMentionPosition: setMentionPosition,
      isMentionLimitReached: isMentionLimitReached,
      onMentionLimitReached: onMentionLimitReached
    });
  };
  return {
    renderInput,
    renderSuggestions
  };
}
const Suggestions = ({
  type,
  style,
  bottom = 0,
  triggers,
  cursorIndex,
  communityId,
  setMentionUsers,
  setMentionPosition,
  isMentionLimitReached,
  onMentionLimitReached
}) => {
  var _triggers$mention;
  const {
    styles
  } = useStyles();
  const keyword = triggers === null || triggers === void 0 || (_triggers$mention = triggers.mention) === null || _triggers$mention === void 0 ? void 0 : _triggers$mention.keyword;
  const {
    searchResult,
    getNextPage
  } = useSearch(keyword, communityId);
  const onSelectUserMention = user => {
    const position = {
      type: 'user',
      userId: user.id,
      displayName: user.displayName,
      length: user.displayName.length + 1,
      index: cursorIndex - 1 - (keyword === null || keyword === void 0 ? void 0 : keyword.length)
    };
    setMentionUsers(user);
    setMentionPosition(position);
  };
  if (keyword == null) return null;
  return /*#__PURE__*/React.createElement(FlatList, {
    data: searchResult,
    nestedScrollEnabled: true,
    keyExtractor: item => item.id,
    keyboardShouldPersistTaps: "handled",
    showsVerticalScrollIndicator: false,
    onEndReached: () => getNextPage === null || getNextPage === void 0 ? void 0 : getNextPage(),
    contentContainerStyle: type === 'comment' && styles.mentionSuggestionContentContainer,
    style: [type === 'post' && styles.postMentionSuggestionContainer, type === 'comment' && styles.commentMentionContainer, {
      height: 56 * searchResult.length
    }, type === 'comment' && {
      bottom: bottom
    }, style && style],
    renderItem: ({
      item
    }) => {
      return /*#__PURE__*/React.createElement(SearchItem, {
        target: item,
        userProfileNavigateEnabled: false,
        onPress: () => {
          var _triggers$mention2;
          if (isMentionLimitReached) {
            onMentionLimitReached === null || onMentionLimitReached === void 0 || onMentionLimitReached();
            return;
          }
          triggers === null || triggers === void 0 || (_triggers$mention2 = triggers.mention) === null || _triggers$mention2 === void 0 || _triggers$mention2.onSelect(item);
          onSelectUserMention(item);
        }
      });
    }
  });
};
export default useMention;
//# sourceMappingURL=index.js.map