import { jsx, Fragment, jsxs } from "react/jsx-runtime";
import { Component, PRIMARY_KEY_NAME, HasOne, useEntityPersistSuccess } from "@contember/binding";
import { formatString, resolveSortableBy, resolveConnectingEntity, getHumanFriendlyField } from "./utils.js";
import { useState, useCallback } from "react";
import { AutoLabel } from "./AutoLabel.js";
import { TextCell } from "../bindingFacade/collections/DataGrid/cells/TextCell.js";
import { TextField } from "../bindingFacade/fields/TextField.js";
import { BooleanCell } from "../bindingFacade/collections/DataGrid/cells/BooleanCell.js";
import { CheckboxField } from "../bindingFacade/fields/CheckboxField.js";
import { EnumCell } from "../bindingFacade/collections/DataGrid/cells/EnumCell.js";
import { SelectField } from "../bindingFacade/fields/SelectField/SelectField.js";
import { NumberCell } from "../bindingFacade/collections/DataGrid/cells/NumberCell.js";
import { NumberField } from "../bindingFacade/fields/NumberField.js";
import { DateCell } from "../bindingFacade/collections/DataGrid/cells/DateCell.js";
import { DateField } from "../bindingFacade/fields/DateField.js";
import { DateTimeField } from "../bindingFacade/fields/DateTimeField.js";
import { HasOneSelectCell } from "../bindingFacade/collections/DataGrid/cells/HasOneSelectCell.js";
import { HasManySelectCell } from "../bindingFacade/collections/DataGrid/cells/HasManySelectCell.js";
import { TextareaField } from "../bindingFacade/fields/TextareaField.js";
import { dateToStringWithoutTimezone } from "../../utils/dateToStringWithoutTimezone.js";
const AutoCell = Component(
  ({ schema, entityName, fieldName, createEntityLink }) => {
    const field = schema.getEntityField(entityName, fieldName);
    if (field.__typename === "_Column") {
      if (field.name === PRIMARY_KEY_NAME) {
        return /* @__PURE__ */ jsx(
          TextCell,
          {
            header: field.name,
            field: field.name,
            shrunk: true,
            format: (it) => formatString(field.type, it)
          }
        );
      } else if (field.type === "String") {
        return /* @__PURE__ */ jsx(
          TextCell,
          {
            header: field.name,
            field: field.name,
            format: (it) => /* @__PURE__ */ jsx(
              ClickToEdit,
              {
                view: formatString(field.type, it),
                edit: /* @__PURE__ */ jsx(TextareaField, { field: field.name, label: void 0, minRows: 1, size: "small" })
              }
            )
          }
        );
      } else if (field.type === "Uuid") {
        return /* @__PURE__ */ jsx(
          TextCell,
          {
            header: field.name,
            field: field.name,
            format: (it) => /* @__PURE__ */ jsx(
              ClickToEdit,
              {
                view: formatString(field.type, it),
                edit: /* @__PURE__ */ jsx(TextField, { field: field.name, label: void 0, size: "small" })
              }
            )
          }
        );
      } else if (field.type === "Bool") {
        return /* @__PURE__ */ jsx(
          BooleanCell,
          {
            header: field.name,
            field: field.name,
            format: (it) => /* @__PURE__ */ jsx(
              ClickToEdit,
              {
                view: it ? "✓" : "✗",
                edit: /* @__PURE__ */ jsx(CheckboxField, { field: field.name, label: void 0 })
              }
            )
          }
        );
      } else if (field.type === "Enum") {
        const enumValues = schema.getEnumValues(field.enumName);
        return /* @__PURE__ */ jsx(
          EnumCell,
          {
            header: field.name,
            field: field.name,
            options: Object.fromEntries(enumValues.map((it) => [it, it])),
            format: (it) => /* @__PURE__ */ jsx(
              ClickToEdit,
              {
                view: it,
                edit: /* @__PURE__ */ jsx(SelectField, { field: field.name, label: void 0, options: enumValues.map((it2) => ({ value: it2, label: it2 })) })
              }
            )
          }
        );
      } else if (field.type === "Integer" || field.type === "Double") {
        return /* @__PURE__ */ jsx(
          NumberCell,
          {
            header: field.name,
            field: field.name,
            format: (it) => /* @__PURE__ */ jsx(
              ClickToEdit,
              {
                view: it,
                edit: /* @__PURE__ */ jsx(NumberField, { field: field.name, label: void 0, size: "small" })
              }
            )
          }
        );
      } else if (field.type === "Date") {
        return /* @__PURE__ */ jsx(
          DateCell,
          {
            header: field.name,
            field: field.name,
            format: (it) => /* @__PURE__ */ jsx(
              ClickToEdit,
              {
                view: dateToStringWithoutTimezone(it, { includeTime: false }),
                edit: /* @__PURE__ */ jsx(DateField, { field: field.name, label: void 0 })
              }
            )
          }
        );
      } else if (field.type === "DateTime") {
        return /* @__PURE__ */ jsx(
          DateCell,
          {
            header: field.name,
            field: field.name,
            format: (it) => /* @__PURE__ */ jsx(
              ClickToEdit,
              {
                view: dateToStringWithoutTimezone(it, { includeTime: true }),
                edit: /* @__PURE__ */ jsx(DateTimeField, { field: field.name, label: void 0 })
              }
            )
          }
        );
      } else {
        return /* @__PURE__ */ jsx(Fragment, {});
      }
    } 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);
      let optionLabel = /* @__PURE__ */ jsx(AutoLabel, { field: humanFieldName, createLink: createEntityLink });
      optionLabel = connectingEntity ? /* @__PURE__ */ jsx(HasOne, { field: connectingEntity.field.name, children: optionLabel }) : optionLabel;
      if (field.type === "OneHasOne" || field.type === "ManyHasOne") {
        return /* @__PURE__ */ jsx(
          HasOneSelectCell,
          {
            header: field.name,
            field: field.name,
            options: field.targetEntity,
            searchByFields: [humanFieldName],
            optionLabel
          },
          field.name
        );
      } else {
        return /* @__PURE__ */ jsx(
          HasManySelectCell,
          {
            header: field.name,
            field: field.name,
            options: field.targetEntity,
            searchByFields: [connectingEntity ? `${connectingEntity.field.name}.${humanFieldName}` : humanFieldName],
            optionLabel
          },
          field.name
        );
      }
    }
  }
);
const ClickToEdit = Component(
  (props) => {
    const [edit, setEdit] = useState(false);
    const onClick = useCallback((e) => e.ctrlKey && setEdit(true), []);
    useEntityPersistSuccess(() => setEdit(false));
    return edit ? /* @__PURE__ */ jsx(Fragment, { children: props.edit }) : /* @__PURE__ */ jsx("div", { onClick, children: props.view });
  },
  (props) => /* @__PURE__ */ jsxs(Fragment, { children: [
    props.edit,
    props.view
  ] })
);
export {
  AutoCell
};
//# sourceMappingURL=AutoCell.js.map
