import { useCallback, createElement } from "react";
import { Component, useAccessorTreeState, AccessorTree } from "@contember/binding";
import { Button, useDialog, DropdownContentContainerProvider } from "@contember/ui";
import { SelectFileDialog } from "./SelectFileDialog.js";
const SelectFileInput = Component(({
  formatMessage,
  insertSelectedText,
  onSelectConfirm,
  selectButtonComponent: SelectButton = Button,
  selectButtonComponentExtraProps,
  selectButtonProps,
  selectButtonText: selectButtonTextProp,
  isMultiple,
  fileSelection
}) => {
  const { openDialog } = useDialog();
  const selectButtonText = formatMessage(selectButtonTextProp, "upload.selectButton.text");
  const accessorTree = useAccessorTreeState();
  const onClick = useCallback(async (event) => {
    event.stopPropagation();
    const selectedEntities = await openDialog({
      type: "captivating",
      bare: true,
      content: (props) => /* @__PURE__ */ createElement(DropdownContentContainerProvider, null, /* @__PURE__ */ createElement(AccessorTree, {
        state: accessorTree
      }, /* @__PURE__ */ createElement(SelectFileDialog, {
        formatMessage,
        onCancel: () => props.resolve(),
        onSelect: props.resolve,
        insertSelectedText,
        selectButtonText,
        isMultiple,
        ...fileSelection
      })))
    });
    if (selectedEntities !== void 0) {
      onSelectConfirm(selectedEntities);
    }
  }, [accessorTree, fileSelection, formatMessage, insertSelectedText, isMultiple, onSelectConfirm, openDialog, selectButtonText]);
  return /* @__PURE__ */ createElement(SelectButton, {
    ...selectButtonComponentExtraProps,
    size: "small",
    children: selectButtonText,
    onClick,
    ...selectButtonProps
  });
}, () => {
  return null;
}, "SelectFileInput");
export { SelectFileInput };
//# sourceMappingURL=SelectFileInput.js.map
