import { BindingError, useHasEntity, useEntity } from "@contember/binding";
import { useMemo } from "react";
import { PageNotFound } from "../urlMapper.js";
const ROUTING_BINDING_PARAMETER_PREFIX = "entity.";
const createBindingLinkParametersResolver = (entity) => {
  return (param) => {
    if (param.startsWith(ROUTING_BINDING_PARAMETER_PREFIX)) {
      if (!entity) {
        throw new BindingError(`Cannot use data binding routing parameters in non-databinding context`);
      }
      return entity.getField(param.slice(ROUTING_BINDING_PARAMETER_PREFIX.length)).value ?? void 0;
    }
    throw new PageNotFound(`Routing parameter ${param} not found`);
  };
};
const useBindingLinkParametersResolver = () => {
  const hasEntity = useHasEntity();
  const entity = hasEntity ? useEntity() : void 0;
  return useMemo(() => createBindingLinkParametersResolver(entity), [entity]);
};
export { ROUTING_BINDING_PARAMETER_PREFIX, createBindingLinkParametersResolver, useBindingLinkParametersResolver };
//# sourceMappingURL=useBindingLinkParametersResolver.js.map
