"use strict";
const React = require("react");
const index_H1ljeG8 = require("./index--h1ljeG8-TLddt1Eo.js");
function _interopNamespace(e) {
  if (e && e.__esModule)
    return e;
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
  if (e) {
    for (const k in e) {
      if (k !== "default") {
        const d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: () => e[k]
        });
      }
    }
  }
  n.default = e;
  return Object.freeze(n);
}
const React__namespace = /* @__PURE__ */ _interopNamespace(React);
const useSyncRbac = (collectionTypeUID, query, containerName = "listView") => {
  const [isLoading, setIsLoading] = React__namespace.useState(true);
  const dispatch = index_H1ljeG8.useTypedDispatch();
  const userPermissions = index_H1ljeG8.useAuth("useSyncRbac", (state) => state.permissions);
  const relatedPermissions = React__namespace.useMemo(() => {
    const contentTypePermissions = userPermissions.filter(
      (permission) => permission.subject === collectionTypeUID
    );
    return contentTypePermissions.reduce(
      (acc, permission) => ({ ...acc, [permission.action]: [permission] }),
      {}
    );
  }, [collectionTypeUID, userPermissions]);
  React__namespace.useEffect(() => {
    setIsLoading(true);
    if (relatedPermissions) {
      dispatch(
        index_H1ljeG8.setPermissions({
          permissions: relatedPermissions,
          __meta__: {
            plugins: query ? query.plugins : void 0,
            containerName
          }
        })
      );
    }
    setIsLoading(false);
    return () => {
      dispatch(index_H1ljeG8.resetPermissions());
    };
  }, [relatedPermissions, dispatch, query, containerName]);
  const permissions = index_H1ljeG8.useTypedSelector((state) => state["content-manager"].rbac.permissions);
  const isPermissionMismatch = permissions && permissions.some((permission) => permission.subject !== collectionTypeUID) ? true : false;
  return {
    isLoading,
    isError: !isLoading && isPermissionMismatch,
    permissions
  };
};
exports.useSyncRbac = useSyncRbac;
//# sourceMappingURL=useSyncRbac-Od0wrAnD-_nmioRwh.js.map
