import { useMemo } from 'react';
import { TouchableOpacity, View } from 'react-native';
import { useNavigation } from '@react-navigation/native';
import Avatar from '../../../../../components/Avatar';
import { useStyles } from './style';
import { Typography } from '../../../../../../core/components/Typography/Typography';
import MenuButton from '../../../../../elements/MenuButton';
import useAuth from '../../../../../../core/hooks/useAuth';
import { useBottomSheet } from '../../../../../../core/stores/slices/bottomSheetSlice';
import MenuAction from '../../../../../elements/MenuAction';
import { MemberRoles } from '../../../../../../core/constants';
import { useToast } from '../../../../../../core/stores/slices/toastSlice';
import { demote, promote, report, trash, unreport } from '../../../../../../core/assets/icons';
import { checkEditRolePermission, isModerator } from '../../../../../utils/permissions';
import { useRolesQuery, useMembersQuery, useUserFlaggedByMeQuery } from '../../../../../hooks';
import { BrandBadge } from '../../../../../elements/BrandBadge';
function useMemberItem({
  member,
  communityId,
  refreshMembers
}) {
  const {
    styles
  } = useStyles();
  const {
    client
  } = useAuth();
  const {
    openBottomSheet,
    open,
    closeBottomSheet
  } = useBottomSheet();
  const {
    showToast
  } = useToast();
  const {
    addRoles,
    removeRoles
  } = useRolesQuery();
  const {
    removeMembers
  } = useMembersQuery();
  const {
    isFlaggedByMe,
    isLoading: isFlaggedByMeLoading,
    reportUser,
    unreportUser
  } = useUserFlaggedByMeQuery({
    userId: member.userId,
    enabled: open
  });
  const navigation = useNavigation();
  const addModeratorRoles = () => {
    addRoles({
      communityId,
      roles: [MemberRoles.COMMUNITY_MODERATOR, MemberRoles.CHANNEL_MODERATOR],
      userIds: [member.userId]
    }, {
      onSuccess: () => {
        refreshMembers === null || refreshMembers === void 0 || refreshMembers();
        showToast({
          type: 'success',
          message: 'Successfully promoted to moderator.'
        });
      },
      onError: () => {
        refreshMembers === null || refreshMembers === void 0 || refreshMembers();
        showToast({
          type: 'informative',
          message: 'Failed to promote member. Please try again.'
        });
      }
    });
  };
  const removeModeratorRoles = () => {
    removeRoles({
      communityId,
      roles: [MemberRoles.COMMUNITY_MODERATOR, MemberRoles.CHANNEL_MODERATOR],
      userIds: [member.userId]
    }, {
      onSuccess: () => {
        refreshMembers === null || refreshMembers === void 0 || refreshMembers();
        showToast({
          type: 'success',
          message: 'Successfully demoted to member.'
        });
      },
      onError: () => {
        refreshMembers === null || refreshMembers === void 0 || refreshMembers();
        showToast({
          type: 'informative',
          message: 'Failed to demote member. Please try again.'
        });
      }
    });
  };
  const flag = () => reportUser(member.userId);
  const unflag = () => unreportUser(member.userId);
  const removeMember = () => {
    removeMembers({
      communityId,
      userIds: [member.userId]
    }, {
      onSuccess: () => {
        refreshMembers === null || refreshMembers === void 0 || refreshMembers();
        showToast({
          type: 'success',
          message: 'Member removed from this community.'
        });
      },
      onError: () => {
        refreshMembers === null || refreshMembers === void 0 || refreshMembers();
        showToast({
          type: 'informative',
          message: 'Failed to remove member. Please try again.'
        });
      }
    });
  };
  const isCurrentUser = (client === null || client === void 0 ? void 0 : client.userId) === member.userId;
  const isModeratorUser = isModerator(member.roles);
  const isCurrentUserModerator = useMemo(() => checkEditRolePermission(communityId), [communityId]);
  const goToUserProfile = () => {
    navigation.navigate('UserProfile', {
      userId: member.userId
    });
  };
  return {
    styles,
    isCurrentUser,
    addModeratorRoles,
    removeModeratorRoles,
    openBottomSheet,
    closeBottomSheet,
    isFlaggedByMe,
    flag,
    unflag,
    removeMember,
    isModeratorUser,
    isCurrentUserModerator,
    isFlaggedByMeLoading,
    goToUserProfile
  };
}
function MemberItem({
  member,
  communityId,
  refreshMembers
}) {
  var _member$user, _member$user2, _member$user3, _member$user4;
  const {
    isCurrentUser,
    styles,
    openBottomSheet,
    closeBottomSheet,
    addModeratorRoles,
    removeModeratorRoles,
    flag,
    isFlaggedByMe,
    unflag,
    removeMember,
    isModeratorUser,
    isCurrentUserModerator,
    goToUserProfile
  } = useMemberItem({
    member,
    communityId,
    refreshMembers
  });
  return /*#__PURE__*/React.createElement(View, {
    style: styles.container
  }, /*#__PURE__*/React.createElement(TouchableOpacity, {
    style: styles.userContainer,
    onPress: goToUserProfile
  }, /*#__PURE__*/React.createElement(Avatar.User, {
    shouldRedirectToUserProfile: true,
    imageStyle: styles.userAvatar,
    uri: (_member$user = member.user) === null || _member$user === void 0 ? void 0 : _member$user.avatarCustomUrl,
    roles: member.roles,
    userName: ((_member$user2 = member.user) === null || _member$user2 === void 0 ? void 0 : _member$user2.displayName) ?? ((_member$user3 = member.user) === null || _member$user3 === void 0 ? void 0 : _member$user3.userId),
    userId: member.userId
  }), /*#__PURE__*/React.createElement(View, {
    style: styles.displayNameContainer
  }, /*#__PURE__*/React.createElement(Typography.BodyBold, {
    style: styles.userName,
    numberOfLines: 1,
    ellipsizeMode: "tail"
  }, member === null || member === void 0 ? void 0 : member.user.displayName), (member === null || member === void 0 || (_member$user4 = member.user) === null || _member$user4 === void 0 ? void 0 : _member$user4.isBrand) && /*#__PURE__*/React.createElement(BrandBadge, {
    width: 20,
    height: 20,
    accessible: true,
    accessibilityLabel: "Brand verified"
  }))), !isCurrentUser && /*#__PURE__*/React.createElement(MenuButton, {
    onPress: () => {
      openBottomSheet({
        height: isCurrentUserModerator ? 240 : 140,
        content: /*#__PURE__*/React.createElement(View, null, isCurrentUserModerator && /*#__PURE__*/React.createElement(React.Fragment, null, isModeratorUser ? /*#__PURE__*/React.createElement(MenuAction, {
          label: "Demote to member",
          iconProps: {
            xml: demote()
          },
          testID: "demote-to-member-button",
          onPress: () => {
            closeBottomSheet();
            removeModeratorRoles();
          }
        }) : /*#__PURE__*/React.createElement(MenuAction, {
          label: "Promote to moderator",
          iconProps: {
            xml: promote()
          },
          testID: "promote-to-moderator-button",
          onPress: () => {
            closeBottomSheet();
            addModeratorRoles();
          }
        })), isFlaggedByMe ? /*#__PURE__*/React.createElement(MenuAction, {
          label: "Unreport user",
          testID: "unreport-user-button",
          iconProps: {
            xml: unreport()
          },
          onPress: () => {
            closeBottomSheet();
            unflag();
          }
        }) : /*#__PURE__*/React.createElement(MenuAction, {
          label: "Report user",
          testID: "report-user-button",
          iconProps: {
            xml: report()
          },
          onPress: () => {
            closeBottomSheet();
            flag();
          }
        }), isCurrentUserModerator && /*#__PURE__*/React.createElement(MenuAction, {
          danger: true,
          iconProps: {
            xml: trash()
          },
          label: "Remove from community",
          testID: "remove-from-community-button",
          onPress: () => {
            closeBottomSheet();
            removeMember();
          }
        }))
      });
    }
  }));
}
export default MemberItem;
//# sourceMappingURL=MemberItem.js.map