// packages/block-editor/src/components/rich-text/native/use-format-types.js
import { useMemo } from "@wordpress/element";
import { useSelect, useDispatch } from "@wordpress/data";
import { store as richTextStore } from "@wordpress/rich-text";
function formatTypesSelector(select) {
  return select(richTextStore).getFormatTypes();
}
var interactiveContentTags = /* @__PURE__ */ new Set([
  "a",
  "audio",
  "button",
  "details",
  "embed",
  "iframe",
  "input",
  "label",
  "select",
  "textarea",
  "video"
]);
function useFormatTypes({
  clientId,
  identifier,
  withoutInteractiveFormatting,
  allowedFormats
}) {
  const allFormatTypes = useSelect(formatTypesSelector, []);
  const formatTypes = useMemo(() => {
    return allFormatTypes.filter(({ name, tagName }) => {
      if (allowedFormats && !allowedFormats.includes(name)) {
        return false;
      }
      if (withoutInteractiveFormatting && interactiveContentTags.has(tagName)) {
        return false;
      }
      return true;
    });
  }, [allFormatTypes, allowedFormats, interactiveContentTags]);
  const keyedSelected = useSelect(
    (select) => formatTypes.reduce((accumulator, type) => {
      if (type.__experimentalGetPropsForEditableTreePreparation) {
        accumulator[type.name] = type.__experimentalGetPropsForEditableTreePreparation(
          select,
          {
            richTextIdentifier: identifier,
            blockClientId: clientId
          }
        );
      }
      return accumulator;
    }, {}),
    [formatTypes, clientId, identifier]
  );
  const dispatch = useDispatch();
  const prepareHandlers = [];
  const valueHandlers = [];
  const changeHandlers = [];
  const dependencies = [];
  formatTypes.forEach((type) => {
    if (type.__experimentalCreatePrepareEditableTree) {
      const selected = keyedSelected[type.name];
      const handler = type.__experimentalCreatePrepareEditableTree(
        selected,
        {
          richTextIdentifier: identifier,
          blockClientId: clientId
        }
      );
      if (type.__experimentalCreateOnChangeEditableValue) {
        valueHandlers.push(handler);
      } else {
        prepareHandlers.push(handler);
      }
      for (const key in selected) {
        dependencies.push(selected[key]);
      }
    }
    if (type.__experimentalCreateOnChangeEditableValue) {
      let dispatchers = {};
      if (type.__experimentalGetPropsForEditableTreeChangeHandler) {
        dispatchers = type.__experimentalGetPropsForEditableTreeChangeHandler(
          dispatch,
          {
            richTextIdentifier: identifier,
            blockClientId: clientId
          }
        );
      }
      changeHandlers.push(
        type.__experimentalCreateOnChangeEditableValue(
          {
            ...keyedSelected[type.name] || {},
            ...dispatchers
          },
          {
            richTextIdentifier: identifier,
            blockClientId: clientId
          }
        )
      );
    }
  });
  return {
    formatTypes,
    prepareHandlers,
    valueHandlers,
    changeHandlers,
    dependencies
  };
}
export {
  useFormatTypes
};
//# sourceMappingURL=use-format-types.js.map
