import { Component, QueryLanguage, wrapFilterInHasOnes } from "@contember/binding";
import { toDateString, Stack, DateInput, FieldContainer } from "@contember/ui";
import { useCallback, memo, forwardRef } from "react";
import { dataGridCellsDictionary } from "./dataGridCellsDictionary.js";
import { jsx, jsxs } from "react/jsx-runtime";
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__ */ jsx(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__ */ jsxs(Stack, {
        direction: "horizontal",
        children: [/* @__PURE__ */ jsx(DateBoundInput, {
          label: formatMessage("dataGridCells.dateCell.fromLabel"),
          children: /* @__PURE__ */ jsx(DateInput, {
            value: start,
            onChange: onDateStartChange,
            max: end
          })
        }), /* @__PURE__ */ jsx(DateBoundInput, {
          label: formatMessage("dataGridCells.dateCell.toLabel"),
          children: /* @__PURE__ */ jsx(DateInput, {
            value: end,
            onChange: onDateEndChange,
            min: start
          })
        })]
      });
    },
    children: /* @__PURE__ */ jsx(DateFieldView, {
      ...props
    })
  });
}, "DateCell");
const DateBoundInput = memo(forwardRef(({
  label,
  children
}, ref) => /* @__PURE__ */ jsx(FieldContainer, {
  label,
  labelPosition: "labelInlineLeft",
  children
})));
export {
  DateCell
};
//# sourceMappingURL=DateCell.js.map
