"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _react = require("react");
var _native = require("@react-navigation/native");
var _hooks = require("../../../../hooks");
var _enumUIKitID = require("../../../../enums/enumUIKitID");
var _BehaviourProvider = require("../../../../providers/BehaviourProvider");
var _CreatePostMenu = _interopRequireDefault(require("../CreatePostMenu"));
var _TextKeyElement = _interopRequireDefault(require("../../../../elements/TextKeyElement/TextKeyElement"));
var _usePopup = require("../../../../hooks/usePopup");
var _PopupMenu = _interopRequireDefault(require("../../../../components/PopupMenu/PopupMenu"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const AmitySocialHomeTopNavigationComponent = ({
  activeTab
}) => {
  const pageId = _enumUIKitID.PageID.social_home_page;
  const componentId = _enumUIKitID.ComponentID.top_navigation;
  const componentConfig = (0, _hooks.useAmityComponent)({
    pageId,
    componentId
  });
  const theme = componentConfig.themeStyles;
  const {
    AmitySocialHomeTopNavigationComponentBehaviour
  } = (0, _BehaviourProvider.useBehaviour)();
  const {
    isOpen,
    setIsOpen,
    toggle
  } = (0, _usePopup.usePopup)();
  const [myCommunitiesTab] = (0, _hooks.useUiKitConfig)({
    page: _enumUIKitID.PageID.social_home_page,
    component: _enumUIKitID.ComponentID.WildCardComponent,
    element: _enumUIKitID.ElementID.my_communities_button,
    keys: ['text']
  });
  const [exploreTab] = (0, _hooks.useUiKitConfig)({
    page: _enumUIKitID.PageID.social_home_page,
    component: _enumUIKitID.ComponentID.WildCardComponent,
    element: _enumUIKitID.ElementID.explore_button,
    keys: ['text']
  });
  const searchIcon = (0, _hooks.useConfigImageUri)({
    configPath: {
      page: _enumUIKitID.PageID.social_home_page,
      component: _enumUIKitID.ComponentID.top_navigation,
      element: _enumUIKitID.ElementID.global_search_button
    },
    configKey: 'icon'
  });
  const createIcon = (0, _hooks.useConfigImageUri)({
    configPath: {
      page: _enumUIKitID.PageID.social_home_page,
      component: _enumUIKitID.ComponentID.top_navigation,
      element: _enumUIKitID.ElementID.post_creation_button
    },
    configKey: 'icon'
  });
  const navigation = (0, _native.useNavigation)();
  const styles = _reactNative.StyleSheet.create({
    headerContainer: {
      width: '100%',
      alignSelf: 'center',
      flexDirection: 'row',
      justifyContent: 'space-between',
      alignItems: 'center',
      paddingHorizontal: 24,
      paddingVertical: 8,
      marginVertical: 8,
      zIndex: 1,
      position: 'relative'
    },
    title: {
      fontWeight: 'bold',
      color: theme.colors.base,
      fontSize: 20
    },
    flexContainer: {
      flexDirection: 'row',
      alignItems: 'center'
    },
    iconBtn: {
      borderRadius: 50,
      backgroundColor: theme.colors.baseShade4,
      padding: 4,
      marginHorizontal: 4
    },
    icon: {
      width: 20,
      height: 20,
      tintColor: theme.colors.base
    }
  });
  (0, _native.useFocusEffect)((0, _react.useCallback)(() => {
    return () => setIsOpen(false);
  }, [setIsOpen]));
  const onPressSearch = (0, _react.useCallback)(() => {
    if (myCommunitiesTab === activeTab) {
      if (AmitySocialHomeTopNavigationComponentBehaviour.goToMyCommunitiesSearchPage) {
        return AmitySocialHomeTopNavigationComponentBehaviour.goToMyCommunitiesSearchPage();
      }
      return navigation.navigate('AmityMyCommunitiesSearchPage');
    }
    if (AmitySocialHomeTopNavigationComponentBehaviour.goToGlobalSearchPage) {
      return AmitySocialHomeTopNavigationComponentBehaviour.goToGlobalSearchPage();
    }
    navigation.navigate('AmitySocialGlobalSearchPage');
  }, [AmitySocialHomeTopNavigationComponentBehaviour, activeTab, myCommunitiesTab, navigation]);
  const onToggleCreateComponent = (0, _react.useCallback)(() => {
    toggle();
  }, [toggle]);
  const onCreateCommunity = (0, _react.useCallback)(() => {
    navigation.navigate('CreateCommunity');
  }, [navigation]);
  const onPressCreate = (0, _react.useCallback)(() => {
    if (AmitySocialHomeTopNavigationComponentBehaviour.onPressCreate) return AmitySocialHomeTopNavigationComponentBehaviour.onPressCreate();
    if (activeTab === myCommunitiesTab) return onCreateCommunity();
    return onToggleCreateComponent();
  }, [AmitySocialHomeTopNavigationComponentBehaviour, activeTab, myCommunitiesTab, onCreateCommunity, onToggleCreateComponent]);
  if (componentConfig !== null && componentConfig !== void 0 && componentConfig.isExcluded) return null;
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.headerContainer,
    testID: componentConfig.accessibilityId,
    accessibilityLabel: componentConfig.accessibilityId
  }, /*#__PURE__*/React.createElement(_TextKeyElement.default, {
    pageID: pageId,
    componentID: componentId,
    elementID: _enumUIKitID.ElementID.header_label,
    style: styles.title
  }), /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.flexContainer
  }, /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    style: styles.iconBtn,
    onPress: onPressSearch,
    testID: "top_navigation/global_search_button",
    accessibilityLabel: "top_navigation/global_search_button"
  }, /*#__PURE__*/React.createElement(_reactNative.Image, {
    source: searchIcon,
    style: styles.icon
  })), activeTab !== exploreTab && /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    style: styles.iconBtn,
    onPress: onPressCreate,
    testID: "top_navigation/post_creation_button",
    accessibilityLabel: "top_navigation/post_creation_button"
  }, /*#__PURE__*/React.createElement(_reactNative.Image, {
    source: createIcon,
    style: styles.icon
  }))), /*#__PURE__*/React.createElement(_PopupMenu.default, {
    setOpen: setIsOpen,
    open: isOpen,
    position: {
      top: 45,
      right: 15
    }
  }, /*#__PURE__*/React.createElement(_CreatePostMenu.default, {
    pageId: _enumUIKitID.PageID.social_home_page,
    componentId: _enumUIKitID.ComponentID.create_post_menu
  }))));
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(AmitySocialHomeTopNavigationComponent);
//# sourceMappingURL=TopNavigation.js.map