import { CommunityPostSettings, CommunityRepository } from '@amityco/ts-sdk-react-native';
export function getCommunityById(communityId) {
  const communityObject = new Promise((resolve, reject) => {
    let object;
    const unsubscribe = CommunityRepository.getCommunity(communityId, data => {
      if (data.error) {
        reject(data.error);
      }
      object = data;
    });
    resolve({
      data: object,
      unsubscribe
    });
  });
  return communityObject;
}
export function createCommunity(communityParam) {
  const communityObject = new Promise(async resolve => {
    const newCommunity = {
      description: communityParam.description,
      displayName: communityParam.displayName,
      isPublic: communityParam.isPublic,
      categoryIds: [communityParam.category],
      userIds: communityParam.userIds,
      postSetting: CommunityPostSettings.ANYONE_CAN_POST,
      avatarFileId: communityParam.avatarFileId.length > 0 ? communityParam.avatarFileId : undefined
    };
    const {
      data: community
    } = await CommunityRepository.createCommunity(newCommunity);
    resolve(community);
  });
  return communityObject;
}
export const updateCommunity = (communityId, communityParam) => {
  const communityObject = new Promise(async (resolve, reject) => {
    try {
      const newCommunity = {
        description: communityParam.description,
        displayName: communityParam.displayName,
        isPublic: communityParam.isPublic,
        categoryIds: [communityParam.category],
        avatarFileId: (communityParam === null || communityParam === void 0 ? void 0 : communityParam.avatarFileId) ?? undefined
      };
      const {
        data: community
      } = await CommunityRepository.updateCommunity(communityId, newCommunity);
      resolve(community);
    } catch (error) {
      reject(error);
    }
  });
  return communityObject;
};
export async function checkCommunityPermission(communityId, client, apiRegion) {
  var _client$token;
  const url = `https://api.${apiRegion}.amity.co/api/v3/communities/${communityId}/permissions/me`;
  const accessToken = client === null || client === void 0 || (_client$token = client.token) === null || _client$token === void 0 ? void 0 : _client$token.accessToken;
  try {
    const response = await fetch(url, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${accessToken}`
      }
    });
    const data = await response.json();
    return data;
  } catch (error) {
    console.log('error:', error);
  }
}
export const updateCommunityMember = ({
  operation,
  communityId,
  memberIds
}) => {
  const communityMembers = new Promise(async (resolve, reject) => {
    try {
      const updateMembers = operation === 'ADD' ? await CommunityRepository.Membership.addMembers(communityId, memberIds) : await CommunityRepository.Membership.removeMembers(communityId, memberIds);
      resolve(updateMembers);
    } catch (error) {
      reject(error);
    }
  });
  return communityMembers;
};
export const assignRolesToUsers = (communityId, roles, userIds) => {
  const assignUserRoles = new Promise(async (resolve, reject) => {
    try {
      const result = await CommunityRepository.Moderation.addRoles(communityId, roles, userIds);
      resolve(result);
    } catch (error) {
      reject(error);
    }
  });
  return assignUserRoles;
};
export const removeRolesFromUsers = (communityId, roles, userIds) => {
  const removeUserRoles = new Promise(async (resolve, reject) => {
    try {
      const result = await CommunityRepository.Moderation.removeRoles(communityId, roles, userIds);
      resolve(result);
    } catch (error) {
      reject(error);
    }
  });
  return removeUserRoles;
};
//# sourceMappingURL=community.js.map