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 { jsxDEV, Fragment } from "react/jsx-dev-runtime";
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";
var _jsxFileName = "/__w/interface/interface/packages/admin/src/components/Auto/AutoCell.tsx";
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__ */ jsxDEV(TextCell, {
        header: field.name,
        field: field.name,
        shrunk: true,
        format: (it) => formatString(field.type, it)
      }, void 0, false, {
        fileName: _jsxFileName,
        lineNumber: 38,
        columnNumber: 6
      }, void 0);
    } else if (field.type === "String") {
      return /* @__PURE__ */ jsxDEV(TextCell, {
        header: field.name,
        field: field.name,
        format: (it) => /* @__PURE__ */ jsxDEV(ClickToEdit, {
          view: formatString(field.type, it),
          edit: /* @__PURE__ */ jsxDEV(TextareaField, {
            field: field.name,
            label: void 0,
            minRows: 1,
            size: "small"
          }, void 0, false, {
            fileName: _jsxFileName,
            lineNumber: 54,
            columnNumber: 15
          }, void 0)
        }, void 0, false, {
          fileName: _jsxFileName,
          lineNumber: 52,
          columnNumber: 8
        }, void 0)
      }, void 0, false, {
        fileName: _jsxFileName,
        lineNumber: 48,
        columnNumber: 6
      }, void 0);
    } else if (field.type === "Uuid") {
      return /* @__PURE__ */ jsxDEV(TextCell, {
        header: field.name,
        field: field.name,
        format: (it) => /* @__PURE__ */ jsxDEV(ClickToEdit, {
          view: formatString(field.type, it),
          edit: /* @__PURE__ */ jsxDEV(TextField, {
            field: field.name,
            label: void 0,
            size: "small"
          }, void 0, false, {
            fileName: _jsxFileName,
            lineNumber: 68,
            columnNumber: 15
          }, void 0)
        }, void 0, false, {
          fileName: _jsxFileName,
          lineNumber: 66,
          columnNumber: 8
        }, void 0)
      }, void 0, false, {
        fileName: _jsxFileName,
        lineNumber: 62,
        columnNumber: 6
      }, void 0);
    } else if (field.type === "Bool") {
      return /* @__PURE__ */ jsxDEV(BooleanCell, {
        header: field.name,
        field: field.name,
        format: (it) => /* @__PURE__ */ jsxDEV(ClickToEdit, {
          view: it ? "\u2713" : "\u2717",
          edit: /* @__PURE__ */ jsxDEV(CheckboxField, {
            field: field.name,
            label: void 0
          }, void 0, false, {
            fileName: _jsxFileName,
            lineNumber: 82,
            columnNumber: 15
          }, void 0)
        }, void 0, false, {
          fileName: _jsxFileName,
          lineNumber: 80,
          columnNumber: 8
        }, void 0)
      }, void 0, false, {
        fileName: _jsxFileName,
        lineNumber: 76,
        columnNumber: 6
      }, void 0);
    } else if (field.type === "Enum") {
      const enumValues = schema.getEnumValues(field.enumName);
      return /* @__PURE__ */ jsxDEV(EnumCell, {
        header: field.name,
        field: field.name,
        options: Object.fromEntries(enumValues.map((it) => [it, it])),
        format: (it) => /* @__PURE__ */ jsxDEV(ClickToEdit, {
          view: it,
          edit: /* @__PURE__ */ jsxDEV(SelectField, {
            field: field.name,
            label: void 0,
            options: enumValues.map((it2) => ({
              value: it2,
              label: it2
            }))
          }, void 0, false, {
            fileName: _jsxFileName,
            lineNumber: 99,
            columnNumber: 15
          }, void 0)
        }, void 0, false, {
          fileName: _jsxFileName,
          lineNumber: 97,
          columnNumber: 8
        }, void 0)
      }, void 0, false, {
        fileName: _jsxFileName,
        lineNumber: 92,
        columnNumber: 6
      }, void 0);
    } else if (field.type === "Integer" || field.type === "Double") {
      return /* @__PURE__ */ jsxDEV(NumberCell, {
        header: field.name,
        field: field.name,
        format: (it) => /* @__PURE__ */ jsxDEV(ClickToEdit, {
          view: it,
          edit: /* @__PURE__ */ jsxDEV(NumberField, {
            field: field.name,
            label: void 0,
            size: "small"
          }, void 0, false, {
            fileName: _jsxFileName,
            lineNumber: 113,
            columnNumber: 15
          }, void 0)
        }, void 0, false, {
          fileName: _jsxFileName,
          lineNumber: 111,
          columnNumber: 8
        }, void 0)
      }, void 0, false, {
        fileName: _jsxFileName,
        lineNumber: 107,
        columnNumber: 6
      }, void 0);
    } else if (field.type === "Date") {
      return /* @__PURE__ */ jsxDEV(DateCell, {
        header: field.name,
        field: field.name,
        format: (it) => /* @__PURE__ */ jsxDEV(ClickToEdit, {
          view: dateToStringWithoutTimezone(it, {
            includeTime: false
          }),
          edit: /* @__PURE__ */ jsxDEV(DateField, {
            field: field.name,
            label: void 0
          }, void 0, false, {
            fileName: _jsxFileName,
            lineNumber: 127,
            columnNumber: 15
          }, void 0)
        }, void 0, false, {
          fileName: _jsxFileName,
          lineNumber: 125,
          columnNumber: 8
        }, void 0)
      }, void 0, false, {
        fileName: _jsxFileName,
        lineNumber: 121,
        columnNumber: 6
      }, void 0);
    } else if (field.type === "DateTime") {
      return /* @__PURE__ */ jsxDEV(DateCell, {
        header: field.name,
        field: field.name,
        format: (it) => /* @__PURE__ */ jsxDEV(ClickToEdit, {
          view: dateToStringWithoutTimezone(it, {
            includeTime: true
          }),
          edit: /* @__PURE__ */ jsxDEV(DateTimeField, {
            field: field.name,
            label: void 0
          }, void 0, false, {
            fileName: _jsxFileName,
            lineNumber: 141,
            columnNumber: 15
          }, void 0)
        }, void 0, false, {
          fileName: _jsxFileName,
          lineNumber: 139,
          columnNumber: 8
        }, void 0)
      }, void 0, false, {
        fileName: _jsxFileName,
        lineNumber: 135,
        columnNumber: 6
      }, void 0);
    } else {
      return /* @__PURE__ */ jsxDEV(Fragment, {}, void 0, false);
    }
  } 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__ */ jsxDEV(AutoLabel, {
      field: humanFieldName,
      createLink: createEntityLink
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 158,
      columnNumber: 22
    }, void 0);
    optionLabel = connectingEntity ? /* @__PURE__ */ jsxDEV(HasOne, {
      field: connectingEntity.field.name,
      children: optionLabel
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 159,
      columnNumber: 37
    }, void 0) : optionLabel;
    if (field.type === "OneHasOne" || field.type === "ManyHasOne") {
      return /* @__PURE__ */ jsxDEV(HasOneSelectCell, {
        header: field.name,
        field: field.name,
        options: field.targetEntity,
        searchByFields: [humanFieldName],
        optionLabel
      }, field.name, false, {
        fileName: _jsxFileName,
        lineNumber: 163,
        columnNumber: 6
      }, void 0);
    } else {
      return /* @__PURE__ */ jsxDEV(HasManySelectCell, {
        header: field.name,
        field: field.name,
        options: field.targetEntity,
        searchByFields: [connectingEntity ? `${connectingEntity.field.name}.${humanFieldName}` : humanFieldName],
        optionLabel
      }, field.name, false, {
        fileName: _jsxFileName,
        lineNumber: 175,
        columnNumber: 6
      }, void 0);
    }
  }
});
const ClickToEdit = Component((props) => {
  const [edit, setEdit] = useState(false);
  const onClick = useCallback((e) => e.ctrlKey && setEdit(true), []);
  useEntityPersistSuccess(() => setEdit(false));
  return edit ? /* @__PURE__ */ jsxDEV(Fragment, {
    children: props.edit
  }, void 0, false) : /* @__PURE__ */ jsxDEV("div", {
    onClick,
    children: props.view
  }, void 0, false, {
    fileName: _jsxFileName,
    lineNumber: 195,
    columnNumber: 37
  }, void 0);
}, (props) => /* @__PURE__ */ jsxDEV(Fragment, {
  children: [props.edit, props.view]
}, void 0, true));
export {
  AutoCell
};
//# sourceMappingURL=AutoCell.js.map
