import { useEnvironment } from "@contember/binding";
import { useMemo } from "react";
import { createBindingLinkParametersResolver } from "../../routing/binding/useBindingLinkParametersResolver.js";
import { useRoutingLinkFactory } from "../../routing/useRoutingLink.js";
import { parseLinkTarget } from "../../routing/binding/LinkLanguage.js";
const useEntityRedirectOnPersistSuccess = (target) => {
  const linkFactory = useRoutingLinkFactory();
  const env = useEnvironment();
  return useMemo(() => {
    if (!target) {
      return void 0;
    }
    return (getAccessor, options) => {
      if (options.successType === "nothingToPersist") {
        return;
      }
      const entity = getAccessor();
      const linkTarget = typeof target === "function" ? (request) => target(request, entity.id, entity, options) : target;
      const parsedTarget = parseLinkTarget(linkTarget, env);
      const parameters = {};
      const parametersResolver = createBindingLinkParametersResolver(entity);
      const link = linkFactory(parsedTarget, parameters, parametersResolver);
      link.navigate();
    };
  }, [env, linkFactory, target]);
};
export { useEntityRedirectOnPersistSuccess };
//# sourceMappingURL=useEntityRedirectOnPersistSuccess.js.map
