import { PRIMARY_KEY_NAME } from "@contember/binding";
import { jsxDEV } from "react/jsx-dev-runtime";
var _jsxFileName = "/__w/interface/interface/packages/admin/src/components/Auto/utils.tsx";
const getHumanFriendlyField = (entitySchema) => {
  for (const field of ["name", "title", "heading", "label", "caption", "slug", "code", "description"]) {
    if (entitySchema.fields.has(field)) {
      return field;
    }
  }
  return PRIMARY_KEY_NAME;
};
const formatString = (type, value) => {
  if (typeof value !== "string") {
    return value;
  } else if (type === "Uuid") {
    return /* @__PURE__ */ jsxDEV("span", {
      title: value,
      children: value.slice(0, 8)
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 19,
      columnNumber: 10
    }, void 0);
  } else if (type === "String") {
    return value.length > 100 ? /* @__PURE__ */ jsxDEV("span", {
      title: value,
      children: value.slice(0, 100) + "..."
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 22,
      columnNumber: 31
    }, void 0) : value;
  } else {
    return value;
  }
};
const resolveSortableBy = (schema, field) => {
  if (field.type !== "OneHasMany" || field.side !== "inverse") {
    return void 0;
  }
  if (field.orderBy === null || field.orderBy.length !== 1 || field.orderBy[0].path.length !== 1) {
    return void 0;
  }
  const sortableBy = schema.getEntityField(field.targetEntity, field.orderBy[0].path[0]);
  return sortableBy.type === "Integer" ? sortableBy.name : void 0;
};
const resolveConnectingEntity = (schema, field, sortableBy) => {
  if (field.type !== "OneHasMany" || field.side !== "inverse") {
    return void 0;
  }
  const excludedFields = ["id", field.ownedBy, ...sortableBy ? [sortableBy] : []];
  const connectingEntity = schema.getEntity(field.targetEntity);
  const connectingEntityFields = Array.from(connectingEntity.fields.values()).filter((it) => !excludedFields.includes(it.name));
  if (connectingEntityFields.length !== 1 || connectingEntityFields[0].__typename !== "_Relation" || connectingEntityFields[0].type !== "ManyHasOne") {
    return void 0;
  }
  return {
    entity: connectingEntity,
    field: connectingEntityFields[0]
  };
};
export {
  formatString,
  getHumanFriendlyField,
  resolveConnectingEntity,
  resolveSortableBy
};
//# sourceMappingURL=utils.js.map
