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 { jsxDEV } from "react/jsx-dev-runtime";
import { DataGridColumn } from "../base/DataGridColumn.js";
import { useMessageFormatter } from "../../../../../i18n/useMessageFormatter.js";
import { FieldFallbackView } from "../../../fieldViews/FieldFallbackView.js";
var _jsxFileName = "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/TextCell.tsx";
const TextCell = Component((props) => {
  return /* @__PURE__ */ jsxDEV(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__ */ jsxDEV(Stack, {
        direction: "horizontal",
        children: [/* @__PURE__ */ jsxDEV(GenericTextCellFilter, {
          ...props2,
          filter,
          setFilter
        }, void 0, false, {
          fileName: _jsxFileName,
          lineNumber: 74,
          columnNumber: 7
        }, void 0), /* @__PURE__ */ jsxDEV(FieldContainer, {
          label: /* @__PURE__ */ jsxDEV("span", {
            style: {
              whiteSpace: "nowrap"
            },
            children: filter.mode === "doesNotMatch" ? formatMessage("dataGridCells.textCell.excludeNull", {
              strong: (chunks) => /* @__PURE__ */ jsxDEV("strong", {
                children: chunks
              }, void 0, false, {
                fileName: _jsxFileName,
                lineNumber: 79,
                columnNumber: 31
              }, void 0)
            }) : formatMessage("dataGridCells.textCell.includeNull", {
              strong: (chunks) => /* @__PURE__ */ jsxDEV("strong", {
                children: chunks
              }, void 0, false, {
                fileName: _jsxFileName,
                lineNumber: 82,
                columnNumber: 31
              }, void 0)
            })
          }, void 0, false, {
            fileName: _jsxFileName,
            lineNumber: 76,
            columnNumber: 16
          }, void 0),
          labelPosition: "labelInlineRight",
          children: /* @__PURE__ */ jsxDEV(Checkbox, {
            notNull: true,
            value: filter.nullCondition,
            onChange: (checked) => {
              setFilter({
                ...filter,
                nullCondition: !!checked
              });
            }
          }, void 0, false, {
            fileName: _jsxFileName,
            lineNumber: 87,
            columnNumber: 9
          }, void 0)
        }, void 0, false, {
          fileName: _jsxFileName,
          lineNumber: 75,
          columnNumber: 8
        }, void 0)]
      }, void 0, true, {
        fileName: _jsxFileName,
        lineNumber: 73,
        columnNumber: 6
      }, void 0);
    },
    children: /* @__PURE__ */ jsxDEV(Field, {
      ...props,
      format: (value) => {
        if (value === null) {
          return /* @__PURE__ */ jsxDEV(FieldFallbackView, {
            fallback: props.fallback,
            fallbackStyle: props.fallbackStyle
          }, void 0, false, {
            fileName: _jsxFileName,
            lineNumber: 106,
            columnNumber: 14
          }, void 0);
        }
        if (props.format) {
          return props.format(value);
        }
        return value;
      }
    }, void 0, false, {
      fileName: _jsxFileName,
      lineNumber: 102,
      columnNumber: 4
    }, void 0)
  }, void 0, false, {
    fileName: _jsxFileName,
    lineNumber: 35,
    columnNumber: 3
  }, void 0);
}, "TextCell");
export {
  TextCell
};
//# sourceMappingURL=TextCell.js.map
