/*
 * Copyright 2026 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 { Cross } from "@blueprintjs/icons";
import classnames from "classnames";
import React, { memo, useCallback, useMemo, useRef } from "react";
import { ActionButton } from "../../base-components/action-button/ActionButton.js";
import styles from "./FilePickerField.module.css.js";
export const FilePickerField = /*#__PURE__*/memo(function ({
  id,
  value,
  onChange,
  error,
  isMulti,
  accept,
  // maxSize is enforced by form-level validation (extractValidationRules),
  // not here. Silently dropping oversized files would leave the user with
  // no indication of why their selection disappeared.
  maxSize: _maxSize,
  text = "No file chosen",
  buttonText = "Browse",
  disabled
}) {
  const inputRef = useRef(null);
  const fileTriggerRef = useRef(null);
  const openFileDialog = useCallback(() => {
    inputRef.current?.click();
  }, []);
  const focusFileTrigger = useCallback(() => {
    fileTriggerRef.current?.focus();
  }, []);
  const handleBrowsePointerDown = useCallback(event => {
    // Browse is a visual affordance inside the field, not a separate tab
    // stop; keep the field trigger as the focused element and focus ring.
    event.preventDefault();
    focusFileTrigger();
  }, [focusFileTrigger]);
  const handleBrowseClick = useCallback(() => {
    focusFileTrigger();
    openFileDialog();
  }, [focusFileTrigger, openFileDialog]);
  const handleInputChange = useCallback(event => {
    const files = event.target.files;
    if (files == null || files.length === 0) {
      onChange?.(null);
      return;
    }
    if (isMulti) {
      onChange?.(Array.from(files));
    } else {
      onChange?.(files[0] ?? null);
    }
  }, [onChange, isMulti]);
  const handleClear = useCallback(event => {
    event.preventDefault();
    onChange?.(null);
    if (inputRef.current != null) {
      inputRef.current.value = "";
    }
  }, [onChange]);
  const handleKeyDown = useCallback(event => {
    if (event.key === "Enter" || event.key === " ") {
      event.preventDefault();
      openFileDialog();
    }
  }, [openFileDialog]);
  const displayText = useMemo(() => getDisplayText(value), [value]);
  const hasValue = displayText != null;
  const acceptString = useMemo(() => normalizeAccept(accept), [accept]);
  return /*#__PURE__*/React.createElement("div", {
    className: styles.osdkFilePickerTrigger,
    "aria-invalid": error != null || undefined
  }, /*#__PURE__*/React.createElement("input", {
    ref: inputRef,
    type: "file",
    className: styles.osdkFilePickerHiddenInput,
    multiple: isMulti,
    accept: acceptString,
    onChange: handleInputChange,
    tabIndex: -1,
    disabled: disabled
  }), /*#__PURE__*/React.createElement(Button, {
    ref: fileTriggerRef,
    id: id,
    type: "button",
    className: classnames(styles.osdkFilePickerText, !hasValue && styles.osdkFilePickerPlaceholder),
    onClick: openFileDialog,
    onKeyDown: handleKeyDown,
    "aria-label": "Choose file",
    "aria-invalid": error != null || undefined,
    disabled: disabled
  }, displayText ?? text), hasValue && /*#__PURE__*/React.createElement(Button, {
    type: "button",
    className: styles.osdkFilePickerClear,
    onClick: handleClear,
    "aria-label": "Clear selection",
    disabled: disabled
  }, /*#__PURE__*/React.createElement(Cross, null)), /*#__PURE__*/React.createElement(ActionButton, {
    type: "button",
    tabIndex: -1,
    className: styles.osdkFilePickerBrowse,
    onPointerDown: handleBrowsePointerDown,
    onClick: handleBrowseClick,
    disabled: disabled
  }, buttonText));
});
function normalizeAccept(accept) {
  if (accept == null) {
    return undefined;
  }
  return Array.isArray(accept) ? accept.join(",") : accept;
}
function getDisplayText(value) {
  if (value == null) {
    return undefined;
  }
  if (Array.isArray(value)) {
    return value.map(f => f.name).join(", ");
  }
  return value.name;
}
//# sourceMappingURL=FilePickerField.js.map