import { jsx, jsxs } from "react/jsx-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__ */ jsx(TextField, { ...common, readOnly: true });
      } else if (field.type === "String") {
        return /* @__PURE__ */ jsx(TextareaField, { ...common, minRows: 1 });
      } else if (field.type === "Uuid") {
        return /* @__PURE__ */ jsx(TextField, { ...common });
      } else if (field.type === "Bool") {
        return /* @__PURE__ */ jsx(CheckboxField, { ...common });
      } else if (field.type === "Integer") {
        return /* @__PURE__ */ jsx(NumberField, { ...common });
      } else if (field.type === "Double") {
        return /* @__PURE__ */ jsx(FloatField, { ...common });
      } else if (field.type === "Date") {
        return /* @__PURE__ */ jsx(DateField, { ...common });
      } else if (field.type === "DateTime") {
        return /* @__PURE__ */ jsx(DateTimeField, { ...common });
      } else if (field.type === "Enum") {
        const enumValues = schema.getEnumValues(field.enumName);
        const options = enumValues.map((it) => ({ value: it, label: it }));
        return /* @__PURE__ */ jsx(SelectField, { ...common, options, allowNull: field.nullable });
      } else if (field.type === "Json") {
        return /* @__PURE__ */ jsx(FieldContainer, { ...common, children: /* @__PURE__ */ jsx(Field, { field: field.name, format: (it) => /* @__PURE__ */ jsx("pre", { children: JSON.stringify(it) }) }) });
      } else {
        return /* @__PURE__ */ jsxs(FieldContainer, { ...common, children: [
          "Unsupported field type ",
          field.type
        ] });
      }
    } 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__ */ jsx(AutoLabel, { field: humanFieldName, createLink: createEditLink });
      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__ */ jsx(AutoFields, { excludedFields, excludedEntities, createEditLink }) : void 0;
      if (field.type === "OneHasOne" || field.type === "ManyHasOne") {
        return /* @__PURE__ */ jsx(
          SelectField,
          {
            field: field.name,
            label: field.name,
            options: targetEntity.name,
            optionLabel,
            searchByFields: [humanFieldName],
            createNewForm,
            allowNull: field.nullable === true,
            required: field.nullable !== true,
            lazy: true
          }
        );
      } else {
        return /* @__PURE__ */ jsx(
          MultiSelectField,
          {
            field: field.name,
            label: field.name,
            options: targetEntity.name,
            optionLabel,
            searchByFields: [humanFieldName],
            sortableBy,
            createNewForm,
            connectingEntityField: connectingEntity ? connectingEntity.field.name : void 0,
            lazy: true
          }
        );
      }
    }
  }
);
export {
  AutoField
};
//# sourceMappingURL=AutoField.js.map
