"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const reactBinding = require("@contember/react-binding");
const react = require("react");
const useBindingLinkParametersResolver = require("../../routing/binding/useBindingLinkParametersResolver.cjs");
const useRoutingLink = require("../../routing/useRoutingLink.cjs");
const RequestContext = require("../../routing/RequestContext.cjs");
const LinkLanguage = require("../../routing/binding/LinkLanguage.cjs");
const useEntityRedirectOnPersistSuccess = (target) => {
  const linkFactory = useRoutingLink.useRoutingLinkFactory();
  const currentRequest = RequestContext.useCurrentRequest();
  const env = reactBinding.useEnvironment();
  return react.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 = LinkLanguage.parseLinkTarget(linkTarget, env);
      const parameters = {};
      const parametersResolver = useBindingLinkParametersResolver.createBindingLinkParametersResolver(entity);
      const link = linkFactory(parsedTarget, parameters, parametersResolver);
      link.navigate();
    };
  }, [currentRequest, env, linkFactory, target]);
};
exports.useEntityRedirectOnPersistSuccess = useEntityRedirectOnPersistSuccess;
//# sourceMappingURL=useEntityRedirectOnPersistSuccess.cjs.map
