"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useCommunitySetting = useCommunitySetting;
var _reactNative = require("react-native");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _BehaviourProvider = require("../../../../providers/BehaviourProvider");
var _native = require("@react-navigation/native");
var _reactQuery = require("@tanstack/react-query");
var _constants = require("../../../../../core/constants");
var _styles = require("../styles");
var _toastSlice = require("../../../../../core/stores/slices/toastSlice");
var _useCustomRankingGlobalFeed = require("../../../../hooks/useCustomRankingGlobalFeed");
var _useCommunityNotificationSettingsQuery = require("../../../../hooks/queries/useCommunityNotificationSettingsQuery");
function useCommunitySetting(community) {
  const {
    styles
  } = (0, _styles.useStyles)();
  const navigation = (0, _native.useNavigation)();
  const {
    AmityCommunitySettingPageBehavior
  } = (0, _BehaviourProvider.useBehaviour)();
  const {
    showToast
  } = (0, _toastSlice.useToast)();
  const {
    refresh,
    globalFeedPosts
  } = (0, _useCustomRankingGlobalFeed.useCustomRankingGlobalFeed)({
    enabled: false
  });
  const {
    data: notificationSettings
  } = (0, _useCommunityNotificationSettingsQuery.useCommunityNotificationSettingsQuery)({
    communityId: community.communityId
  });
  const {
    mutate: leaveCommunity
  } = (0, _reactQuery.useMutation)({
    mutationFn: async () => await _tsSdkReactNative.CommunityRepository.leaveCommunity(community.communityId),
    onSuccess: () => {
      navigation.goBack();
      showToast({
        message: 'Successfully left the group',
        type: 'success'
      });
      globalFeedPosts.length === 0 && setTimeout(() => refresh(), 3000);
    },
    onError: error => {
      if (error.message.includes(_constants.ERROR_CODE.ONLY_ONE_MODERATOR)) {
        _reactNative.Alert.alert('Unable to leave community', "You're the only moderator in this group. To leave community, nominate other members to moderator role.");
      } else {
        _reactNative.Alert.alert('Unable to leave community', 'Something went wrong. Please try again later');
      }
    }
  });
  const {
    mutate: closeCommunity
  } = (0, _reactQuery.useMutation)({
    mutationFn: async () => await _tsSdkReactNative.CommunityRepository.deleteCommunity(community.communityId),
    onSuccess: () => {
      navigation.replace('AmitySocialHomePage');
    },
    onError: () => {
      _reactNative.Alert.alert('Unable to close community', 'Something went wrong. Please try again later.');
    }
  });
  const handleCloseCommunity = async () => {
    _reactNative.Alert.alert('Close community?', 'All members will be removed from the community. All posts, messages, reactions, and media shared in community will be deleted. This cannot be undone.', [{
      text: 'Cancel',
      style: 'cancel'
    }, {
      text: 'Confirm',
      style: 'destructive',
      onPress: () => closeCommunity()
    }]);
  };
  const handleLeaveCommunity = () => {
    _reactNative.Alert.alert('Leave community', 'Leave the community. You will no longer be able to post and interact in this community.', [{
      text: 'Cancel',
      style: 'cancel'
    }, {
      text: 'Leave',
      onPress: () => leaveCommunity(),
      style: 'destructive'
    }]);
  };
  const handleEditCommunity = () => {
    if (AmityCommunitySettingPageBehavior.goToEditCommunityPage) {
      return AmityCommunitySettingPageBehavior.goToEditCommunityPage({
        community
      });
    }
    navigation.navigate('EditCommunity', {
      community
    });
  };
  const handleCommunityMembership = () => {
    if (AmityCommunitySettingPageBehavior.goToMembershipPage) {
      return AmityCommunitySettingPageBehavior.goToMembershipPage({
        community
      });
    }
    navigation.navigate('CommunityMembership', {
      community
    });
  };
  const handleCommunityPostPermission = () => {
    if (AmityCommunitySettingPageBehavior.goToPostPermissionPage) {
      return AmityCommunitySettingPageBehavior.goToPostPermissionPage({
        community
      });
    }
    navigation.navigate('CommunityPostPermission', {
      community
    });
  };
  const handleCommunityStorySetting = () => {
    if (AmityCommunitySettingPageBehavior.goToStorySettingPage) {
      return AmityCommunitySettingPageBehavior.goToStorySettingPage({
        community
      });
    }
    navigation.navigate('CommunityStorySetting', {
      community
    });
  };
  const isNetworkNotificationsEnabled = !notificationSettings || notificationSettings.events.length === 0 || notificationSettings.events.some(e => e.isNetworkEnabled !== false);
  const handleCommunityNotificationSetting = () => {
    if (AmityCommunitySettingPageBehavior.goToNotificationPage) {
      return AmityCommunitySettingPageBehavior.goToNotificationPage({
        community
      });
    }
    navigation.navigate('CommunityNotificationSetting', {
      community
    });
  };
  return {
    styles,
    handleCloseCommunity,
    handleLeaveCommunity,
    handleEditCommunity,
    handleCommunityMembership,
    handleCommunityPostPermission,
    handleCommunityStorySetting,
    handleCommunityNotificationSetting,
    isNotificationEnabled: (notificationSettings === null || notificationSettings === void 0 ? void 0 : notificationSettings.isEnabled) ?? false,
    isNetworkNotificationsEnabled
  };
}
//# sourceMappingURL=useCommunitySetting.js.map