"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.usePostPermission = usePostPermission;
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _react = require("react");
var _permissions = require("../utils/permissions");
var _useAuth = _interopRequireDefault(require("../../core/hooks/useAuth"));
var _constants = require("../../core/constants");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function usePostPermission({
  community
}) {
  const {
    client
  } = (0, _useAuth.default)();
  const [hasPostPermission, setHasPostPermission] = (0, _react.useState)(false);
  const isOnlyAdminCanPost = (community === null || community === void 0 ? void 0 : community.postSetting) === _tsSdkReactNative.CommunityPostSettings.ONLY_ADMIN_CAN_POST;
  (0, _react.useEffect)(() => {
    if (!(community !== null && community !== void 0 && community.communityId) || !(client !== null && client !== void 0 && client.userId)) return;
    _tsSdkReactNative.CommunityRepository.Membership.getMembers({
      communityId: community.communityId,
      limit: 20,
      roles: [_constants.MemberRoles.ADMIN, _constants.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 ? (0, _permissions.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