import { Component, QueryLanguage, wrapFilterInHasOnes, Field } from "@contember/binding";
import { Stack, Select, NumberInput } from "@contember/ui";
import { dataGridCellsDictionary } from "./dataGridCellsDictionary.js";
import { NullConditionFilter } from "./NullConditionFilter.js";
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
import { DataGridColumn } from "../base/DataGridColumn.js";
import { useMessageFormatter } from "../../../../../i18n/useMessageFormatter.js";
import { FieldFallbackView } from "../../../fieldViews/FieldFallbackView.js";
const NumberCell = 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 === null && !filter.nullCondition) {
        return void 0;
      }
      const baseOperators = {
        eq: "eq",
        gte: "gte",
        lte: "lte"
      };
      const conditions = [];
      if (filter.query !== null) {
        conditions.push({
          [baseOperators[filter.mode]]: filter.query
        });
      }
      if (filter.nullCondition) {
        conditions.push({
          isNull: true
        });
      }
      const desugared = QueryLanguage.desugarRelativeSingleField(props, environment);
      return wrapFilterInHasOnes(desugared.hasOneRelationPath, {
        [desugared.field]: {
          or: conditions
        }
      });
    },
    emptyFilter: {
      mode: "eq",
      query: null,
      nullCondition: false
    },
    filterRenderer: ({
      filter,
      setFilter,
      environment
    }) => {
      const formatMessage = useMessageFormatter(dataGridCellsDictionary);
      const options = [{
        value: "eq",
        label: formatMessage("dataGridCells.numberCell.equals")
      }, {
        value: "gte",
        label: formatMessage("dataGridCells.numberCell.greaterThan")
      }, {
        value: "lte",
        label: formatMessage("dataGridCells.numberCell.lessThan")
      }];
      return /* @__PURE__ */ jsxs(Fragment, {
        children: [/* @__PURE__ */ jsxs(Stack, {
          direction: "horizontal",
          children: [/* @__PURE__ */ jsx(Select, {
            notNull: true,
            value: filter.mode,
            options,
            onChange: (value) => {
              if (!value) {
                return;
              }
              setFilter({
                ...filter,
                mode: value
              });
            }
          }), /* @__PURE__ */ jsx(NumberInput, {
            value: filter.query,
            placeholder: "Value",
            onChange: (value) => {
              setFilter({
                ...filter,
                query: value ?? null
              });
            }
          })]
        }), /* @__PURE__ */ jsx(NullConditionFilter, {
          filter,
          setFilter,
          environment,
          field: props.field,
          showNullConditionFilter: props.showNullConditionFilter
        })]
      });
    },
    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;
      }
    })
  });
}, "NumberCell");
export {
  NumberCell
};
//# sourceMappingURL=NumberCell.js.map
