import { createElement } from "react";
import { Component, QueryLanguage, wrapFilterInHasOnes, Field } from "@contember/binding";
import { Stack, FieldContainer, Checkbox } from "@contember/ui";
import { dataGridCellsDictionary } from "./dataGridCellsDictionary.js";
import { createGenericTextCellFilterCondition, GenericTextCellFilter } from "./GenericTextCellFilter.js";
import { DataGridColumn } from "../base/DataGridColumn.js";
import { useMessageFormatter } from "../../../../../i18n/useMessageFormatter.js";
import { FieldFallbackView } from "../../../fieldViews/FieldFallbackView.js";
const TextCell = Component((props) => {
  return /* @__PURE__ */ createElement(DataGridColumn, {
    ...props,
    enableOrdering: !props.disableOrder,
    getNewOrderBy: (newDirection, { environment }) => newDirection ? QueryLanguage.desugarOrderBy(`${props.field} ${newDirection}`, environment) : void 0,
    getNewFilter: (filter, { environment }) => {
      if (filter.query === "" && filter.nullCondition === false) {
        return void 0;
      }
      let condition = filter.query !== "" ? createGenericTextCellFilterCondition(filter) : {};
      if (filter.mode === "doesNotMatch") {
        if (filter.nullCondition) {
          condition = {
            and: [condition, { isNull: false }]
          };
        }
      } else if (filter.nullCondition) {
        condition = {
          or: [condition, { isNull: true }]
        };
      }
      const desugared = QueryLanguage.desugarRelativeSingleField(props, environment);
      return wrapFilterInHasOnes(desugared.hasOneRelationPath, {
        [desugared.field]: condition
      });
    },
    emptyFilter: {
      mode: "matches",
      query: "",
      nullCondition: false
    },
    filterRenderer: ({ filter, setFilter, ...props2 }) => {
      const formatMessage = useMessageFormatter(dataGridCellsDictionary);
      return /* @__PURE__ */ createElement(Stack, {
        direction: "horizontal"
      }, /* @__PURE__ */ createElement(GenericTextCellFilter, {
        ...props2,
        filter,
        setFilter
      }), /* @__PURE__ */ createElement(FieldContainer, {
        label: /* @__PURE__ */ createElement("span", {
          style: { whiteSpace: "nowrap" }
        }, filter.mode === "doesNotMatch" ? formatMessage("dataGridCells.textCell.excludeNull", {
          strong: (chunks) => /* @__PURE__ */ createElement("strong", null, chunks)
        }) : formatMessage("dataGridCells.textCell.includeNull", {
          strong: (chunks) => /* @__PURE__ */ createElement("strong", null, chunks)
        })),
        labelPosition: "labelInlineRight"
      }, /* @__PURE__ */ createElement(Checkbox, {
        notNull: true,
        value: filter.nullCondition,
        onChange: (checked) => {
          setFilter({
            ...filter,
            nullCondition: !!checked
          });
        }
      })));
    }
  }, /* @__PURE__ */ createElement(Field, {
    ...props,
    format: (value) => {
      if (value === null) {
        return /* @__PURE__ */ createElement(FieldFallbackView, {
          fallback: props.fallback,
          fallbackStyle: props.fallbackStyle
        });
      }
      if (props.format) {
        return props.format(value);
      }
      return value;
    }
  }));
}, "TextCell");
export { TextCell };
//# sourceMappingURL=TextCell.js.map
