import { jsx, Fragment } from "react/jsx-runtime";
import { useChildrenAsLabel } from "@contember/react-utils";
import { DialogModal, Heading, Button } from "@contember/ui";
import { useState, useCallback, useMemo } from "react";
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__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(
    DialogModal,
    {
      layout: "wide",
      onClose: onCancel,
      header: /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(Heading, { children: heading }) }),
      footer: /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(
        Button,
        {
          distinction: "primary",
          onClick: onConfirm,
          children: formatMessage(insertSelectedText, "upload.insertSelected.text")
        }
      ) }),
      children: /* @__PURE__ */ jsx(
        Component,
        {
          ...dialogProps,
          onToggleSelectConnector: onToggleSelect,
          selectedEntityIds
        }
      )
    }
  ) });
};
export {
  SelectFileDialog
};
//# sourceMappingURL=SelectFileDialog.js.map
