import { Pressable, View } from 'react-native';
import BackButtonIconElement from '../../../../elements/BackButtonIconElement/BackButtonIconElement';
import { CommonActions, useNavigation, useRoute } from '@react-navigation/native';
import { PageID, ComponentID } from '../../../../enums';
import { useStyles } from './styles';
import { useCommunity } from '../../../../hooks';
import { useBottomSheet } from '../../../../../core/stores/slices/bottomSheetSlice';
import { useShareableLink } from '../../../../../core/hooks/useShareableLink';
import { ShareableLinkModel } from '../../../../types';
import MenuAction from '../../../../elements/MenuAction/MenuAction';
import MenuButton from '../../../../elements/MenuButton/MenuButton';
import { settings, people } from '../../../../../core/assets/icons';
import { CopyLinkAction } from '../../../../elements/CopyLinkAction';
import { ShareAction } from '../../../../elements/ShareAction';
import { checkEditCommunityPermission } from '../../../../utils/permissions';
export function TopBar({
  pageId = PageID.WildCardPage,
  componentId = ComponentID.WildCardComponent,
  communityId,
  isFromComponent
}) {
  const {
    styles
  } = useStyles();
  const {
    community
  } = useCommunity(communityId);
  const {
    getShareLink
  } = useShareableLink();
  const {
    openBottomSheet,
    closeBottomSheet,
    bottomSheetHeight
  } = useBottomSheet();
  const navigation = useNavigation();
  const route = useRoute();
  const {
    pop
  } = (route === null || route === void 0 ? void 0 : route.params) || {};
  const shareLink = community ? getShareLink(ShareableLinkModel.communities, community.communityId) : null;
  const isModerator = checkEditCommunityPermission(community === null || community === void 0 ? void 0 : community.communityId);
  const canShare = !!shareLink && ((community === null || community === void 0 ? void 0 : community.isPublic) || (community === null || community === void 0 ? void 0 : community.isDiscoverable) || isModerator);
  const isJoined = community === null || community === void 0 ? void 0 : community.isJoined;
  const actions = [{
    show: !!isJoined && !!isModerator,
    action: /*#__PURE__*/React.createElement(MenuAction, {
      gap: "small",
      key: "settings",
      iconProps: {
        xml: settings()
      },
      label: "Community settings",
      onPress: () => {
        closeBottomSheet();
        navigation.navigate('CommunitySetting', {
          community
        });
      }
    })
  }, {
    show: !!isJoined && !isModerator,
    action: /*#__PURE__*/React.createElement(MenuAction, {
      key: "info",
      gap: "small",
      iconProps: {
        xml: people()
      },
      label: "Community information",
      onPress: () => {
        closeBottomSheet();
        navigation.navigate('CommunitySetting', {
          community
        });
      }
    })
  }, {
    show: !!canShare,
    action: /*#__PURE__*/React.createElement(CopyLinkAction, {
      key: "copy",
      link: shareLink,
      pageId: PageID.community_profile_page
    })
  }, {
    show: !!canShare,
    action: /*#__PURE__*/React.createElement(ShareAction, {
      key: "share",
      link: shareLink,
      pageId: PageID.community_profile_page
    })
  }].filter(({
    show
  }) => show);
  const isPreviousRouteCommunityPostPermissionOrEditCommunity = () => {
    const navigationState = navigation.getState();
    const routes = navigationState.routes;
    const currentIndex = navigationState.index;
    const previousRoute = currentIndex > 0 ? routes[currentIndex - 1] : null;
    return (previousRoute === null || previousRoute === void 0 ? void 0 : previousRoute.name) === 'CommunityPostPermission' || (previousRoute === null || previousRoute === void 0 ? void 0 : previousRoute.name) === 'EditCommunity';
  };
  return /*#__PURE__*/React.createElement(View, {
    style: styles.container
  }, /*#__PURE__*/React.createElement(Pressable, {
    style: styles.button,
    onPress: () => {
      if (isPreviousRouteCommunityPostPermissionOrEditCommunity()) {
        return navigation.pop(4);
      }
      const routes = navigation.getState().routes;
      if (pop === 2) {
        return navigation.pop(2);
      }
      if (isFromComponent && routes.length === 1) {
        navigation.navigate('AmitySocialHomePage');
        navigation.dispatch(CommonActions.reset({
          index: 0,
          routes: [{
            name: 'AmitySocialHomePage'
          }]
        }));
      } else {
        navigation.goBack();
      }
    }
  }, /*#__PURE__*/React.createElement(BackButtonIconElement, {
    pageID: pageId,
    componentID: componentId,
    style: styles.buttonIcon
  })), ((community === null || community === void 0 ? void 0 : community.isJoined) || canShare) && /*#__PURE__*/React.createElement(MenuButton, {
    pageId: pageId,
    variant: "filled",
    componentId: componentId,
    onPress: () => {
      openBottomSheet({
        height: bottomSheetHeight[actions.length],
        content: /*#__PURE__*/React.createElement(View, null, actions.map(({
          action
        }) => action))
      });
    }
  }));
}
//# sourceMappingURL=TopBar.js.map