"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isModerator = exports.isCommunityModerator = exports.isAdmin = void 0;
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _constants = require("../constants");
const ADMIN = 'global-admin';
const {
  COMMUNITY_MODERATOR,
  CHANNEL_MODERATOR,
  MODERATOR,
  SUPER_MODERATOR
} = _constants.MemberRoles;
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));
};
exports.isModerator = isModerator;
const isAdmin = userRoles => {
  if (!(userRoles !== null && userRoles !== void 0 && userRoles.length)) {
    return false;
  }
  return userRoles.includes(ADMIN);
};
exports.isAdmin = isAdmin;
const isCommunityModerator = async ({
  userId,
  communityId
}) => {
  if (!userId || !communityId) return false;
  try {
    var _data$;
    const {
      error,
      loading,
      data
    } = await new Promise(resolve => {
      _tsSdkReactNative.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;
  }
};
exports.isCommunityModerator = isCommunityModerator;
//# sourceMappingURL=permission.js.map