"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const reactBinding = require("@contember/react-binding");
const react = require("react");
const RoutingLink = require("../RoutingLink.cjs");
const RoutingParameter = require("../RoutingParameter.cjs");
const useRoutingLink = require("../useRoutingLink.cjs");
const LinkLanguage = require("./LinkLanguage.cjs");
const useBindingLinkParametersResolver = require("./useBindingLinkParametersResolver.cjs");
const Link = reactBinding.Component(({ to, ...props }) => {
  const parametersResolver = useBindingLinkParametersResolver.useBindingLinkParametersResolver();
  const env = reactBinding.useEnvironment();
  const desugaredTo = react.useMemo(() => {
    return LinkLanguage.parseLinkTarget(to, env);
  }, [to, env]);
  return /* @__PURE__ */ jsxRuntime.jsx(RoutingLink.RoutingLink, { parametersResolver, to: desugaredTo, ...props });
}, (props, env) => {
  const to = LinkLanguage.parseLinkTarget(props.to, env);
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
    createFieldsFromTarget(to).map((it, index) => /* @__PURE__ */ jsxRuntime.jsx(reactBinding.Field, { field: it }, `${index}-${it}`)),
    props.children
  ] });
});
Link.displayName = "Link";
const PageLink = Link;
const createFieldsFromTarget = (to) => {
  const dummyRequest = {
    dimensions: {},
    pageName: "",
    parameters: {}
  };
  const request = useRoutingLink.targetToRequest(to, dummyRequest);
  return collectDynamicParameters(request?.parameters ?? {}).filter((it) => it.startsWith(useBindingLinkParametersResolver.ROUTING_BINDING_PARAMETER_PREFIX)).map((it) => it.slice(useBindingLinkParametersResolver.ROUTING_BINDING_PARAMETER_PREFIX.length));
};
const collectDynamicParameters = (parameters) => {
  return Object.values(parameters).flatMap((it) => {
    if (it instanceof RoutingParameter.RoutingParameter) {
      return it.name;
    }
    if (typeof it === "object" && it !== null) {
      return collectDynamicParameters(it);
    }
    return null;
  }).filter((it) => it !== null);
};
exports.Link = Link;
exports.PageLink = PageLink;
//# sourceMappingURL=Link.cjs.map
