import { useState, useCallback, useMemo, createElement, Fragment } from "react";
import { useChildrenAsLabel, DialogModal, Heading, Button } from "@contember/ui";
const SelectFileDialog = ({
  onSelect,
  onCancel,
  formatMessage,
  insertSelectedText,
  selectButtonText,
  isMultiple,
  dialogComponent: Component,
  dialogProps
}) => {
  const heading = useChildrenAsLabel(selectButtonText);
  const [selectedEntities, setEntities] = useState([]);
  const onToggleSelect = useCallback((entity) => {
    setEntities((current) => {
      const index = current.findIndex((it) => it.entity.id === entity.entity.id);
      if (index >= 0) {
        const newValues = [...current];
        newValues.splice(index, 1);
        return newValues;
      }
      if (isMultiple) {
        return [...current, entity];
      } else {
        return [entity];
      }
    });
  }, [isMultiple]);
  const onConfirm = useCallback(() => {
    onSelect(selectedEntities);
  }, [onSelect, selectedEntities]);
  const selectedEntityIds = useMemo(() => selectedEntities.map((it) => it.entity.id), [selectedEntities]);
  return /* @__PURE__ */ createElement(Fragment, null, /* @__PURE__ */ createElement(DialogModal, {
    layout: "wide",
    onClose: onCancel,
    header: /* @__PURE__ */ createElement(Fragment, null, /* @__PURE__ */ createElement(Heading, null, heading)),
    footer: /* @__PURE__ */ createElement(Fragment, null, /* @__PURE__ */ createElement(Button, {
      distinction: "primary",
      onClick: onConfirm
    }, formatMessage(insertSelectedText, "upload.insertSelected.text")))
  }, /* @__PURE__ */ createElement(Component, {
    ...dialogProps,
    onToggleSelectConnector: onToggleSelect,
    selectedEntityIds
  })));
};
export { SelectFileDialog };
//# sourceMappingURL=SelectFileDialog.js.map
