"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/rich-text/native/use-format-types.js
var use_format_types_exports = {};
__export(use_format_types_exports, {
  useFormatTypes: () => useFormatTypes
});
module.exports = __toCommonJS(use_format_types_exports);
var import_element = require("@wordpress/element");
var import_data = require("@wordpress/data");
var import_rich_text = require("@wordpress/rich-text");
function formatTypesSelector(select) {
  return select(import_rich_text.store).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 = (0, import_data.useSelect)(formatTypesSelector, []);
  const formatTypes = (0, import_element.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 = (0, import_data.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 = (0, import_data.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
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useFormatTypes
});
//# sourceMappingURL=use-format-types.js.map
