import { Component, QueryLanguage, wrapFilterInHasOnes, useEntity, HasOne } from "@contember/binding";
import { renderDynamicChoiceFieldStatic } from "../../../fields/ChoiceField/renderDynamicChoiceFieldStatic.js";
import { SelectCellFilter } from "./SelectCellFilter.js";
import { useDesugaredOptionPath } from "../../../fields/ChoiceField/hooks/useDesugaredOptionPath.js";
import { jsxDEV, Fragment } 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/HasOneSelectCell.tsx";
const HasOneSelectCell = 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.desugarRelativeSingleEntity(props, environment);
      const conditions = [];
      if (filter.id.length > 0) {
        conditions.push({
          in: filter.id
        });
      }
      if (filter.nullCondition === true) {
        conditions.push({
          isNull: true
        });
      }
      return wrapFilterInHasOnes(desugared.hasOneRelationPath, {
        id: {
          or: conditions
        }
      });
    },
    emptyFilter: {
      id: [],
      nullCondition: false
    },
    filterRenderer: (filterProps) => /* @__PURE__ */ jsxDEV(SelectCellFilter, {
      optionProps: {
        lazy: {
          initialLimit: 0
        },
        ...props
      },
      ...filterProps
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 50,
      columnNumber: 35
    }, void 0),
    children: /* @__PURE__ */ jsxDEV(HasOneSelectCellContent, {
      lazy: {
        initialLimit: 0
      },
      ...props
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 55,
      columnNumber: 4
    }, void 0)
  }, void 0, false, {
    fileName: _jsxFileName,
    lineNumber: 26,
    columnNumber: 3
  }, void 0);
}, "HasOneSelectField");
const HasOneSelectCellContent = Component((props) => {
  const desugaredOptionPath = useDesugaredOptionPath(props, void 0);
  const entity = useEntity(props).getEntity({
    field: desugaredOptionPath.hasOneRelationPath
  });
  if ("renderOption" in props) {
    return /* @__PURE__ */ jsxDEV(Fragment, {
      children: props.renderOption(entity)
    }, void 0, false);
  }
  if ("field" in desugaredOptionPath) {
    const val = entity.getField(desugaredOptionPath).value;
    if (val !== null) {
      return /* @__PURE__ */ jsxDEV(Fragment, {
        children: val
      }, void 0, false);
    }
    return /* @__PURE__ */ jsxDEV(FieldFallbackView, {
      fallback: props.fallback,
      fallbackStyle: props.fallbackStyle
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 74,
      columnNumber: 11
    }, void 0);
  }
  if ("optionLabel" in props) {
    return /* @__PURE__ */ jsxDEV(HasOne, {
      field: props.field,
      expectedMutation: "none",
      children: props.optionLabel
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 78,
      columnNumber: 11
    }, void 0);
  }
  return /* @__PURE__ */ jsxDEV(Fragment, {}, void 0, false);
}, (props, environment) => {
  const {
    subTree,
    renderedOption
  } = renderDynamicChoiceFieldStatic(props, environment);
  const {
    subTree: currentValuesSubtree
  } = renderDynamicChoiceFieldStatic(props, environment, {
    id: {
      in: []
    }
  });
  return /* @__PURE__ */ jsxDEV(Fragment, {
    children: [subTree, currentValuesSubtree, /* @__PURE__ */ jsxDEV(HasOne, {
      field: props.field,
      expectedMutation: "none",
      children: renderedOption
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 92,
      columnNumber: 5
    }, void 0)]
  }, void 0, true);
});
export {
  HasOneSelectCell
};
//# sourceMappingURL=HasOneSelectCell.js.map
