"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _native = require("@react-navigation/native");
var _Avatar = _interopRequireDefault(require("../../../../../components/Avatar"));
var _style = require("./style");
var _Typography = require("../../../../../../core/components/Typography/Typography");
var _MenuButton = _interopRequireDefault(require("../../../../../elements/MenuButton"));
var _useAuth = _interopRequireDefault(require("../../../../../../core/hooks/useAuth"));
var _bottomSheetSlice = require("../../../../../../core/stores/slices/bottomSheetSlice");
var _MenuAction = _interopRequireDefault(require("../../../../../elements/MenuAction"));
var _constants = require("../../../../../../core/constants");
var _toastSlice = require("../../../../../../core/stores/slices/toastSlice");
var _icons = require("../../../../../../core/assets/icons");
var _permissions = require("../../../../../utils/permissions");
var _hooks = require("../../../../../hooks");
var _BrandBadge = require("../../../../../elements/BrandBadge");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function useMemberItem({
  member,
  communityId,
  refreshMembers
}) {
  const {
    styles
  } = (0, _style.useStyles)();
  const {
    client
  } = (0, _useAuth.default)();
  const {
    openBottomSheet,
    open,
    closeBottomSheet
  } = (0, _bottomSheetSlice.useBottomSheet)();
  const {
    showToast
  } = (0, _toastSlice.useToast)();
  const {
    addRoles,
    removeRoles
  } = (0, _hooks.useRolesQuery)();
  const {
    removeMembers
  } = (0, _hooks.useMembersQuery)();
  const {
    isFlaggedByMe,
    isLoading: isFlaggedByMeLoading,
    reportUser,
    unreportUser
  } = (0, _hooks.useUserFlaggedByMeQuery)({
    userId: member.userId,
    enabled: open
  });
  const navigation = (0, _native.useNavigation)();
  const addModeratorRoles = () => {
    addRoles({
      communityId,
      roles: [_constants.MemberRoles.COMMUNITY_MODERATOR, _constants.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: [_constants.MemberRoles.COMMUNITY_MODERATOR, _constants.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 = (0, _permissions.isModerator)(member.roles);
  const isCurrentUserModerator = (0, _react.useMemo)(() => (0, _permissions.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(_reactNative.View, {
    style: styles.container
  }, /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    style: styles.userContainer,
    onPress: goToUserProfile
  }, /*#__PURE__*/React.createElement(_Avatar.default.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(_reactNative.View, {
    style: styles.displayNameContainer
  }, /*#__PURE__*/React.createElement(_Typography.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.BrandBadge, {
    width: 20,
    height: 20,
    accessible: true,
    accessibilityLabel: "Brand verified"
  }))), !isCurrentUser && /*#__PURE__*/React.createElement(_MenuButton.default, {
    onPress: () => {
      openBottomSheet({
        height: isCurrentUserModerator ? 240 : 140,
        content: /*#__PURE__*/React.createElement(_reactNative.View, null, isCurrentUserModerator && /*#__PURE__*/React.createElement(React.Fragment, null, isModeratorUser ? /*#__PURE__*/React.createElement(_MenuAction.default, {
          label: "Demote to member",
          iconProps: {
            xml: (0, _icons.demote)()
          },
          testID: "demote-to-member-button",
          onPress: () => {
            closeBottomSheet();
            removeModeratorRoles();
          }
        }) : /*#__PURE__*/React.createElement(_MenuAction.default, {
          label: "Promote to moderator",
          iconProps: {
            xml: (0, _icons.promote)()
          },
          testID: "promote-to-moderator-button",
          onPress: () => {
            closeBottomSheet();
            addModeratorRoles();
          }
        })), isFlaggedByMe ? /*#__PURE__*/React.createElement(_MenuAction.default, {
          label: "Unreport user",
          testID: "unreport-user-button",
          iconProps: {
            xml: (0, _icons.unreport)()
          },
          onPress: () => {
            closeBottomSheet();
            unflag();
          }
        }) : /*#__PURE__*/React.createElement(_MenuAction.default, {
          label: "Report user",
          testID: "report-user-button",
          iconProps: {
            xml: (0, _icons.report)()
          },
          onPress: () => {
            closeBottomSheet();
            flag();
          }
        }), isCurrentUserModerator && /*#__PURE__*/React.createElement(_MenuAction.default, {
          danger: true,
          iconProps: {
            xml: (0, _icons.trash)()
          },
          label: "Remove from community",
          testID: "remove-from-community-button",
          onPress: () => {
            closeBottomSheet();
            removeMember();
          }
        }))
      });
    }
  }));
}
var _default = exports.default = MemberItem;
//# sourceMappingURL=MemberItem.js.map