/*
 * Copyright 2025 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Button } from "@base-ui/react/button";
import { Input } from "@base-ui/react/input";
import classnames from "classnames";
import React, { memo, useCallback, useMemo } from "react";
import { formatDateForDisplay, formatDateForInput, parseDateFromInput } from "../../../shared/dateUtils.js";
import styles from "./TimelineInput.module.css";
function TimelineInputInner({
  startDate,
  endDate,
  onChange,
  className,
  minDate,
  maxDate,
  formatDate
}) {
  const renderDate = (date, fallback) => date == null ? fallback : formatDate ? formatDate(date) : formatDateForDisplay(date, fallback);
  const handleStartChange = useCallback(e => {
    const date = parseDateFromInput(e.target.value);
    onChange(date, endDate);
  }, [onChange, endDate]);
  const handleEndChange = useCallback(e => {
    const date = parseDateFromInput(e.target.value);
    onChange(startDate, date);
  }, [onChange, startDate]);
  const handleClear = useCallback(() => {
    onChange(undefined, undefined);
  }, [onChange]);
  const startInputMax = useMemo(() => endDate ?? maxDate, [endDate, maxDate]);
  const endInputMin = useMemo(() => startDate ?? minDate, [startDate, minDate]);
  return /*#__PURE__*/React.createElement("div", {
    className: classnames(styles.timeline, className)
  }, /*#__PURE__*/React.createElement("div", {
    className: styles.labels
  }, /*#__PURE__*/React.createElement("span", null, renderDate(startDate, "—")), /*#__PURE__*/React.createElement("span", null, "to"), /*#__PURE__*/React.createElement("span", null, renderDate(endDate, "—")), (startDate || endDate) && /*#__PURE__*/React.createElement(Button, {
    type: "button",
    onClick: handleClear,
    "aria-label": "Clear range",
    className: styles.clearButton
  }, "\xD7")), /*#__PURE__*/React.createElement("div", {
    className: styles.brush
  }, /*#__PURE__*/React.createElement(Input, {
    type: "date",
    className: styles.input,
    value: formatDateForInput(startDate),
    onChange: handleStartChange,
    min: minDate ? formatDateForInput(minDate) : undefined,
    max: startInputMax ? formatDateForInput(startInputMax) : undefined,
    "aria-label": "Start date"
  }), /*#__PURE__*/React.createElement("span", null, "\u2014"), /*#__PURE__*/React.createElement(Input, {
    type: "date",
    className: styles.input,
    value: formatDateForInput(endDate),
    onChange: handleEndChange,
    min: endInputMin ? formatDateForInput(endInputMin) : undefined,
    max: maxDate ? formatDateForInput(maxDate) : undefined,
    "aria-label": "End date"
  })));
}
export const TimelineInput = /*#__PURE__*/memo(TimelineInputInner);
//# sourceMappingURL=TimelineInput.js.map