import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
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 { DataGridColumn } from "../base/DataGridColumn.js";
import { FieldFallbackView } from "../../../fieldViews/FieldFallbackView.js";
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: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/HasOneSelectCell.tsx",
        lineNumber: 59,
        columnNumber: 35
      }, globalThis),
      children: /* @__PURE__ */ jsxDEV(HasOneSelectCellContent, { lazy: { initialLimit: 0 }, ...props }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/HasOneSelectCell.tsx",
        lineNumber: 64,
        columnNumber: 4
      }, globalThis)
    },
    void 0,
    false,
    {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/HasOneSelectCell.tsx",
      lineNumber: 35,
      columnNumber: 3
    },
    globalThis
  );
}, "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, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/HasOneSelectCell.tsx",
        lineNumber: 75,
        columnNumber: 11
      }, globalThis);
    }
    if ("field" in desugaredOptionPath) {
      const val = entity.getField(desugaredOptionPath).value;
      if (val !== null) {
        return /* @__PURE__ */ jsxDEV(Fragment, { children: val }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/HasOneSelectCell.tsx",
          lineNumber: 81,
          columnNumber: 12
        }, globalThis);
      }
      return /* @__PURE__ */ jsxDEV(FieldFallbackView, { fallback: props.fallback, fallbackStyle: props.fallbackStyle }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/HasOneSelectCell.tsx",
        lineNumber: 83,
        columnNumber: 11
      }, globalThis);
    }
    if ("optionLabel" in props) {
      return /* @__PURE__ */ jsxDEV(HasOne, { field: props.field, expectedMutation: "none", children: props.optionLabel }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/HasOneSelectCell.tsx",
        lineNumber: 87,
        columnNumber: 11
      }, globalThis);
    }
    return /* @__PURE__ */ jsxDEV(Fragment, {}, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/HasOneSelectCell.tsx",
      lineNumber: 90,
      columnNumber: 10
    }, globalThis);
  },
  (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: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/HasOneSelectCell.tsx",
        lineNumber: 101,
        columnNumber: 5
      }, globalThis)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/HasOneSelectCell.tsx",
      lineNumber: 98,
      columnNumber: 4
    }, globalThis);
  }
);
export {
  HasOneSelectCell
};
//# sourceMappingURL=HasOneSelectCell.js.map
