"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const reactUtils = require("@contember/react-utils");
const ui = require("@contember/ui");
const react = require("react");
const SelectFileDialog = ({
  onSelect,
  onCancel,
  formatMessage,
  insertSelectedText,
  selectButtonText,
  isMultiple,
  dialogComponent: Component,
  dialogProps
}) => {
  const heading = reactUtils.useChildrenAsLabel(selectButtonText);
  const [selectedEntities, setEntities] = react.useState([]);
  const onToggleSelect = react.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 = react.useCallback(() => {
    onSelect(selectedEntities);
  }, [onSelect, selectedEntities]);
  const selectedEntityIds = react.useMemo(() => selectedEntities.map((it) => it.entity.id), [selectedEntities]);
  return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: /* @__PURE__ */ jsxRuntime.jsx(
    ui.DialogModal,
    {
      layout: "wide",
      onClose: onCancel,
      header: /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: heading }) }),
      footer: /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: /* @__PURE__ */ jsxRuntime.jsx(
        ui.Button,
        {
          distinction: "primary",
          onClick: onConfirm,
          children: formatMessage(insertSelectedText, "upload.insertSelected.text")
        }
      ) }),
      children: /* @__PURE__ */ jsxRuntime.jsx(
        Component,
        {
          ...dialogProps,
          onToggleSelectConnector: onToggleSelect,
          selectedEntityIds
        }
      )
    }
  ) });
};
exports.SelectFileDialog = SelectFileDialog;
//# sourceMappingURL=SelectFileDialog.cjs.map
