import { createElement } from "react";
import { Component, QueryLanguage, wrapFilterInHasOnes } from "@contember/binding";
import { Stack, FieldContainer, Checkbox } from "@contember/ui";
import { dataGridCellsDictionary } from "./dataGridCellsDictionary.js";
import { BooleanFieldView } from "../../../fieldViews/BooleanFieldView.js";
import { DataGridColumn } from "../base/DataGridColumn.js";
import { useMessageFormatter } from "../../../../../i18n/useMessageFormatter.js";
const BooleanCell = Component((props) => {
  return /* @__PURE__ */ createElement(DataGridColumn, {
    shrunk: true,
    ...props,
    enableOrdering: !props.disableOrder,
    getNewOrderBy: (newDirection, { environment }) => newDirection ? QueryLanguage.desugarOrderBy(`${props.field} ${newDirection}`, environment) : void 0,
    getNewFilter: (filterArtifact, { environment }) => {
      const conditions = [];
      if (filterArtifact.includeTrue) {
        conditions.push({ eq: true });
      }
      if (filterArtifact.includeFalse) {
        conditions.push({ eq: false });
      }
      if (filterArtifact.includeNull) {
        conditions.push({ isNull: true });
      }
      if (conditions.length === 0 || conditions.length === 3) {
        return void 0;
      }
      const desugared = QueryLanguage.desugarRelativeSingleField(props.field, environment);
      return wrapFilterInHasOnes(desugared.hasOneRelationPath, {
        [desugared.field]: conditions.length > 1 ? { or: conditions } : conditions[0]
      });
    },
    emptyFilter: {
      includeFalse: false,
      includeTrue: false,
      includeNull: false
    },
    filterRenderer: ({ filter, setFilter }) => {
      const formatMessage = useMessageFormatter(dataGridCellsDictionary);
      return /* @__PURE__ */ createElement(Stack, {
        direction: "horizontal"
      }, [
        ["includeTrue", formatMessage("dataGridCells.booleanCell.includeTrue")],
        ["includeFalse", formatMessage("dataGridCells.booleanCell.includeFalse")],
        ["includeNull", formatMessage("dataGridCells.booleanCell.includeNull")]
      ].map(([item, label], index) => /* @__PURE__ */ createElement(FieldContainer, {
        key: `${index}-${label}`,
        label,
        labelPosition: "labelInlineRight"
      }, /* @__PURE__ */ createElement(Checkbox, {
        key: item,
        notNull: true,
        value: filter[item],
        onChange: (checked) => {
          setFilter({ ...filter, [item]: checked });
        }
      }))));
    }
  }, /* @__PURE__ */ createElement(BooleanFieldView, {
    ...props
  }));
}, "BooleanCell");
export { BooleanCell };
//# sourceMappingURL=BooleanCell.js.map
