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 { jsxDEV, Fragment as Fragment$1 } from "react/jsx-dev-runtime";
import { DataGridColumn } from "../base/DataGridColumn.js";
import { FieldFallbackView } from "../../../fieldViews/FieldFallbackView.js";
var _jsxFileName = "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/HasManySelectCell.tsx";
const HasManySelectCell = Component((props) => {
  return /* @__PURE__ */ jsxDEV(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__ */ jsxDEV(SelectCellFilter, {
      optionProps: {
        lazy: {
          initialLimit: 0
        },
        ...props
      },
      ...filterProps
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 64,
      columnNumber: 35
    }, void 0),
    children: /* @__PURE__ */ jsxDEV(HasManySelectCellContent, {
      lazy: {
        initialLimit: 0
      },
      ...props
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 69,
      columnNumber: 4
    }, void 0)
  }, void 0, false, {
    fileName: _jsxFileName,
    lineNumber: 32,
    columnNumber: 3
  }, void 0);
}, "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__ */ jsxDEV(FieldFallbackView, {
        fallback: props.fallback,
        fallbackStyle: props.fallbackStyle
      }, void 0, false, {
        fileName: _jsxFileName,
        lineNumber: 91,
        columnNumber: 12
      }, void 0);
    }
    return elementsRenderer(entitiesArray.map((it) => {
      const val = it.getField(desugaredOptionPath.field).value;
      if (val !== null) {
        return /* @__PURE__ */ jsxDEV(Fragment, {
          children: val
        }, it.key, false, {
          fileName: _jsxFileName,
          lineNumber: 96,
          columnNumber: 13
        }, void 0);
      }
      return /* @__PURE__ */ jsxDEV(FieldFallbackView, {
        fallback: props.fallback,
        fallbackStyle: props.fallbackStyle
      }, it.key, false, {
        fileName: _jsxFileName,
        lineNumber: 98,
        columnNumber: 12
      }, void 0);
    }));
  }
  if ("optionLabel" in props) {
    return elementsRenderer(entitiesArray.map((it) => /* @__PURE__ */ jsxDEV(Entity, {
      accessor: it,
      children: props.optionLabel
    }, it.key, false, {
      fileName: _jsxFileName,
      lineNumber: 104,
      columnNumber: 5
    }, void 0)));
  }
  return /* @__PURE__ */ jsxDEV(Fragment$1, {}, void 0, false);
}, (props, environment) => {
  const {
    subTree,
    renderedOption
  } = renderDynamicChoiceFieldStatic(props, environment);
  const {
    subTree: currentValuesSubtree
  } = renderDynamicChoiceFieldStatic(props, environment, {
    id: {
      in: []
    }
  });
  return /* @__PURE__ */ jsxDEV(Fragment$1, {
    children: [subTree, currentValuesSubtree, /* @__PURE__ */ jsxDEV(HasMany, {
      field: props.field,
      expectedMutation: "none",
      children: renderedOption
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 119,
      columnNumber: 5
    }, void 0)]
  }, void 0, true);
});
const defaultElementsRenderer = (elements) => /* @__PURE__ */ jsxDEV(Fragment$1, {
  children: elements.map((it, index, arr) => /* @__PURE__ */ jsxDEV(Fragment, {
    children: [it, index < arr.length - 1 ? ", " : ""]
  }, typeof it === "object" && it !== null && "key" in it ? it.key : index, true, {
    fileName: _jsxFileName,
    lineNumber: 129,
    columnNumber: 37
  }, void 0))
}, void 0, false);
export {
  HasManySelectCell
};
//# sourceMappingURL=HasManySelectCell.js.map
