import { CommunityPostSettings, CommunityRepository } from '@amityco/ts-sdk-react-native';
import { useEffect, useState } from 'react';
import { isModerator } from '../utils/permissions';
import useAuth from '../../core/hooks/useAuth';
import { MemberRoles } from '../../core/constants';
export function usePostPermission({
  community
}) {
  const {
    client
  } = useAuth();
  const [hasPostPermission, setHasPostPermission] = useState(false);
  const isOnlyAdminCanPost = (community === null || community === void 0 ? void 0 : community.postSetting) === CommunityPostSettings.ONLY_ADMIN_CAN_POST;
  useEffect(() => {
    if (!(community !== null && community !== void 0 && community.communityId) || !(client !== null && client !== void 0 && client.userId)) return;
    CommunityRepository.Membership.getMembers({
      communityId: community.communityId,
      limit: 20,
      roles: [MemberRoles.ADMIN, MemberRoles.COMMUNITY_MODERATOR]
    }, ({
      data
    }) => {
      var _data$find;
      const userRoles = (_data$find = data.find(member => member.userId === client.userId)) === null || _data$find === void 0 ? void 0 : _data$find.roles;
      setHasPostPermission(isOnlyAdminCanPost ? isModerator(userRoles) : !!(community !== null && community !== void 0 && community.isJoined));
    });
  }, [community === null || community === void 0 ? void 0 : community.communityId, client === null || client === void 0 ? void 0 : client.userId, isOnlyAdminCanPost, community === null || community === void 0 ? void 0 : community.isJoined]);
  return hasPostPermission;
}
//# sourceMappingURL=usePostPermission.js.map