import { createElement, useCallback, Fragment } from "react";
import { Component, QueryLanguage, wrapFilterInHasOnes, SugaredField } from "@contember/binding";
import { GraphQlLiteral } from "@contember/client";
import { DataGridColumn } from "../base/DataGridColumn.js";
const EnumCell = Component((props) => {
  return /* @__PURE__ */ createElement(DataGridColumn, {
    ...props,
    enableOrdering: true,
    getNewOrderBy: (newDirection, { environment }) => newDirection ? QueryLanguage.desugarOrderBy(`${props.field} ${newDirection}`, environment) : void 0,
    enableFiltering: true,
    getNewFilter: (filterArtefact, { environment }) => {
      if (filterArtefact.length === 0) {
        return void 0;
      }
      const desugared = QueryLanguage.desugarRelativeSingleField(props.field, environment);
      return wrapFilterInHasOnes(desugared.hasOneRelationPath, {
        [desugared.field]: {
          in: filterArtefact.map((it) => new GraphQlLiteral(it))
        }
      });
    },
    emptyFilter: [],
    filterRenderer: ({ filter, setFilter, environment }) => {
      const onChange = useCallback((event) => {
        if (event.target.checked) {
          setFilter([...filter, event.target.value]);
        } else {
          setFilter(filter.filter((it) => it !== event.target.value));
        }
      }, [filter, setFilter]);
      const checkboxList = Object.entries(props.options).map(([value, label]) => /* @__PURE__ */ createElement("label", {
        key: value,
        style: { display: "block" }
      }, /* @__PURE__ */ createElement("input", {
        type: "checkbox",
        value,
        checked: filter.includes(value),
        onChange
      }), label));
      return /* @__PURE__ */ createElement(Fragment, null, checkboxList);
    }
  }, /* @__PURE__ */ createElement(SugaredField, {
    field: props.field,
    format: (value) => value ? props.options[value] : ""
  }));
}, "EnumCell");
export { EnumCell };
//# sourceMappingURL=EnumCell.js.map
