"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/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");
var import_private_keys = require("../../store/private-keys");
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 prefixSelectKeys(selected, prefix) {
  if (typeof selected !== "object") {
    return { [prefix]: selected };
  }
  return Object.fromEntries(
    Object.entries(selected).map(([key, value]) => [
      `${prefix}.${key}`,
      value
    ])
  );
}
function getPrefixedSelectKeys(selected, prefix) {
  if (selected[prefix]) {
    return selected[prefix];
  }
  return Object.keys(selected).filter((key) => key.startsWith(prefix + ".")).reduce((accumulator, key) => {
    accumulator[key.slice(prefix.length + 1)] = selected[key];
    return accumulator;
  }, {});
}
function useFormatTypes({
  clientId,
  identifier,
  allowedFormats,
  withoutInteractiveFormatting,
  disableNoneEssentialFormatting = false
}) {
  const allFormatTypes = (0, import_data.useSelect)(formatTypesSelector, []);
  const formatTypes = (0, import_element.useMemo)(() => {
    return allFormatTypes.filter(
      ({
        name,
        interactive,
        tagName,
        [import_private_keys.essentialFormatKey]: isEssential
      }) => {
        if (allowedFormats && !allowedFormats.includes(name)) {
          return false;
        }
        if (disableNoneEssentialFormatting && !isEssential) {
          return false;
        }
        if (withoutInteractiveFormatting && (interactive || interactiveContentTags.has(tagName))) {
          return false;
        }
        return true;
      }
    );
  }, [
    allFormatTypes,
    allowedFormats,
    disableNoneEssentialFormatting,
    withoutInteractiveFormatting
  ]);
  const keyedSelected = (0, import_data.useSelect)(
    (select) => formatTypes.reduce((accumulator, type) => {
      if (!type.__experimentalGetPropsForEditableTreePreparation) {
        return accumulator;
      }
      return {
        ...accumulator,
        ...prefixSelectKeys(
          type.__experimentalGetPropsForEditableTreePreparation(
            select,
            {
              richTextIdentifier: identifier,
              blockClientId: clientId
            }
          ),
          type.name
        )
      };
    }, {}),
    [formatTypes, clientId, identifier]
  );
  const dispatch = (0, import_data.useDispatch)();
  const prepareHandlers = [];
  const valueHandlers = [];
  const changeHandlers = [];
  const dependencies = [];
  for (const key in keyedSelected) {
    dependencies.push(keyedSelected[key]);
  }
  formatTypes.forEach((type) => {
    if (type.__experimentalCreatePrepareEditableTree) {
      const handler = type.__experimentalCreatePrepareEditableTree(
        getPrefixedSelectKeys(keyedSelected, type.name),
        {
          richTextIdentifier: identifier,
          blockClientId: clientId
        }
      );
      if (type.__experimentalCreateOnChangeEditableValue) {
        valueHandlers.push(handler);
      } else {
        prepareHandlers.push(handler);
      }
    }
    if (type.__experimentalCreateOnChangeEditableValue) {
      let dispatchers = {};
      if (type.__experimentalGetPropsForEditableTreeChangeHandler) {
        dispatchers = type.__experimentalGetPropsForEditableTreeChangeHandler(
          dispatch,
          {
            richTextIdentifier: identifier,
            blockClientId: clientId
          }
        );
      }
      const selected = getPrefixedSelectKeys(keyedSelected, type.name);
      changeHandlers.push(
        type.__experimentalCreateOnChangeEditableValue(
          {
            ...typeof selected === "object" ? selected : {},
            ...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
