"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const react = require("react");
const RequestContext = require("./RequestContext.cjs");
const RoutingContext = require("./RoutingContext.cjs");
const RoutingParameter = require("./RoutingParameter.cjs");
const urlMapper = require("./urlMapper.cjs");
const isRoutingLinkTarget = (value) => {
  return typeof value === "string" || typeof value === "function" || typeof value === "object" && value !== null && "pageName" in value;
};
const targetToRequest = (target, currentRequest) => {
  if (typeof target === "function") {
    target = target(currentRequest);
  }
  if (typeof target === "string") {
    return RequestContext.requestChangeFactory(target, {})(currentRequest);
  }
  if (target === null) {
    return null;
  }
  return target;
};
const resolveParameters = (parameters, resolveParameter) => {
  return Object.fromEntries(Object.entries(parameters).map(([name, value]) => {
    if (value instanceof RoutingParameter.RoutingParameter) {
      return [name, resolveParameter(value.name)];
    }
    return [name, value];
  }));
};
const useRoutingLinkFactory = () => {
  const currentRequest = RequestContext.useCurrentRequest();
  const routing = RoutingContext.useRouting();
  const pushRequest = RequestContext.usePushRequest();
  return react.useCallback((target, parameters, parametersResolver) => {
    const tmpRequest = targetToRequest(target, currentRequest);
    const request = tmpRequest === null ? null : {
      pageName: tmpRequest.pageName,
      dimensions: tmpRequest.dimensions ?? currentRequest?.dimensions ?? {},
      parameters: resolveParameters(tmpRequest.parameters ?? {}, (param) => {
        if (param.startsWith("request.")) {
          return currentRequest?.parameters[param.slice("request.".length)];
        }
        if (parameters && param in parameters) {
          return parameters[param];
        }
        if (!parametersResolver) {
          throw new urlMapper.PageNotFound(`Routing parameter ${param} not found`);
        }
        return parametersResolver?.(param);
      })
    };
    let href;
    try {
      href = urlMapper.requestStateToPath(routing, request);
    } catch (e) {
      {
        throw e;
      }
    }
    return {
      href,
      isActive: window.location.pathname === href,
      // todo better active detection
      navigate: (e) => {
        e?.preventDefault();
        pushRequest(request);
      }
    };
  }, [currentRequest, pushRequest, routing]);
};
const useRoutingLink = (target, parametersResolver, parameters) => {
  const linkFactory = useRoutingLinkFactory();
  return react.useMemo(() => {
    return linkFactory(target, parameters, parametersResolver);
  }, [linkFactory, parameters, parametersResolver, target]);
};
exports.isRoutingLinkTarget = isRoutingLinkTarget;
exports.targetToRequest = targetToRequest;
exports.useRoutingLink = useRoutingLink;
exports.useRoutingLinkFactory = useRoutingLinkFactory;
//# sourceMappingURL=useRoutingLink.cjs.map
