import { jsxDEV } from "react/jsx-dev-runtime";
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 { 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__ */ jsxDEV(
    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__ */ jsxDEV(Stack, { direction: "horizontal", children: [
          /* @__PURE__ */ jsxDEV(DateBoundInput, { label: formatMessage("dataGridCells.dateCell.fromLabel"), children: /* @__PURE__ */ jsxDEV(
            DateInput,
            {
              value: start,
              onChange: onDateStartChange,
              max: end
            },
            void 0,
            false,
            {
              fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/DateCell.tsx",
              lineNumber: 87,
              columnNumber: 8
            },
            globalThis
          ) }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/DateCell.tsx",
            lineNumber: 86,
            columnNumber: 7
          }, globalThis),
          /* @__PURE__ */ jsxDEV(DateBoundInput, { label: formatMessage("dataGridCells.dateCell.toLabel"), children: /* @__PURE__ */ jsxDEV(
            DateInput,
            {
              value: end,
              onChange: onDateEndChange,
              min: start
            },
            void 0,
            false,
            {
              fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/DateCell.tsx",
              lineNumber: 94,
              columnNumber: 8
            },
            globalThis
          ) }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/DateCell.tsx",
            lineNumber: 93,
            columnNumber: 7
          }, globalThis)
        ] }, void 0, true, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/DateCell.tsx",
          lineNumber: 85,
          columnNumber: 6
        }, globalThis);
      },
      children: /* @__PURE__ */ jsxDEV(DateFieldView, { ...props }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/DateCell.tsx",
        lineNumber: 104,
        columnNumber: 4
      }, globalThis)
    },
    void 0,
    false,
    {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/DateCell.tsx",
      lineNumber: 36,
      columnNumber: 3
    },
    globalThis
  );
}, "DateCell");
const DateBoundInput = memo(
  forwardRef(({ label, children }, ref) => /* @__PURE__ */ jsxDEV(FieldContainer, { label, labelPosition: "labelInlineLeft", children }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/cells/DateCell.tsx",
    lineNumber: 111,
    columnNumber: 3
  }, globalThis))
);
export {
  DateCell
};
//# sourceMappingURL=DateCell.js.map
