import { CommunityRepository } from '@amityco/ts-sdk-react-native';
import { MemberRoles } from '../constants';
const ADMIN = 'global-admin';
const {
  COMMUNITY_MODERATOR,
  CHANNEL_MODERATOR,
  MODERATOR,
  SUPER_MODERATOR
} = MemberRoles;
export const isModerator = userRoles => {
  if (!(userRoles !== null && userRoles !== void 0 && userRoles.length)) {
    return false;
  }
  const roles = [COMMUNITY_MODERATOR, CHANNEL_MODERATOR, MODERATOR, SUPER_MODERATOR];
  return userRoles.some(role => roles.includes(role));
};
export const isAdmin = userRoles => {
  if (!(userRoles !== null && userRoles !== void 0 && userRoles.length)) {
    return false;
  }
  return userRoles.includes(ADMIN);
};
export const isCommunityModerator = async ({
  userId,
  communityId
}) => {
  if (!userId || !communityId) return false;
  try {
    var _data$;
    const {
      error,
      loading,
      data
    } = await new Promise(resolve => {
      CommunityRepository.Membership.searchMembers({
        communityId: communityId,
        search: userId,
        limit: 1,
        sortBy: 'firstCreated',
        memberships: ['member'],
        includeDeleted: false
      }, result => resolve(result));
    });
    if (error || loading || !data) return false;
    const userRoles = ((_data$ = data[0]) === null || _data$ === void 0 ? void 0 : _data$.roles) ?? [];
    return isModerator(userRoles);
  } catch (error) {
    console.error('Error checking community moderator:', error);
    return false;
  }
};
//# sourceMappingURL=permission.js.map