/*
 * 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 classnames from "classnames";
import React, { memo, useCallback } from "react";
import { DatePicker } from "../../../shared/calendar/index.js";
import { formatDateForDisplay, formatDateForInput } from "../../../shared/dateUtils.js";
import styles from "./MultiDateInput.module.css";
import sharedStyles from "./shared.module.css";
function MultiDateInputInner({
  selectedDates,
  onChange,
  className,
  style,
  minDate,
  maxDate,
  showClearAll = true,
  formatDate
}) {
  const addDate = useCallback(date => {
    if (date == null) return;
    const dateStr = formatDateForInput(date);
    const exists = selectedDates.some(d => formatDateForInput(d) === dateStr);
    if (!exists) {
      onChange([...selectedDates, date].sort((a, b) => a.getTime() - b.getTime()));
    }
  }, [selectedDates, onChange]);
  const removeDate = useCallback(date => {
    const dateStr = formatDateForInput(date);
    onChange(selectedDates.filter(d => formatDateForInput(d) !== dateStr));
  }, [selectedDates, onChange]);
  const clearAll = useCallback(() => {
    onChange([]);
  }, [onChange]);
  return /*#__PURE__*/React.createElement("div", {
    className: classnames(styles.multiDate, className),
    style: style
  }, selectedDates.length > 0 && /*#__PURE__*/React.createElement("div", {
    className: sharedStyles.tagContainer
  }, selectedDates.map(date => /*#__PURE__*/React.createElement(DateTag, {
    key: date.toISOString(),
    date: date,
    formatDate: formatDate,
    onRemove: removeDate
  })), showClearAll && selectedDates.length > 1 && /*#__PURE__*/React.createElement(Button, {
    className: styles.clearAll,
    onClick: clearAll,
    "aria-label": "Clear all dates"
  }, "Clear all")), /*#__PURE__*/React.createElement("div", {
    className: styles.calendarContainer
  }, /*#__PURE__*/React.createElement(DatePicker, {
    value: null,
    onChange: addDate,
    min: minDate,
    max: maxDate,
    ariaLabel: "Add date",
    modal: false,
    formatDate: formatDate
  })));
}
export const MultiDateInput = /*#__PURE__*/memo(MultiDateInputInner);
function DateTag({
  date,
  formatDate,
  onRemove
}) {
  const handleRemove = useCallback(() => {
    onRemove(date);
  }, [onRemove, date]);
  const label = formatDate != null ? formatDate(date) : formatDateForDisplay(date);
  return /*#__PURE__*/React.createElement("span", {
    className: sharedStyles.tag
  }, label, /*#__PURE__*/React.createElement(Button, {
    className: sharedStyles.tagRemove,
    onClick: handleRemove,
    "aria-label": `Remove ${label}`
  }, "\xD7"));
}
//# sourceMappingURL=MultiDateInput.js.map