import { Component, QueryLanguage, wrapFilterInHasOnes, useEntityList, Entity, HasMany } from "@contember/binding";
import { useMemo, Fragment } from "react";
import { renderDynamicChoiceFieldStatic } from "../../../fields/ChoiceField/renderDynamicChoiceFieldStatic.js";
import { SelectCellFilter } from "./SelectCellFilter.js";
import { useDesugaredOptionPath } from "../../../fields/ChoiceField/hooks/useDesugaredOptionPath.js";
import { jsx, Fragment as Fragment$1, jsxs } from "react/jsx-runtime";
import { DataGridColumn } from "../base/DataGridColumn.js";
import { FieldFallbackView } from "../../../fieldViews/FieldFallbackView.js";
const HasManySelectCell = Component((props) => {
  return /* @__PURE__ */ jsx(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__ */ jsx(SelectCellFilter, {
      optionProps: {
        lazy: {
          initialLimit: 0
        },
        ...props
      },
      ...filterProps
    }),
    children: /* @__PURE__ */ jsx(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__ */ jsx(FieldFallbackView, {
        fallback: props.fallback,
        fallbackStyle: props.fallbackStyle
      });
    }
    return elementsRenderer(entitiesArray.map((it) => {
      const val = it.getField(desugaredOptionPath.field).value;
      if (val !== null) {
        return /* @__PURE__ */ jsx(Fragment, {
          children: val
        }, it.key);
      }
      return /* @__PURE__ */ jsx(FieldFallbackView, {
        fallback: props.fallback,
        fallbackStyle: props.fallbackStyle
      }, it.key);
    }));
  }
  if ("optionLabel" in props) {
    return elementsRenderer(entitiesArray.map((it) => /* @__PURE__ */ jsx(Entity, {
      accessor: it,
      children: props.optionLabel
    }, it.key)));
  }
  return /* @__PURE__ */ jsx(Fragment$1, {});
}, (props, environment) => {
  const {
    subTree,
    renderedOption
  } = renderDynamicChoiceFieldStatic(props, environment);
  const {
    subTree: currentValuesSubtree
  } = renderDynamicChoiceFieldStatic(props, environment, {
    id: {
      in: []
    }
  });
  return /* @__PURE__ */ jsxs(Fragment$1, {
    children: [subTree, currentValuesSubtree, /* @__PURE__ */ jsx(HasMany, {
      field: props.field,
      expectedMutation: "none",
      children: renderedOption
    })]
  });
});
const defaultElementsRenderer = (elements) => /* @__PURE__ */ jsx(Fragment$1, {
  children: elements.map((it, index, arr) => /* @__PURE__ */ jsxs(Fragment, {
    children: [it, index < arr.length - 1 ? ", " : ""]
  }, typeof it === "object" && it !== null && "key" in it ? it.key : index))
});
export {
  HasManySelectCell
};
//# sourceMappingURL=HasManySelectCell.js.map
