"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TopBar = TopBar;
var _reactNative = require("react-native");
var _BackButtonIconElement = _interopRequireDefault(require("../../../../elements/BackButtonIconElement/BackButtonIconElement"));
var _native = require("@react-navigation/native");
var _enums = require("../../../../enums");
var _styles = require("./styles");
var _hooks = require("../../../../hooks");
var _bottomSheetSlice = require("../../../../../core/stores/slices/bottomSheetSlice");
var _useShareableLink = require("../../../../../core/hooks/useShareableLink");
var _types = require("../../../../types");
var _MenuAction = _interopRequireDefault(require("../../../../elements/MenuAction/MenuAction"));
var _MenuButton = _interopRequireDefault(require("../../../../elements/MenuButton/MenuButton"));
var _icons = require("../../../../../core/assets/icons");
var _CopyLinkAction = require("../../../../elements/CopyLinkAction");
var _ShareAction = require("../../../../elements/ShareAction");
var _permissions = require("../../../../utils/permissions");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function TopBar({
  pageId = _enums.PageID.WildCardPage,
  componentId = _enums.ComponentID.WildCardComponent,
  communityId,
  isFromComponent
}) {
  const {
    styles
  } = (0, _styles.useStyles)();
  const {
    community
  } = (0, _hooks.useCommunity)(communityId);
  const {
    getShareLink
  } = (0, _useShareableLink.useShareableLink)();
  const {
    openBottomSheet,
    closeBottomSheet,
    bottomSheetHeight
  } = (0, _bottomSheetSlice.useBottomSheet)();
  const navigation = (0, _native.useNavigation)();
  const route = (0, _native.useRoute)();
  const {
    pop
  } = (route === null || route === void 0 ? void 0 : route.params) || {};
  const shareLink = community ? getShareLink(_types.ShareableLinkModel.communities, community.communityId) : null;
  const isModerator = (0, _permissions.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.default, {
      gap: "small",
      key: "settings",
      iconProps: {
        xml: (0, _icons.settings)()
      },
      label: "Community settings",
      onPress: () => {
        closeBottomSheet();
        navigation.navigate('CommunitySetting', {
          community
        });
      }
    })
  }, {
    show: !!isJoined && !isModerator,
    action: /*#__PURE__*/React.createElement(_MenuAction.default, {
      key: "info",
      gap: "small",
      iconProps: {
        xml: (0, _icons.people)()
      },
      label: "Community information",
      onPress: () => {
        closeBottomSheet();
        navigation.navigate('CommunitySetting', {
          community
        });
      }
    })
  }, {
    show: !!canShare,
    action: /*#__PURE__*/React.createElement(_CopyLinkAction.CopyLinkAction, {
      key: "copy",
      link: shareLink,
      pageId: _enums.PageID.community_profile_page
    })
  }, {
    show: !!canShare,
    action: /*#__PURE__*/React.createElement(_ShareAction.ShareAction, {
      key: "share",
      link: shareLink,
      pageId: _enums.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(_reactNative.View, {
    style: styles.container
  }, /*#__PURE__*/React.createElement(_reactNative.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(_native.CommonActions.reset({
          index: 0,
          routes: [{
            name: 'AmitySocialHomePage'
          }]
        }));
      } else {
        navigation.goBack();
      }
    }
  }, /*#__PURE__*/React.createElement(_BackButtonIconElement.default, {
    pageID: pageId,
    componentID: componentId,
    style: styles.buttonIcon
  })), ((community === null || community === void 0 ? void 0 : community.isJoined) || canShare) && /*#__PURE__*/React.createElement(_MenuButton.default, {
    pageId: pageId,
    variant: "filled",
    componentId: componentId,
    onPress: () => {
      openBottomSheet({
        height: bottomSheetHeight[actions.length],
        content: /*#__PURE__*/React.createElement(_reactNative.View, null, actions.map(({
          action
        }) => action))
      });
    }
  }));
}
//# sourceMappingURL=TopBar.js.map