import { useEnvironment } from "@contember/binding";
import { useMemo } from "react";
import { createBindingLinkParametersResolver } from "../../routing/binding/useBindingLinkParametersResolver.js";
import { useRoutingLinkFactory } from "../../routing/useRoutingLink.js";
import { useCurrentRequest } from "../../routing/RequestContext.js";
import { parseLinkTarget } from "../../routing/binding/LinkLanguage.js";
const useEntityRedirectOnPersistSuccess = (target) => {
  const linkFactory = useRoutingLinkFactory();
  const currentRequest = useCurrentRequest();
  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" ? target(currentRequest, entity.id, entity, options) : target;
      if (!linkTarget) {
        return;
      }
      const parsedTarget = parseLinkTarget(linkTarget, env);
      const parameters = {};
      const parametersResolver = createBindingLinkParametersResolver(entity);
      const link = linkFactory(parsedTarget, parameters, parametersResolver);
      link.navigate();
    };
  }, [currentRequest, env, linkFactory, target]);
};
export {
  useEntityRedirectOnPersistSuccess
};
//# sourceMappingURL=useEntityRedirectOnPersistSuccess.js.map
