// packages/block-editor/src/components/provider/index.js
import { useDispatch } from "@wordpress/data";
import { useEffect, useMemo } from "@wordpress/element";
import { SlotFillProvider } from "@wordpress/components";
import {
  MediaUploadProvider,
  store as uploadStore
} from "@wordpress/upload-media";
import withRegistryProvider from "./with-registry-provider";
import useBlockSync from "./use-block-sync";
import { store as blockEditorStore } from "../../store";
import { BlockRefsProvider } from "./block-refs-provider";
import { unlock } from "../../lock-unlock";
import KeyboardShortcuts from "../keyboard-shortcuts";
import useMediaUploadSettings from "./use-media-upload-settings";
import { jsx, jsxs } from "react/jsx-runtime";
var noop = () => {
};
function mediaUpload(registry, {
  allowedTypes,
  additionalData = {},
  filesList,
  onError = noop,
  onFileChange,
  onSuccess,
  onBatchSuccess
}) {
  void registry.dispatch(uploadStore).addItems({
    files: filesList,
    onChange: onFileChange,
    onSuccess,
    onBatchSuccess,
    onError: ({ message }) => onError(message),
    additionalData,
    allowedTypes
  });
}
var ExperimentalBlockEditorProvider = withRegistryProvider(
  (props) => {
    const {
      settings: _settings,
      registry,
      stripExperimentalSettings = false
    } = props;
    const mediaUploadSettings = useMediaUploadSettings(_settings);
    let settings = _settings;
    if (window.__experimentalMediaProcessing && _settings.mediaUpload) {
      settings = useMemo(
        () => ({
          ..._settings,
          mediaUpload: mediaUpload.bind(null, registry)
        }),
        [_settings, registry]
      );
    }
    const { __experimentalUpdateSettings } = unlock(
      useDispatch(blockEditorStore)
    );
    useEffect(() => {
      __experimentalUpdateSettings(
        {
          ...settings,
          __internalIsInitialized: true
        },
        {
          stripExperimentalSettings,
          reset: true
        }
      );
    }, [
      settings,
      stripExperimentalSettings,
      __experimentalUpdateSettings
    ]);
    useBlockSync(props);
    const children = /* @__PURE__ */ jsxs(SlotFillProvider, { passthrough: true, children: [
      !settings?.isPreviewMode && /* @__PURE__ */ jsx(KeyboardShortcuts.Register, {}),
      /* @__PURE__ */ jsx(BlockRefsProvider, { children: props.children })
    ] });
    if (window.__experimentalMediaProcessing) {
      return /* @__PURE__ */ jsx(
        MediaUploadProvider,
        {
          settings: mediaUploadSettings,
          useSubRegistry: false,
          children
        }
      );
    }
    return children;
  }
);
var BlockEditorProvider = (props) => {
  return /* @__PURE__ */ jsx(ExperimentalBlockEditorProvider, { ...props, stripExperimentalSettings: true, children: props.children });
};
var provider_default = BlockEditorProvider;
export {
  BlockEditorProvider,
  ExperimentalBlockEditorProvider,
  provider_default as default
};
//# sourceMappingURL=index.js.map
