import { Component, QueryLanguage } from "@contember/binding";
import { AutoCell } from "./AutoCell.js";
import { jsx, jsxs } from "react/jsx-runtime";
import { GenericCell } from "../bindingFacade/collections/DataGrid/cells/GenericCell.js";
import { DataGrid } from "../bindingFacade/collections/DataGrid/grid/DataGrid.js";
import { LinkButton } from "../../routing/binding/LinkButton.js";
import { DeleteEntityButton } from "../bindingFacade/collections/helpers/DeleteEntityButton.js";
const AutoGrid = Component((props, env) => {
  const schema = env.getSchema();
  const entities = QueryLanguage.desugarQualifiedEntityList({
    entities: props.entities
  }, env);
  const {
    createViewLinkTarget,
    createEditLinkTarget,
    ...dataGridProps
  } = props;
  const columns = createDataGridColumns(schema, entities.entityName, createViewLinkTarget, createEditLinkTarget);
  return /* @__PURE__ */ jsx(DataGrid, {
    ...dataGridProps,
    children: columns
  });
});
const createDataGridColumns = (schema, entityName, createViewLinkTarget, createEditLinkTarget) => {
  const entitySchema = schema.getEntity(entityName);
  const fieldColumns = Array.from(entitySchema.fields.values()).map((it) => /* @__PURE__ */ jsx(AutoCell, {
    schema,
    entityName,
    fieldName: it.name,
    createEntityLink: createViewLinkTarget
  }, it.name));
  const actionColumn = /* @__PURE__ */ jsxs(GenericCell, {
    shrunk: true,
    children: [createEditLinkTarget && /* @__PURE__ */ jsx(LinkButton, {
      to: createEditLinkTarget(entityName),
      distinction: "seamless",
      children: "edit"
    }), /* @__PURE__ */ jsx(DeleteEntityButton, {})]
  }, "action");
  return [...fieldColumns, actionColumn];
};
export {
  AutoGrid
};
//# sourceMappingURL=AutoGrid.js.map
