import { jsx, jsxs } from "react/jsx-runtime";
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__ */ jsx(
    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__ */ jsxs(Stack, { direction: "horizontal", children: [
          /* @__PURE__ */ jsx(GenericTextCellFilter, { ...props2, filter, setFilter }),
          /* @__PURE__ */ jsx(
            FieldContainer,
            {
              label: /* @__PURE__ */ jsx("span", { style: { whiteSpace: "nowrap" }, children: filter.mode === "doesNotMatch" ? formatMessage("dataGridCells.textCell.excludeNull", {
                strong: (chunks) => /* @__PURE__ */ jsx("strong", { children: chunks })
              }) : formatMessage("dataGridCells.textCell.includeNull", {
                strong: (chunks) => /* @__PURE__ */ jsx("strong", { children: chunks })
              }) }),
              labelPosition: "labelInlineRight",
              children: /* @__PURE__ */ jsx(
                Checkbox,
                {
                  notNull: true,
                  value: filter.nullCondition,
                  onChange: (checked) => {
                    setFilter({
                      ...filter,
                      nullCondition: !!checked
                    });
                  }
                }
              )
            }
          )
        ] });
      },
      children: /* @__PURE__ */ jsx(
        Field,
        {
          ...props,
          format: (value) => {
            if (value === null) {
              return /* @__PURE__ */ jsx(FieldFallbackView, { fallback: props.fallback, fallbackStyle: props.fallbackStyle });
            }
            if (props.format) {
              return props.format(value);
            }
            return value;
          }
        }
      )
    }
  );
}, "TextCell");
export {
  TextCell
};
//# sourceMappingURL=TextCell.js.map
