import * as React from "react";
import { ag as useTypedDispatch, a3 as useAuth, ah as setPermissions, ai as resetPermissions, a9 as useTypedSelector } from "./index--h1ljeG8-PYl4iNtw.mjs";
const useSyncRbac = (collectionTypeUID, query, containerName = "listView") => {
  const [isLoading, setIsLoading] = React.useState(true);
  const dispatch = useTypedDispatch();
  const userPermissions = useAuth("useSyncRbac", (state) => state.permissions);
  const relatedPermissions = React.useMemo(() => {
    const contentTypePermissions = userPermissions.filter(
      (permission) => permission.subject === collectionTypeUID
    );
    return contentTypePermissions.reduce(
      (acc, permission) => ({ ...acc, [permission.action]: [permission] }),
      {}
    );
  }, [collectionTypeUID, userPermissions]);
  React.useEffect(() => {
    setIsLoading(true);
    if (relatedPermissions) {
      dispatch(
        setPermissions({
          permissions: relatedPermissions,
          __meta__: {
            plugins: query ? query.plugins : void 0,
            containerName
          }
        })
      );
    }
    setIsLoading(false);
    return () => {
      dispatch(resetPermissions());
    };
  }, [relatedPermissions, dispatch, query, containerName]);
  const permissions = useTypedSelector((state) => state["content-manager"].rbac.permissions);
  const isPermissionMismatch = permissions && permissions.some((permission) => permission.subject !== collectionTypeUID) ? true : false;
  return {
    isLoading,
    isError: !isLoading && isPermissionMismatch,
    permissions
  };
};
export {
  useSyncRbac as u
};
//# sourceMappingURL=useSyncRbac-Od0wrAnD-NsSU7-VV.mjs.map
