import { createElement, useCallback, memo, forwardRef } from "react";
import { Component, QueryLanguage, wrapFilterInHasOnes } from "@contember/binding";
import { toDateString, Stack, DateInput, FieldContainer } from "@contember/ui";
import { dataGridCellsDictionary } from "./dataGridCellsDictionary.js";
import { dateToStringWithoutTimezone } from "../../../../../utils/dateToStringWithoutTimezone.js";
import { DateFieldView } from "../../../fieldViews/DateFieldView.js";
import { DataGridColumn } from "../base/DataGridColumn.js";
import { useMessageFormatter } from "../../../../../i18n/useMessageFormatter.js";
const DateCell = Component((props) => {
  return /* @__PURE__ */ createElement(DataGridColumn, {
    ...props,
    enableOrdering: !props.disableOrder,
    getNewOrderBy: (newDirection, { environment }) => newDirection ? QueryLanguage.desugarOrderBy(`${props.field} ${newDirection}`, environment) : void 0,
    getNewFilter: (filterArtifact, { environment }) => {
      if (!filterArtifact.start && !filterArtifact.end) {
        return void 0;
      }
      const desugared = QueryLanguage.desugarRelativeSingleField(props.field, environment);
      const conditions = [];
      if (filterArtifact.start) {
        conditions.push({ gte: filterArtifact.start });
      }
      if (filterArtifact.end) {
        conditions.push({ lte: filterArtifact.end });
      }
      return wrapFilterInHasOnes(desugared.hasOneRelationPath, {
        [desugared.field]: conditions.length > 1 ? { and: conditions } : conditions[0]
      });
    },
    emptyFilter: {
      start: null,
      end: null
    },
    filterRenderer: ({ filter, setFilter }) => {
      const formatMessage = useMessageFormatter(dataGridCellsDictionary);
      const start = toDateString(filter.start) ?? "";
      const end = toDateString(filter.end) ?? "";
      const onDateStartChange = useCallback((value) => {
        setFilter({
          ...filter,
          start: value ? dateToStringWithoutTimezone(new Date(value)) : null
        });
      }, [filter, setFilter]);
      const onDateEndChange = useCallback((value) => {
        setFilter({
          ...filter,
          end: value ? dateToStringWithoutTimezone(new Date(value)) : null
        });
      }, [filter, setFilter]);
      return /* @__PURE__ */ createElement(Stack, {
        direction: "horizontal"
      }, /* @__PURE__ */ createElement(DateBoundInput, {
        label: formatMessage("dataGridCells.dateCell.fromLabel")
      }, /* @__PURE__ */ createElement(DateInput, {
        value: start,
        onChange: onDateStartChange,
        max: end
      })), /* @__PURE__ */ createElement(DateBoundInput, {
        label: formatMessage("dataGridCells.dateCell.toLabel")
      }, /* @__PURE__ */ createElement(DateInput, {
        value: end,
        onChange: onDateEndChange,
        min: start
      })));
    }
  }, /* @__PURE__ */ createElement(DateFieldView, {
    ...props
  }));
}, "DateCell");
const DateBoundInput = memo(forwardRef(({ label, children }, ref) => /* @__PURE__ */ createElement(FieldContainer, {
  label,
  labelPosition: "labelInlineLeft"
}, children)));
export { DateCell };
//# sourceMappingURL=DateCell.js.map
