import { createElement, useMemo, Fragment } from "react";
import { Component, QueryLanguage, wrapFilterInHasOnes, useEntityList, HasMany } from "@contember/binding";
import { renderDynamicChoiceFieldStatic } from "../../../fields/ChoiceField/renderDynamicChoiceFieldStatic.js";
import { SelectCellFilter } from "./SelectCellFilter.js";
import { useDesugaredOptionPath } from "../../../fields/ChoiceField/hooks/useDesugaredOptionPath.js";
import { FieldFallbackView } from "../../../fieldViews/FieldFallbackView.js";
import { DataGridColumn } from "../base/DataGridColumn.js";
const HasManySelectCell = Component((props) => {
  return /* @__PURE__ */ createElement(DataGridColumn, {
    ...props,
    enableOrdering: false,
    getNewFilter: (filter, { environment }) => {
      if (filter.id.length === 0 && filter.nullCondition === false) {
        return void 0;
      }
      const desugared = QueryLanguage.desugarRelativeEntityList(props, environment);
      const ors = [];
      if (filter.id.length > 0) {
        ors.push(wrapFilterInHasOnes(desugared.hasOneRelationPath, {
          [desugared.hasManyRelation.field]: {
            id: { in: filter.id }
          }
        }));
      }
      if (filter.nullCondition === true) {
        ors.push({
          not: wrapFilterInHasOnes(desugared.hasOneRelationPath, {
            [desugared.hasManyRelation.field]: {
              id: { isNull: false }
            }
          })
        });
      }
      return { or: ors };
    },
    emptyFilter: {
      id: [],
      nullCondition: false
    },
    filterRenderer: (filterProps) => /* @__PURE__ */ createElement(SelectCellFilter, {
      optionProps: {
        lazy: { initialLimit: 0 },
        ...props
      },
      ...filterProps
    })
  }, /* @__PURE__ */ createElement(HasManySelectCellContent, {
    lazy: { initialLimit: 0 },
    ...props
  }));
}, "HasManySelectField");
const HasManySelectCellContent = Component((props) => {
  const desugaredOptionPath = useDesugaredOptionPath(props, void 0);
  const entities = useEntityList(props);
  const entitiesArray = useMemo(() => Array.from(entities, (it) => it.getEntity({ field: desugaredOptionPath.hasOneRelationPath })), [desugaredOptionPath.hasOneRelationPath, entities]);
  const elementsRenderer = props.renderElements ?? defaultElementsRenderer;
  if ("renderOption" in props) {
    return elementsRenderer(entitiesArray.map((it) => props.renderOption(it)));
  }
  if ("field" in desugaredOptionPath) {
    if (entitiesArray.length === 0) {
      return /* @__PURE__ */ createElement(FieldFallbackView, {
        fallback: props.fallback,
        fallbackStyle: props.fallbackStyle
      });
    }
    return elementsRenderer(entitiesArray.map((it) => {
      const val = it.getField(desugaredOptionPath.field).value;
      if (val !== null) {
        return /* @__PURE__ */ createElement(Fragment, {
          key: it.key
        }, val);
      }
      return /* @__PURE__ */ createElement(FieldFallbackView, {
        key: it.key,
        fallback: props.fallback,
        fallbackStyle: props.fallbackStyle
      });
    }));
  }
  return /* @__PURE__ */ createElement(Fragment, null);
}, (props, environment) => {
  const { subTree, renderedOption } = renderDynamicChoiceFieldStatic(props, environment);
  const { subTree: currentValuesSubtree } = renderDynamicChoiceFieldStatic(props, environment, { id: { in: [] } });
  return /* @__PURE__ */ createElement(Fragment, null, subTree, currentValuesSubtree, /* @__PURE__ */ createElement(HasMany, {
    field: props.field,
    expectedMutation: "none"
  }, renderedOption));
});
const defaultElementsRenderer = (elements) => /* @__PURE__ */ createElement(Fragment, null, elements.map((it, index, arr) => /* @__PURE__ */ createElement(Fragment, {
  key: typeof it === "object" && it !== null && "key" in it ? it.key : index
}, it, index < arr.length - 1 ? ", " : "")));
export { HasManySelectCell };
//# sourceMappingURL=HasManySelectCell.js.map
