import { memo, useState } from 'react';
import { View } from 'react-native';
import { ComponentID, PageID } from '../../../../enums';
import { useAmityComponent, useCommunity, isModerator, useUser } from '../../../../hooks';
import { useStyles } from './styles';
import { CommunityCover } from '../../elements/CommunityCover/CommunityCover';
import CommunityPrivateBadge from '../../../../elements/CommunityPrivateBadge/CommunityPrivateBadge';
import CommunityName from '../../../../elements/CommunityName/CommunityName';
import CommunityVerifyBadge from '../../../../elements/CommunityVerifyBadge/CommunityVerifyBadge';
import CommunityDescription from '../../../../elements/CommunityDescription/CommunityDescription';
import CommunityInfo from '../../../../elements/CommunityInfo/CommunityInfo';
import CommunityJoinButtonElement from '../../../../elements/CommunityJoinButtonElement/CommunityJoinButtonElement';
import CommunityPendingPost from '../../../../elements/CommunityPendingPost/CommunityPendingPost';
import { usePosts } from '../../../../hooks/usePosts';
import AmityStoryTabComponent from '../../../../features/story/components/Tab/Tab';
import { AmityStoryTabComponentEnum } from '../../../../types';
import { BUTTON_SIZE } from '../../../../components/Button/Button';
import { useNavigation } from '@react-navigation/native';
import { Client } from '@amityco/ts-sdk-react-native';
import { CommunityCategories } from '../../../../features/feed';
const AmityCommunityHeaderComponent = ({
  pageId = PageID.WildCardPage,
  communityId,
  isScrolling = false,
  isScrolled,
  onHeightChange,
  isFromComponent
}) => {
  const client = Client.getActiveClient();
  const navigation = useNavigation();
  const componentId = ComponentID.community_header;
  const {
    community
  } = useCommunity(communityId);
  const {
    posts: pendingPosts
  } = usePosts({
    targetType: 'community',
    targetId: communityId,
    feedType: 'reviewing'
  });
  const {
    accessibilityId,
    themeStyles
  } = useAmityComponent({
    pageId,
    componentId
  });
  const currentUser = useUser((client === null || client === void 0 ? void 0 : client.userId) || '');
  const isCommunityModerator = isModerator(currentUser === null || currentUser === void 0 ? void 0 : currentUser.roles);
  const styles = useStyles(themeStyles);
  const [headerHeight, setHeaderHeight] = useState(0);
  // Use a single approach for updating height
  const updateHeaderHeight = newHeight => {
    if (newHeight > 0 && newHeight !== headerHeight) {
      setHeaderHeight(newHeight);

      // Call the callback to notify parent
      if (onHeightChange) {
        onHeightChange(newHeight);
      }
    }
  };
  if (!community) return null;
  return /*#__PURE__*/React.createElement(React.Fragment, null, isScrolled ? /*#__PURE__*/React.createElement(View, {
    onLayout: event => {
      const {
        height
      } = event.nativeEvent.layout;
      updateHeaderHeight(height);
    }
  }, /*#__PURE__*/React.createElement(CommunityCover, {
    pageId: pageId,
    componentId: componentId,
    community: community,
    smallHeader: true,
    hideButtons: !isScrolled && isScrolling,
    isFromComponent: isFromComponent
  })) : /*#__PURE__*/React.createElement(View, {
    testID: accessibilityId,
    accessibilityLabel: accessibilityId,
    style: styles.container,
    onLayout: event => {
      const {
        height
      } = event.nativeEvent.layout;
      updateHeaderHeight(height);
    }
  }, /*#__PURE__*/React.createElement(CommunityCover, {
    pageId: pageId,
    componentId: componentId,
    community: community,
    smallHeader: false,
    hideButtons: isScrolling,
    isFromComponent: isFromComponent
  }), /*#__PURE__*/React.createElement(View, {
    style: styles.communityNameWrap
  }, !community.isPublic && /*#__PURE__*/React.createElement(CommunityPrivateBadge, {
    pageId: pageId,
    componentId: componentId
  }), /*#__PURE__*/React.createElement(CommunityName, {
    pageId: pageId,
    componentId: componentId,
    communityName: community.displayName
  }), community.isOfficial && /*#__PURE__*/React.createElement(CommunityVerifyBadge, {
    pageId: pageId,
    componentId: componentId
  })), /*#__PURE__*/React.createElement(CommunityCategories, {
    pageId: pageId,
    componentId: componentId,
    categories: community.categories,
    allVisible: true,
    style: styles.categoryWrap
  }), /*#__PURE__*/React.createElement(CommunityDescription, {
    pageId: pageId,
    componentId: componentId,
    description: community.description,
    style: styles.descriptionWrap
  }), /*#__PURE__*/React.createElement(CommunityInfo, {
    pageId: pageId,
    componentId: componentId,
    community: community,
    style: styles.infoWrap,
    onPress: () => {
      navigation.navigate('CommunityMembership', {
        community: community
      });
    }
  }), !community.isJoined && /*#__PURE__*/React.createElement(View, {
    style: styles.joinButtonWrap
  }, /*#__PURE__*/React.createElement(CommunityJoinButtonElement, {
    pageId: pageId,
    componentId: componentId,
    communityId: community.communityId,
    size: BUTTON_SIZE.LARGE
  })), (isCommunityModerator || (pendingPosts === null || pendingPosts === void 0 ? void 0 : pendingPosts.some(post => {
    var _post$creator;
    return ((_post$creator = post.creator) === null || _post$creator === void 0 ? void 0 : _post$creator.userId) === client.userId;
  }))) && (pendingPosts === null || pendingPosts === void 0 ? void 0 : pendingPosts.length) > 0 && community.isJoined && (community === null || community === void 0 ? void 0 : community.postSetting) === 'ADMIN_REVIEW_POST_REQUIRED' && /*#__PURE__*/React.createElement(CommunityPendingPost, {
    number: pendingPosts.length,
    pageId: pageId,
    componentId: componentId,
    isModerator: isCommunityModerator,
    style: styles.pendingPostWrap,
    onPress: () => {
      navigation.navigate('CommunityPendingRequest', {
        community
      });
    }
  }), /*#__PURE__*/React.createElement(AmityStoryTabComponent, {
    type: AmityStoryTabComponentEnum.communityFeed,
    targetId: communityId
  })));
};
export default /*#__PURE__*/memo(AmityCommunityHeaderComponent);
//# sourceMappingURL=Header.js.map