import { jsxDEV, Fragment } from "react/jsx-dev-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__ */ jsxDEV(Fragment, { children: /* @__PURE__ */ jsxDEV(
    DialogModal,
    {
      layout: "wide",
      onClose: onCancel,
      header: /* @__PURE__ */ jsxDEV(Fragment, { children: /* @__PURE__ */ jsxDEV(Heading, { children: heading }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/selection/SelectFileDialog.tsx",
        lineNumber: 72,
        columnNumber: 5
      }, void 0) }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/selection/SelectFileDialog.tsx",
        lineNumber: 71,
        columnNumber: 12
      }, void 0),
      footer: /* @__PURE__ */ jsxDEV(Fragment, { children: /* @__PURE__ */ jsxDEV(
        Button,
        {
          distinction: "primary",
          onClick: onConfirm,
          children: formatMessage(insertSelectedText, "upload.insertSelected.text")
        },
        void 0,
        false,
        {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/selection/SelectFileDialog.tsx",
          lineNumber: 75,
          columnNumber: 5
        },
        void 0
      ) }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/selection/SelectFileDialog.tsx",
        lineNumber: 74,
        columnNumber: 12
      }, void 0),
      children: /* @__PURE__ */ jsxDEV(
        Component,
        {
          ...dialogProps,
          onToggleSelectConnector: onToggleSelect,
          selectedEntityIds
        },
        void 0,
        false,
        {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/selection/SelectFileDialog.tsx",
          lineNumber: 83,
          columnNumber: 4
        },
        void 0
      )
    },
    void 0,
    false,
    {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/selection/SelectFileDialog.tsx",
      lineNumber: 68,
      columnNumber: 3
    },
    void 0
  ) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/selection/SelectFileDialog.tsx",
    lineNumber: 67,
    columnNumber: 9
  }, void 0);
};
export {
  SelectFileDialog
};
//# sourceMappingURL=SelectFileDialog.js.map
