"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _enums = require("../../../../enums");
var _hooks = require("../../../../hooks");
var _styles = require("./styles");
var _CommunityCover = require("../../elements/CommunityCover/CommunityCover");
var _CommunityPrivateBadge = _interopRequireDefault(require("../../../../elements/CommunityPrivateBadge/CommunityPrivateBadge"));
var _CommunityName = _interopRequireDefault(require("../../../../elements/CommunityName/CommunityName"));
var _CommunityVerifyBadge = _interopRequireDefault(require("../../../../elements/CommunityVerifyBadge/CommunityVerifyBadge"));
var _CommunityDescription = _interopRequireDefault(require("../../../../elements/CommunityDescription/CommunityDescription"));
var _CommunityInfo = _interopRequireDefault(require("../../../../elements/CommunityInfo/CommunityInfo"));
var _CommunityJoinButtonElement = _interopRequireDefault(require("../../../../elements/CommunityJoinButtonElement/CommunityJoinButtonElement"));
var _CommunityPendingPost = _interopRequireDefault(require("../../../../elements/CommunityPendingPost/CommunityPendingPost"));
var _usePosts = require("../../../../hooks/usePosts");
var _Tab = _interopRequireDefault(require("../../../../features/story/components/Tab/Tab"));
var _types = require("../../../../types");
var _Button = require("../../../../components/Button/Button");
var _native = require("@react-navigation/native");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _feed = require("../../../../features/feed");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const AmityCommunityHeaderComponent = ({
  pageId = _enums.PageID.WildCardPage,
  communityId,
  isScrolling = false,
  isScrolled,
  onHeightChange,
  isFromComponent
}) => {
  const client = _tsSdkReactNative.Client.getActiveClient();
  const navigation = (0, _native.useNavigation)();
  const componentId = _enums.ComponentID.community_header;
  const {
    community
  } = (0, _hooks.useCommunity)(communityId);
  const {
    posts: pendingPosts
  } = (0, _usePosts.usePosts)({
    targetType: 'community',
    targetId: communityId,
    feedType: 'reviewing'
  });
  const {
    accessibilityId,
    themeStyles
  } = (0, _hooks.useAmityComponent)({
    pageId,
    componentId
  });
  const currentUser = (0, _hooks.useUser)((client === null || client === void 0 ? void 0 : client.userId) || '');
  const isCommunityModerator = (0, _hooks.isModerator)(currentUser === null || currentUser === void 0 ? void 0 : currentUser.roles);
  const styles = (0, _styles.useStyles)(themeStyles);
  const [headerHeight, setHeaderHeight] = (0, _react.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(_reactNative.View, {
    onLayout: event => {
      const {
        height
      } = event.nativeEvent.layout;
      updateHeaderHeight(height);
    }
  }, /*#__PURE__*/React.createElement(_CommunityCover.CommunityCover, {
    pageId: pageId,
    componentId: componentId,
    community: community,
    smallHeader: true,
    hideButtons: !isScrolled && isScrolling,
    isFromComponent: isFromComponent
  })) : /*#__PURE__*/React.createElement(_reactNative.View, {
    testID: accessibilityId,
    accessibilityLabel: accessibilityId,
    style: styles.container,
    onLayout: event => {
      const {
        height
      } = event.nativeEvent.layout;
      updateHeaderHeight(height);
    }
  }, /*#__PURE__*/React.createElement(_CommunityCover.CommunityCover, {
    pageId: pageId,
    componentId: componentId,
    community: community,
    smallHeader: false,
    hideButtons: isScrolling,
    isFromComponent: isFromComponent
  }), /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.communityNameWrap
  }, !community.isPublic && /*#__PURE__*/React.createElement(_CommunityPrivateBadge.default, {
    pageId: pageId,
    componentId: componentId
  }), /*#__PURE__*/React.createElement(_CommunityName.default, {
    pageId: pageId,
    componentId: componentId,
    communityName: community.displayName
  }), community.isOfficial && /*#__PURE__*/React.createElement(_CommunityVerifyBadge.default, {
    pageId: pageId,
    componentId: componentId
  })), /*#__PURE__*/React.createElement(_feed.CommunityCategories, {
    pageId: pageId,
    componentId: componentId,
    categories: community.categories,
    allVisible: true,
    style: styles.categoryWrap
  }), /*#__PURE__*/React.createElement(_CommunityDescription.default, {
    pageId: pageId,
    componentId: componentId,
    description: community.description,
    style: styles.descriptionWrap
  }), /*#__PURE__*/React.createElement(_CommunityInfo.default, {
    pageId: pageId,
    componentId: componentId,
    community: community,
    style: styles.infoWrap,
    onPress: () => {
      navigation.navigate('CommunityMembership', {
        community: community
      });
    }
  }), !community.isJoined && /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.joinButtonWrap
  }, /*#__PURE__*/React.createElement(_CommunityJoinButtonElement.default, {
    pageId: pageId,
    componentId: componentId,
    communityId: community.communityId,
    size: _Button.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.default, {
    number: pendingPosts.length,
    pageId: pageId,
    componentId: componentId,
    isModerator: isCommunityModerator,
    style: styles.pendingPostWrap,
    onPress: () => {
      navigation.navigate('CommunityPendingRequest', {
        community
      });
    }
  }), /*#__PURE__*/React.createElement(_Tab.default, {
    type: _types.AmityStoryTabComponentEnum.communityFeed,
    targetId: communityId
  })));
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(AmityCommunityHeaderComponent);
//# sourceMappingURL=Header.js.map