import { jsxDEV } from "react/jsx-dev-runtime";
import { Component, Field } from "@contember/binding";
import { resolveSortableBy, resolveConnectingEntity, getHumanFriendlyField } from "./utils.js";
import { AutoFields } from "./AutoFields.js";
import { AutoLabel } from "./AutoLabel.js";
import { FieldContainer } from "@contember/ui";
import { FloatField } from "../bindingFacade/fields/FloatField.js";
import { MultiSelectField } from "../bindingFacade/fields/SelectField/MultiSelectField.js";
import { TextField } from "../bindingFacade/fields/TextField.js";
import { TextareaField } from "../bindingFacade/fields/TextareaField.js";
import { CheckboxField } from "../bindingFacade/fields/CheckboxField.js";
import { NumberField } from "../bindingFacade/fields/NumberField.js";
import { DateField } from "../bindingFacade/fields/DateField.js";
import { DateTimeField } from "../bindingFacade/fields/DateTimeField.js";
import { SelectField } from "../bindingFacade/fields/SelectField/SelectField.js";
const AutoField = Component(
  ({ schema, entityName, fieldName, createEditLink, excludedEntities }) => {
    const field = schema.getEntityField(entityName, fieldName);
    if (field.__typename === "_Column") {
      const common = {
        field: field.name,
        label: field.name,
        required: !field.nullable,
        defaultValue: field.defaultValue
      };
      if (field.name === "id") {
        return /* @__PURE__ */ jsxDEV(TextField, { ...common, readOnly: true }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
          lineNumber: 33,
          columnNumber: 12
        }, globalThis);
      } else if (field.type === "String") {
        return /* @__PURE__ */ jsxDEV(TextareaField, { ...common, minRows: 1 }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
          lineNumber: 36,
          columnNumber: 12
        }, globalThis);
      } else if (field.type === "Uuid") {
        return /* @__PURE__ */ jsxDEV(TextField, { ...common }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
          lineNumber: 39,
          columnNumber: 12
        }, globalThis);
      } else if (field.type === "Bool") {
        return /* @__PURE__ */ jsxDEV(CheckboxField, { ...common }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
          lineNumber: 42,
          columnNumber: 12
        }, globalThis);
      } else if (field.type === "Integer") {
        return /* @__PURE__ */ jsxDEV(NumberField, { ...common }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
          lineNumber: 45,
          columnNumber: 12
        }, globalThis);
      } else if (field.type === "Double") {
        return /* @__PURE__ */ jsxDEV(FloatField, { ...common }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
          lineNumber: 48,
          columnNumber: 12
        }, globalThis);
      } else if (field.type === "Date") {
        return /* @__PURE__ */ jsxDEV(DateField, { ...common }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
          lineNumber: 51,
          columnNumber: 12
        }, globalThis);
      } else if (field.type === "DateTime") {
        return /* @__PURE__ */ jsxDEV(DateTimeField, { ...common }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
          lineNumber: 54,
          columnNumber: 12
        }, globalThis);
      } else if (field.type === "Enum") {
        const enumValues = schema.getEnumValues(field.enumName);
        const options = enumValues.map((it) => ({ value: it, label: it }));
        return /* @__PURE__ */ jsxDEV(SelectField, { ...common, options, allowNull: field.nullable }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
          lineNumber: 59,
          columnNumber: 12
        }, globalThis);
      } else if (field.type === "Json") {
        return /* @__PURE__ */ jsxDEV(FieldContainer, { ...common, children: /* @__PURE__ */ jsxDEV(Field, { field: field.name, format: (it) => /* @__PURE__ */ jsxDEV("pre", { children: JSON.stringify(it) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
          lineNumber: 62,
          columnNumber: 80
        }, globalThis) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
          lineNumber: 62,
          columnNumber: 40
        }, globalThis) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
          lineNumber: 62,
          columnNumber: 12
        }, globalThis);
      } else {
        return /* @__PURE__ */ jsxDEV(FieldContainer, { ...common, children: [
          "Unsupported field type ",
          field.type
        ] }, void 0, true, {
          fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
          lineNumber: 65,
          columnNumber: 12
        }, globalThis);
      }
    } else {
      const sortableBy = resolveSortableBy(schema, field);
      const connectingEntity = resolveConnectingEntity(schema, field, sortableBy);
      const targetField = connectingEntity ? connectingEntity.field : field;
      const targetEntity = schema.getEntity(targetField.targetEntity);
      const humanFieldName = getHumanFriendlyField(targetEntity);
      const optionLabel = /* @__PURE__ */ jsxDEV(AutoLabel, { field: humanFieldName, createLink: createEditLink }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
        lineNumber: 75,
        columnNumber: 24
      }, globalThis);
      const otherSide = targetField.side === "owning" ? targetField.inversedBy : targetField.ownedBy;
      const excludedFields = [otherSide, sortableBy].filter((it) => it);
      const createNewForm = excludedEntities === void 0 || !excludedEntities.includes(targetEntity.name) ? /* @__PURE__ */ jsxDEV(AutoFields, { excludedFields, excludedEntities, createEditLink }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
        lineNumber: 80,
        columnNumber: 7
      }, globalThis) : void 0;
      if (field.type === "OneHasOne" || field.type === "ManyHasOne") {
        return /* @__PURE__ */ jsxDEV(
          SelectField,
          {
            field: field.name,
            label: field.name,
            options: targetEntity.name,
            optionLabel,
            searchByFields: [humanFieldName],
            createNewForm,
            allowNull: field.nullable === true,
            required: field.nullable !== true,
            lazy: true
          },
          void 0,
          false,
          {
            fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
            lineNumber: 85,
            columnNumber: 6
          },
          globalThis
        );
      } else {
        return /* @__PURE__ */ jsxDEV(
          MultiSelectField,
          {
            field: field.name,
            label: field.name,
            options: targetEntity.name,
            optionLabel,
            searchByFields: [humanFieldName],
            sortableBy,
            createNewForm,
            connectingEntityField: connectingEntity ? connectingEntity.field.name : void 0,
            lazy: true
          },
          void 0,
          false,
          {
            fileName: "/__w/interface/interface/packages/admin/src/components/Auto/AutoField.tsx",
            lineNumber: 100,
            columnNumber: 6
          },
          globalThis
        );
      }
    }
  }
);
export {
  AutoField
};
//# sourceMappingURL=AutoField.js.map
