"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactNative = require("react-native");
var _hooks = require("../../hooks");
var _TargetItem = _interopRequireDefault(require("./TargetItem/TargetItem"));
var _reactNativePaper = require("react-native-paper");
var _useAuth = _interopRequireDefault(require("../../../core/hooks/useAuth"));
var _CloseButtonIconElement = _interopRequireDefault(require("../../elements/CloseButtonIconElement/CloseButtonIconElement"));
var _enums = require("../../enums");
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _TextKeyElement = _interopRequireDefault(require("../../elements/TextKeyElement/TextKeyElement"));
var _Typography = require("../../../core/components/Typography/Typography");
var _styles = require("./styles");
var _Elements = require("../../features/feed/components/EmptyNewsFeed/Elements");
var _native = require("@react-navigation/native");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _usePostPermission = require("../../hooks/usePostPermission");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const TargetSelectionPage = ({
  pageId,
  hideMyTimelineTarget = false,
  onSelectFeed,
  onClickClose
}) => {
  const {
    client
  } = (0, _useAuth.default)();
  const defaultTheme = (0, _reactNativePaper.useTheme)();
  const user = (0, _hooks.useUser)(client === null || client === void 0 ? void 0 : client.userId);
  const {
    communities,
    onNextCommunityPage,
    loading
  } = (0, _hooks.useCommunities)();
  const {
    themeStyles,
    accessibilityId
  } = (0, _hooks.useAmityPage)({
    pageId
  });
  const navigation = (0, _native.useNavigation)();
  const theme = themeStyles || defaultTheme;
  const styles = (0, _styles.useStyles)(theme);
  const {
    config: myTimelineConfig
  } = (0, _hooks.useAmityElement)({
    pageId,
    componentId: _enums.ComponentID.WildCardComponent,
    elementId: _enums.ElementID.my_timeline_text
  });
  const renderItem = ({
    item
  }) => {
    const target = /*#__PURE__*/_react.default.createElement(_TargetItem.default, {
      key: item.communityId,
      displayName: item.displayName,
      isBadgeShow: item.isOfficial,
      isPrivate: !item.isPublic,
      onSelect: () => onSelectFeed({
        targetId: item.communityId,
        targetName: item.displayName,
        targetType: 'community',
        community: item,
        postSetting: item.postSetting,
        isPublic: item.isPublic
      }),
      avatarElementId: _enums.ElementID.community_avatar,
      avatarFileId: item.avatarFileId
    });
    if ((item === null || item === void 0 ? void 0 : item.postSetting) === _tsSdkReactNative.CommunityPostSettings.ONLY_ADMIN_CAN_POST) {
      return /*#__PURE__*/_react.default.createElement(AdminOnlyCommunity, {
        community: item
      }, target);
    }
    return target;
  };
  return /*#__PURE__*/_react.default.createElement(_reactNativeSafeAreaContext.SafeAreaView, {
    style: styles.container,
    testID: accessibilityId,
    accessibilityLabel: accessibilityId
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.header
  }, /*#__PURE__*/_react.default.createElement(_reactNative.TouchableOpacity, {
    style: styles.closeButton,
    onPress: () => {
      onClickClose === null || onClickClose === void 0 || onClickClose();
      if (navigation.canGoBack()) {
        navigation.goBack();
      }
    }
  }, /*#__PURE__*/_react.default.createElement(_CloseButtonIconElement.default, {
    style: styles.closeIcon,
    pageID: pageId
  })), /*#__PURE__*/_react.default.createElement(_TextKeyElement.default, {
    style: styles.title,
    pageID: pageId,
    componentID: _enums.ComponentID.WildCardComponent,
    elementID: _enums.ElementID.title
  })), !hideMyTimelineTarget && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_TargetItem.default, {
    pageId: pageId,
    displayNameElementId: _enums.ElementID.my_timeline_text,
    displayName: (myTimelineConfig === null || myTimelineConfig === void 0 ? void 0 : myTimelineConfig.text) || 'My timeline',
    avatarElementId: _enums.ElementID.my_timeline_avatar,
    onSelect: () => user && onSelectFeed({
      targetId: user === null || user === void 0 ? void 0 : user.userId,
      targetName: (myTimelineConfig === null || myTimelineConfig === void 0 ? void 0 : myTimelineConfig.text) || 'My timeline',
      targetType: 'user'
    }),
    avatarCustomUrl: user === null || user === void 0 ? void 0 : user.avatarCustomUrl,
    avatarFileId: user === null || user === void 0 ? void 0 : user.avatarFileId
  }), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.divider
  }, /*#__PURE__*/_react.default.createElement(_reactNativePaper.Divider, {
    theme: {
      colors: {
        outlineVariant: theme.colors.baseShade4
      }
    }
  })), /*#__PURE__*/_react.default.createElement(_Typography.Typography.Body, {
    style: styles.communityHeader
  }, "My communities")), !loading && ((communities === null || communities === void 0 ? void 0 : communities.length) === 0 || !communities) && /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.noCommunityContainer
  }, /*#__PURE__*/_react.default.createElement(_Elements.Illustration, null), /*#__PURE__*/_react.default.createElement(_Typography.Typography.TitleBold, {
    style: styles.noCommunityTitle
  }, "You haven't joined any communities yet.")), /*#__PURE__*/_react.default.createElement(_reactNative.FlatList, {
    data: communities,
    renderItem: renderItem,
    onEndReached: onNextCommunityPage,
    keyExtractor: item => item.communityId.toString()
  }));
};
var _default = exports.default = /*#__PURE__*/_react.default.memo(TargetSelectionPage);
function AdminOnlyCommunity({
  community,
  children
}) {
  const hasPostPermission = (0, _usePostPermission.usePostPermission)({
    community
  });
  return hasPostPermission ? children : null;
}
//# sourceMappingURL=TargetSelectionPage.js.map