"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _styles = require("./styles");
var _reactNative = require("react-native");
var _SearchInput = _interopRequireDefault(require("../../../../../components/SearchInput"));
var _useCommunityMemberCollection = require("../../../../../hooks/collections/community/useCommunityMemberCollection");
var _MemberItem = _interopRequireDefault(require("../MemberItem"));
var _useSearchMemberByDisplayNameCollection = require("../../../../../hooks/collections/search/useSearchMemberByDisplayNameCollection");
var _MemberSkeleton = _interopRequireDefault(require("../../../shared/components/MemberSkeleton"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function useMemberList({
  community
}) {
  const {
    styles,
    theme
  } = (0, _styles.useStyles)();
  const [search, setSearch] = (0, _react.useState)('');
  const memberCollection = (0, _useCommunityMemberCollection.useCommunityMemberCollection)({
    enabled: !!community,
    params: {
      communityId: community === null || community === void 0 ? void 0 : community.communityId,
      memberships: ['member'],
      limit: 20
    }
  });
  const searchMemberCollection = (0, _useSearchMemberByDisplayNameCollection.useSearchMemberByDisplayNameCollection)({
    params: {
      search,
      communityId: community === null || community === void 0 ? void 0 : community.communityId
    },
    enabled: !!(search !== null && search !== void 0 && search.trim()) && !!community
  });
  const collection = search !== null && search !== void 0 && search.trim() ? searchMemberCollection : memberCollection;
  return {
    styles,
    theme,
    search,
    setSearch,
    collection
  };
}
const MemberList = ({
  community
}) => {
  const {
    search,
    setSearch,
    styles,
    collection
  } = useMemberList({
    community
  });
  return /*#__PURE__*/React.createElement(_reactNative.View, null, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.searchContainer
  }, /*#__PURE__*/React.createElement(_SearchInput.default, {
    inputProps: {
      value: search,
      onChangeText: setSearch,
      placeholder: 'Search member'
    }
  })), collection.isLoading && /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.skeletonContainer
  }, /*#__PURE__*/React.createElement(_MemberSkeleton.default, null), /*#__PURE__*/React.createElement(_MemberSkeleton.default, null), /*#__PURE__*/React.createElement(_MemberSkeleton.default, null)), /*#__PURE__*/React.createElement(_reactNative.FlatList, {
    data: collection.data,
    showsVerticalScrollIndicator: false,
    contentContainerStyle: styles.contentContainer,
    onEndReached: () => {
      var _collection$fetchNext;
      if (collection.hasNextPage && !collection.isFetchingNextPage) (_collection$fetchNext = collection.fetchNextPage) === null || _collection$fetchNext === void 0 || _collection$fetchNext.call(collection);
    },
    ListFooterComponent: collection.isFetchingNextPage ? /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.skeletonContainer
    }, /*#__PURE__*/React.createElement(_MemberSkeleton.default, null), /*#__PURE__*/React.createElement(_MemberSkeleton.default, null), /*#__PURE__*/React.createElement(_MemberSkeleton.default, null)) : null,
    renderItem: ({
      item
    }) => /*#__PURE__*/React.createElement(_MemberItem.default, {
      member: item,
      key: item.userId,
      refreshMembers: collection.refetch,
      communityId: community.communityId
    })
  }));
};
var _default = exports.default = MemberList;
//# sourceMappingURL=MemberList.js.map