function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import * as React from 'react';
// @ts-nocheck
import { StyleSheet, View, Text, FlatList } from 'react-native';
import { SingleEmoji } from './SingleEmoji';
import { KeyboardContext } from '../contexts/KeyboardContext';
import { useKeyboardStore } from '../store/useKeyboardStore';
import { parseEmoji } from '../utils/parseEmoji';
import { removeSkinToneModifier } from '../utils/skinToneSelectorUtils';
const emptyEmoji = {
  emoji: '',
  name: 'blank emoji',
  v: '0',
  toneEnabled: false
};
export const EmojiCategory = /*#__PURE__*/React.memo(_ref => {
  let {
    item: {
      title,
      data
    },
    setKeyboardScrollOffsetY
  } = _ref;
  const {
    onEmojiSelected,
    emojiSize,
    numberOfColumns,
    width,
    hideHeader,
    translation,
    categoryPosition,
    clearEmojiTonesData,
    generateEmojiTones,
    theme,
    styles: themeStyles,
    selectedEmojis
  } = React.useContext(KeyboardContext);
  const {
    setKeyboardState,
    keyboardState
  } = useKeyboardStore();
  const [empty, setEmpty] = React.useState([]);
  React.useEffect(() => {
    if (data.length % numberOfColumns) {
      const fillWithEmpty = new Array(numberOfColumns - data.length % numberOfColumns).fill(emptyEmoji);
      setEmpty(fillWithEmpty);
    }
  }, [numberOfColumns, data]);
  const getItemLayout = React.useCallback((_, index) => ({
    length: emojiSize ? emojiSize : 0,
    offset: emojiSize * Math.ceil(index / numberOfColumns),
    index
  }), [emojiSize, numberOfColumns]);
  const handleEmojiPress = React.useCallback(emoji => {
    if (emoji.name === 'blank emoji') return;
    clearEmojiTonesData();
    const parsedEmoji = parseEmoji(emoji);
    setKeyboardState({
      type: 'RECENT_EMOJI_ADD',
      payload: emoji
    });
    if (Array.isArray(selectedEmojis)) return onEmojiSelected({
      ...parsedEmoji,
      alreadySelected: selectedEmojis.includes(emoji.name)
    });
    onEmojiSelected(parsedEmoji);
  }, [selectedEmojis, onEmojiSelected, setKeyboardState, clearEmojiTonesData]);
  const handleEmojiLongPress = React.useCallback((emoji, emojiIndex, emojiSizes) => {
    clearEmojiTonesData();
    const emojiWithoutTone = {
      ...emoji,
      emoji: removeSkinToneModifier(emoji.emoji)
    };
    generateEmojiTones(emojiWithoutTone, emojiIndex, emojiSizes);
  }, [clearEmojiTonesData, generateEmojiTones]);
  const renderItem = React.useCallback(props => {
    const recentlyUsed = (keyboardState === null || keyboardState === void 0 ? void 0 : keyboardState.recentlyUsed) || [];
    const recentlyUsedEmoji = recentlyUsed === null || recentlyUsed === void 0 ? void 0 : recentlyUsed.find(emoji => emoji.name === props.item.name);
    const isSelected = selectedEmojis && selectedEmojis.includes(props.item.name);
    return /*#__PURE__*/React.createElement(SingleEmoji, _extends({}, props, {
      isSelected: isSelected,
      item: recentlyUsedEmoji || props.item,
      emojiSize: emojiSize,
      onPress: handleEmojiPress,
      onLongPress: handleEmojiLongPress,
      selectedEmojiStyle: isSelected ? [styles.selectedEmoji, {
        backgroundColor: theme.emoji.selected
      }, themeStyles.emoji.selected] : {}
    }));
  }, [keyboardState === null || keyboardState === void 0 ? void 0 : keyboardState.recentlyUsed, selectedEmojis, emojiSize, handleEmojiPress, handleEmojiLongPress, theme.emoji.selected, themeStyles.emoji.selected]);
  const handleOnScroll = ev => {
    setKeyboardScrollOffsetY(ev.nativeEvent.contentOffset.y);
    clearEmojiTonesData();
  };
  const keyExtractor = React.useCallback(item => item.name, []);
  return /*#__PURE__*/React.createElement(View, {
    style: [styles.container, {
      width
    }]
  }, !hideHeader && /*#__PURE__*/React.createElement(Text, {
    style: [styles.sectionTitle, themeStyles.header, {
      color: theme.header
    }]
  }, translation[title]), /*#__PURE__*/React.createElement(FlatList, {
    data: [...data, ...empty],
    keyExtractor: keyExtractor,
    numColumns: numberOfColumns,
    renderItem: renderItem,
    getItemLayout: getItemLayout,
    onScroll: handleOnScroll,
    ListFooterComponent: () => /*#__PURE__*/React.createElement(View, {
      style: categoryPosition === 'floating' ? styles.footerFloating : styles.footer
    }),
    initialNumToRender: 10,
    windowSize: 16,
    maxToRenderPerBatch: 5,
    keyboardShouldPersistTaps: "handled"
  }));
}, (prevProps, nextProps) => {
  if (nextProps.item.title !== 'search') return true;
  if (prevProps.item.data.length !== nextProps.item.data.length) return false;
  return prevProps.item.data.map(d => d.name).join() === nextProps.item.data.map(d => d.name).join();
});
const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingHorizontal: 10,
    marginTop: 6
  },
  sectionTitle: {
    marginTop: 12,
    marginBottom: 6,
    marginLeft: 12
  },
  footer: {
    height: 8
  },
  footerFloating: {
    height: 70
  },
  selectedEmoji: {
    borderRadius: 25
  }
});
//# sourceMappingURL=EmojiCategory.js.map