import { j as jsxRuntimeExports } from "../../../../../../../_virtual/jsx-runtime.js";
import { useState } from "react";
import "../../../../../../../node_modules/.pnpm/@formkit_auto-animate@0.8.2/node_modules/@formkit/auto-animate/index.js";
import "../../../../../../../node_modules/.pnpm/react-hot-toast@2.5.2_react-dom@19.1.1_react@19.1.1__react@19.1.1/node_modules/react-hot-toast/dist/index.js";
import { FieldTitle } from "../field-factory.js";
import { usePageQueries } from "../../../../../../../use-page-data.js";
import { Switch as i } from "../../../../../../../node_modules/.pnpm/@radix-ui_themes@3.2.1_@types_react-dom@19.1.9_@types_react@19.1.13__@types_react@19.1._832fa1fef691e6e9510ada03948dc85c/node_modules/@radix-ui/themes/dist/esm/components/switch.js";
import { Root as C, Trigger as u, Content as g, Item as v } from "../../../../../../../node_modules/.pnpm/@radix-ui_themes@3.2.1_@types_react-dom@19.1.9_@types_react@19.1.13__@types_react@19.1._832fa1fef691e6e9510ada03948dc85c/node_modules/@radix-ui/themes/dist/esm/components/select.js";
import { Root as u$1 } from "../../../../../../../node_modules/.pnpm/@radix-ui_themes@3.2.1_@types_react-dom@19.1.9_@types_react@19.1.13__@types_react@19.1._832fa1fef691e6e9510ada03948dc85c/node_modules/@radix-ui/themes/dist/esm/components/text-field.js";
const NOLOOP = "$NOLOOP$";
const DynamicField = (props) => {
  const { currentValue, onChange, schema, title, description } = props;
  const { over = NOLOOP } = currentValue ?? {};
  const [overrideLimit, setOverrideLimit] = useState(!!currentValue?.overrideLimit);
  const pageQueries = usePageQueries();
  const query = pageQueries.find((q) => q.alias === over);
  const placeholder = schema["ui:placeholder"] ?? "Do not repeat";
  const onQueryChange = (newVal) => {
    if (newVal === NOLOOP) {
      onChange(null);
    } else {
      onChange({
        ...currentValue ?? {},
        over: newVal
      });
    }
  };
  if (pageQueries.length === 0) {
    return null;
  }
  const loopableQueries = pageQueries.filter((q) => !q.queryInfo?.limit || q.queryInfo?.limit > 1);
  return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "layout-field w-full flex flex-col gap-3", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex items-center justify-between pr-1", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(
        FieldTitle,
        {
          title: title ?? "Repeat over",
          description: description ?? "repeat the brick for each item returned by the query. If no query is selected, the brick will not repeat.",
          containerClassName: "!basis-1/4"
        }
      ),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(C, { defaultValue: over, size: "2", onValueChange: onQueryChange, children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          u,
          {
            radius: "medium",
            variant: "ghost",
            className: "!basis-2/3 !text-[92%] !max-w-fit",
            placeholder
          }
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsxs(g, { position: "popper", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(v, { value: NOLOOP, children: placeholder }),
          loopableQueries.map((query2) => /* @__PURE__ */ jsxRuntimeExports.jsxs(v, { value: query2.alias, children: [
            query2.alias,
            " - ",
            query2.queryInfo?.label
          ] }, query2.alias))
        ] })
      ] })
    ] }),
    typeof currentValue?.over === "string" && /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex items-center justify-between", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(
        FieldTitle,
        {
          title: "Override limit",
          description: "Override the number of items to repeat over. It can only be inferior to the number of items (limit) set by the query."
        }
      ),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex items-center gap-1.5", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          i,
          {
            size: "2",
            defaultChecked: overrideLimit,
            onCheckedChange: (checked) => {
              setOverrideLimit(checked);
              if (!checked) {
                onChange({
                  ...currentValue ?? {},
                  overrideLimit: void 0
                });
              }
            },
            className: "mr-2"
          }
        ),
        /* @__PURE__ */ jsxRuntimeExports.jsx(
          u$1,
          {
            disabled: !overrideLimit,
            size: "2",
            type: "number",
            min: "1",
            max: query?.queryInfo?.limit ?? 50,
            defaultValue: currentValue?.overrideLimit ?? query?.queryInfo?.limit ?? 10,
            onChange: (e) => {
              const value = e.target.value ? parseInt(e.target.value, 10) : void 0;
              onChange({
                ...currentValue ?? {},
                overrideLimit: value
              });
            },
            className: "w-[34px]"
          }
        )
      ] })
    ] })
  ] });
};
export {
  DynamicField as default
};
//# sourceMappingURL=loop.js.map
