import { j as jsxRuntimeExports } from "../../../../../../_virtual/jsx-runtime.js";
import get from "../../../../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/get.js";
import { IoIosHelpCircleOutline } from "../../../../../../node_modules/.pnpm/react-icons@5.5.0_react@19.1.1/node_modules/react-icons/io/index.js";
import ArrayField from "./fields/array.js";
import TagsField from "./fields/tags.js";
import BackgroundField from "./fields/background.js";
import BorderField from "./fields/border.js";
import AlignSelfField from "./fields/align-self.js";
import AlignItemsField from "./fields/align-items.js";
import JustifyContentField from "./fields/justify-content.js";
import ColorField from "./fields/color.js";
import DatasourceField from "./fields/datasource.js";
import DynamicField from "./fields/loop.js";
import QueryField from "./fields/page-queries.js";
import SiteQueriesField from "./fields/site-queries.js";
import EnumField from "./fields/enum.js";
import IconifyField from "./fields/iconify.js";
import ImageField from "./fields/image.js";
import { NumberField, SliderField } from "./fields/number.js";
import { PagePaddingField } from "./fields/padding.js";
import { StringField, UrlOrPageIdField, PathField, GeoAddressField } from "./fields/string.js";
import SwitchField from "./fields/switch.js";
import { d as b } from "../../../../../sdk/dist/shared/chunk-QGYZ7CG5.js";
import "../../../../../sdk/dist/shared/chunk-AFVJZPLS.js";
import "@sinclair/typebox";
import "../../../../../sdk/dist/shared/chunk-A3GF4PYW.js";
import "../../../../../sdk/dist/shared/chunk-W33JMRRN.js";
import "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 ColorPresetField from "./fields/color-preset.js";
import { CssLengthField } from "./fields/css-length.js";
import { DatarecordField } from "./fields/datarecord.js";
import { fieldLabel } from "./form-class.js";
import { tx as D } from "../../../../../style-system/dist/twind.js";
import { usePageAttributes } from "../../../../../../use-page-data.js";
import { Tooltip as e } 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/tooltip.js";
function getCommonFieldProps(options, fieldSchema) {
  const { brickId, fieldName, formSchema, formData } = options;
  return {
    brickId,
    schema: fieldSchema,
    formSchema,
    formData,
    required: formSchema.properties.required?.includes(fieldName) ?? false,
    title: fieldSchema.title ?? fieldSchema["ui:title"],
    description: fieldSchema.description ?? fieldSchema["ui:description"],
    placeholder: fieldSchema["ui:placeholder"],
    noDynamic: options.noDynamic ?? false
  };
}
function createFieldComponent(options) {
  const { brickId, fieldName, fieldSchema, formSchema, formData, id, onChange, noDynamic } = options;
  const schema = b(fieldSchema);
  const commonProps = getCommonFieldProps(
    {
      brickId,
      fieldName,
      formSchema,
      formData,
      noDynamic
    },
    schema
  );
  const fieldType = schema["ui:field"] ?? (schema.enum ? "enum" : null) ?? schema.type ?? (schema.anyOf ? "anyOf" : "");
  switch (fieldType) {
    case "hidden":
      return null;
    case "border": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        BorderField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "tags": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        TagsField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "color": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        ColorField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "color-preset": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        ColorPresetField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "datarecord": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        DatarecordField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "geoaddress": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        GeoAddressField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "datasource": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        DatasourceField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "loop": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        DynamicField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "page-queries": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        QueryField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "site-queries": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        SiteQueriesField,
        {
          currentValue,
          onChange: (value) => {
          },
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "padding": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        PagePaddingField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "background": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        BackgroundField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "align-self": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        AlignSelfField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "align-items": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        AlignItemsField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "justify-content": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        JustifyContentField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "enum": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        EnumField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "image": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        ImageField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "path": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        PathField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "url-page-id": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        UrlOrPageIdField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "css-length": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        CssLengthField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "slider": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        SliderField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "boolean":
    case "dynamic-content-switch":
    case "switch": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        SwitchField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "string": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        StringField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "integer":
    case "number": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        NumberField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "anyOf": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        EnumField,
        {
          currentValue,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps,
          options: schema.anyOf
        },
        `field-${id}`
      );
    }
    case "array": {
      const rawValue = get(formData, id);
      const currentValue = rawValue !== void 0 ? Array.isArray(rawValue) ? rawValue : [] : commonProps.schema.default || [];
      if (schema.items) {
        const itemSchema = b(schema.items);
        if (itemSchema.type === "object") {
          return /* @__PURE__ */ jsxRuntimeExports.jsx(
            ArrayField,
            {
              currentValue,
              onChange: (newArray) => onChange({ [id]: newArray || [] }, id),
              itemSchema,
              fieldName,
              id,
              parents: options.parents,
              ...commonProps
            },
            `field-${id}`
          );
        }
        if (itemSchema.anyOf || itemSchema.oneOf || itemSchema.enum || itemSchema.union) {
          const options2 = itemSchema.anyOf || itemSchema.oneOf || itemSchema.enum || itemSchema.union || [];
          return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "space-y-2", children: [
            commonProps.title && /* @__PURE__ */ jsxRuntimeExports.jsx("label", { className: "block text-sm font-medium text-gray-700 mb-1", children: commonProps.title }),
            /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "flex flex-wrap gap-1", children: options2.map((option) => {
              const value = typeof option === "string" ? option : option.const;
              const title = typeof option === "string" ? value : option.title || option.const;
              const isSelected = currentValue.includes(value);
              return /* @__PURE__ */ jsxRuntimeExports.jsx(
                "button",
                {
                  type: "button",
                  className: `px-2 py-1 text-xs rounded border transition-colors ${isSelected ? "bg-upstart-500 text-white border-upstart-500" : "bg-gray-100 text-gray-700 border-gray-300 hover:bg-gray-200"}`,
                  onClick: () => {
                    const newValue = isSelected ? currentValue.filter((v) => v !== value) : [...currentValue, value];
                    onChange({ [id]: newValue }, id);
                  },
                  children: title
                },
                value
              );
            }) })
          ] }, `field-${id}`);
        }
        return /* @__PURE__ */ jsxRuntimeExports.jsx(
          ArrayField,
          {
            currentValue,
            onChange: (newArray) => onChange({ [id]: newArray || [] }, id),
            itemSchema,
            fieldName,
            id,
            parents: options.parents,
            ...commonProps
          },
          `field-${id}`
        );
      }
      console.warn("Array field without items schema:", fieldName, schema);
      return null;
    }
    case "iconify": {
      const currentValue = get(formData, id) ?? commonProps.schema.default;
      const defaultCollection = commonProps.schema["ui:default-icon-collection"];
      return /* @__PURE__ */ jsxRuntimeExports.jsx(
        IconifyField,
        {
          currentValue,
          defaultCollection,
          onChange: (value) => onChange({ [id]: value }, id),
          ...commonProps
        },
        `field-${id}`
      );
    }
    case "Function":
      return null;
    // case "object": {
    //   console.log("Unknown schema", schema.pro);
    //   const tmpSchema = schema as TObject;
    //   return Object.entries(tmpSchema.properties).map(([keyBy, prop]) => {
    //     return createFieldComponent({
    //       ...options,
    //       id: `field-${options.id}-$`,
    //       fieldName: prop.title ?? "FOOOO",
    //       fieldSchema: {
    //         ...prop,
    //         "ui:group": tmpSchema["ui:group"],
    //       },
    //     });
    //   });
    // }
    default:
      console.log("!!! Unknown field type: %s", fieldType);
      console.log("Field schema", schema);
      return null;
  }
}
function ObjectFields({
  schema,
  formData,
  formSchema,
  onChange,
  options,
  noDynamic
}) {
  const pageAttributes = usePageAttributes();
  const { brickId, filter, parents = [] } = options;
  const fields = [];
  Object.entries(schema.properties).forEach(([fieldName, fieldSchema]) => {
    const field = b(fieldSchema);
    if (filter && field.type !== "object" && !filter(field)) {
      console.warn("ObjectFields: Ignoring field", field);
      return;
    }
    if (field.metadata?.filter) {
      const filter2 = field.metadata.filter;
      if (!filter2(formSchema, formData, pageAttributes)) {
        return;
      }
    }
    const isArrayItem = parents.length > 0 && parents[parents.length - 1].includes("[");
    const id = !isArrayItem && parents.length > 0 ? `${parents.join(".")}.${fieldName}` : fieldName;
    const fieldComponent = createFieldComponent({
      brickId,
      fieldName,
      fieldSchema: field,
      formSchema: schema,
      formData,
      id,
      onChange,
      parents,
      noDynamic
    });
    if (fieldComponent) {
      fields.push(fieldComponent);
    }
  });
  return fields;
}
function FieldTitle({
  title,
  description,
  className,
  containerClassName,
  htmlFor,
  withIcon = true
}) {
  if (!title) return null;
  return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: D("flex items-center text-nowrap basis-[45%]", containerClassName), children: description ? withIcon ? /* @__PURE__ */ jsxRuntimeExports.jsx(
    "label",
    {
      className: D(
        className,
        fieldLabel,
        " underline-offset-4 no-underline decoration-upstart-300 decoration-dotted cursor-default"
      ),
      htmlFor,
      children: /* @__PURE__ */ jsxRuntimeExports.jsx(
        e,
        {
          content: /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "block text-sm p-1", children: description }),
          className: "!z-[10000]",
          align: "start",
          children: /* @__PURE__ */ jsxRuntimeExports.jsxs("span", { className: "cursor-help flex items-center", children: [
            title,
            /* @__PURE__ */ jsxRuntimeExports.jsx(IoIosHelpCircleOutline, { className: "ml-1 text-[110%] opacity-50" })
          ] })
        }
      )
    }
  ) : /* @__PURE__ */ jsxRuntimeExports.jsx(
    e,
    {
      content: /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: "block text-sm p-1", children: description }),
      className: "!z-[10000]",
      align: "start",
      children: /* @__PURE__ */ jsxRuntimeExports.jsx(
        "label",
        {
          className: D(
            className,
            fieldLabel,
            "underline-offset-4 no-underline hover:underline decoration-upstart-300 decoration-dotted cursor-default"
          ),
          htmlFor,
          children: title
        }
      )
    }
  ) : /* @__PURE__ */ jsxRuntimeExports.jsx("label", { htmlFor, className: D(className, fieldLabel), children: title }) });
}
export {
  FieldTitle,
  ObjectFields as default
};
//# sourceMappingURL=field-factory.js.map
