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 { jsx, jsxs, Fragment } from "react/jsx-runtime";
import { FieldFallbackView } from "../../../fieldViews/FieldFallbackView.js";
import { DataGridColumn } from "../base/DataGridColumn.js";
const EnumCell = Component((props) => {
  return /* @__PURE__ */ jsx(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__ */ jsx(FieldContainer, {
        label,
        labelPosition: "labelInlineRight",
        children: /* @__PURE__ */ jsx(Checkbox, {
          notNull: true,
          value: values.includes(value),
          onChange: (checked) => {
            setFilter({
              ...filter,
              values: checked ? [...values, value] : values.filter((it) => it !== value)
            });
          }
        })
      }, value));
      return /* @__PURE__ */ jsxs(Fragment, {
        children: [checkboxList, /* @__PURE__ */ jsx(NullConditionFilter, {
          filter,
          setFilter,
          environment,
          field: props.field,
          showNullConditionFilter: props.showNullConditionFilter
        })]
      });
    },
    children: /* @__PURE__ */ jsx(SugaredField, {
      field: props.field,
      format: (value) => {
        if (value === null) {
          return /* @__PURE__ */ jsx(FieldFallbackView, {
            fallback: props.fallback,
            fallbackStyle: props.fallbackStyle
          });
        }
        if (props.format) {
          return props.format(props.options[value]);
        }
        return props.options[value];
      }
    })
  });
}, "EnumCell");
export {
  EnumCell
};
//# sourceMappingURL=EnumCell.js.map
