import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
import { useMemo } from "react";
import { Component, QueryLanguage, wrapFilterInHasOnes, SugaredField } from "@contember/binding";
import { GraphQlLiteral } from "@contember/client";
import { FieldContainer, Checkbox } from "@contember/ui";
import { NullConditionFilter } from "./NullConditionFilter.js";
import { FieldFallbackView } from "../../../fieldViews/FieldFallbackView.js";
import { DataGridColumn } from "../base/DataGridColumn.js";
const EnumCell = Component((props) => {
  return /* @__PURE__ */ jsxDEV(
    DataGridColumn,
    {
      ...props,
      enableOrdering: true,
      getNewOrderBy: (newDirection, { environment }) => newDirection ? QueryLanguage.desugarOrderBy(`${props.field} ${newDirection}`, environment) : void 0,
      enableFiltering: true,
      getNewFilter: (filter, { environment }) => {
        const { values, nullCondition = false } = Array.isArray(filter) ? {
          values: filter
        } : filter;
        if (values.length === 0 && !nullCondition) {
          return void 0;
        }
        const desugared = QueryLanguage.desugarRelativeSingleField(props.field, environment);
        const conditions = [];
        if (nullCondition) {
          conditions.push({ isNull: true });
        }
        conditions.push({
          in: values.map((it) => new GraphQlLiteral(it))
        });
        return wrapFilterInHasOnes(desugared.hasOneRelationPath, {
          [desugared.field]: { or: conditions }
        });
      },
      emptyFilter: { nullCondition: false, values: [] },
      filterRenderer: ({ filter: inFilter, setFilter, environment }) => {
        const filter = useMemo(() => Array.isArray(inFilter) ? { nullCondition: false, values: inFilter } : inFilter, [inFilter]);
        const values = filter.values;
        const checkboxList = Object.entries(props.options).map(([value, label]) => /* @__PURE__ */ jsxDEV(
          FieldContainer,
          {
            label,
            labelPosition: "labelInlineRight",
            children: /* @__PURE__ */ jsxDEV(
              Checkbox,
              {
                notNull: true,
                value: values.includes(value),
                onChange: (checked) => {
                  setFilter({ ...filter, values: checked ? [...values, value] : values.filter((it) => it !== value) });
                }
              },
              void 0,
              false,
              {
                fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/EnumCell.tsx",
                lineNumber: 89,
                columnNumber: 7
              },
              globalThis
            )
          },
          value,
          false,
          {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/EnumCell.tsx",
            lineNumber: 84,
            columnNumber: 6
          },
          globalThis
        ));
        return /* @__PURE__ */ jsxDEV(Fragment, { children: [
          checkboxList,
          /* @__PURE__ */ jsxDEV(NullConditionFilter, { filter, setFilter, environment, field: props.field, showNullConditionFilter: props.showNullConditionFilter }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/EnumCell.tsx",
            lineNumber: 101,
            columnNumber: 6
          }, globalThis)
        ] }, void 0, true, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/EnumCell.tsx",
          lineNumber: 99,
          columnNumber: 12
        }, globalThis);
      },
      children: /* @__PURE__ */ jsxDEV(SugaredField, { field: props.field, format: (value) => {
        if (value === null) {
          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/EnumCell.tsx",
            lineNumber: 107,
            columnNumber: 13
          }, globalThis);
        }
        if (props.format) {
          return props.format(props.options[value]);
        }
        return props.options[value];
      } }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/EnumCell.tsx",
        lineNumber: 105,
        columnNumber: 4
      }, globalThis)
    },
    void 0,
    false,
    {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/EnumCell.tsx",
      lineNumber: 47,
      columnNumber: 3
    },
    globalThis
  );
}, "EnumCell");
export {
  EnumCell
};
//# sourceMappingURL=EnumCell.js.map
