// packages/block-editor/src/hooks/metadata.js
import { addFilter } from "@wordpress/hooks";
import { hasBlockSupport } from "@wordpress/blocks";
var META_ATTRIBUTE_NAME = "metadata";
function addMetaAttribute(blockTypeSettings) {
  if (blockTypeSettings?.attributes?.[META_ATTRIBUTE_NAME]?.type) {
    return blockTypeSettings;
  }
  blockTypeSettings.attributes = {
    ...blockTypeSettings.attributes,
    [META_ATTRIBUTE_NAME]: {
      type: "object"
    }
  };
  return blockTypeSettings;
}
function addTransforms(result, source, index, results) {
  if (results.length === 1 && result.innerBlocks.length === source.length) {
    return result;
  }
  if (results.length === 1 && source.length > 1 || results.length > 1 && source.length === 1) {
    return result;
  }
  if (results.length > 1 && source.length > 1 && results.length !== source.length) {
    return result;
  }
  const sourceMetadata = source[index]?.attributes?.metadata;
  if (!sourceMetadata) {
    return result;
  }
  const preservedMetadata = {};
  if (sourceMetadata.noteId && !result.attributes?.metadata?.noteId) {
    preservedMetadata.noteId = sourceMetadata.noteId;
  }
  if (sourceMetadata.name && !result.attributes?.metadata?.name && hasBlockSupport(result.name, "renaming", true)) {
    preservedMetadata.name = sourceMetadata.name;
  }
  if (sourceMetadata.blockVisibility !== void 0 && !result.attributes?.metadata?.blockVisibility && hasBlockSupport(result.name, "visibility", true)) {
    preservedMetadata.blockVisibility = sourceMetadata.blockVisibility;
  }
  if (Object.keys(preservedMetadata).length > 0) {
    return {
      ...result,
      attributes: {
        ...result.attributes,
        metadata: {
          ...result.attributes.metadata,
          ...preservedMetadata
        }
      }
    };
  }
  return result;
}
addFilter(
  "blocks.registerBlockType",
  "core/metadata/addMetaAttribute",
  addMetaAttribute
);
addFilter(
  "blocks.switchToBlockType.transformedBlock",
  "core/metadata/addTransforms",
  addTransforms
);
export {
  addMetaAttribute,
  addTransforms
};
//# sourceMappingURL=metadata.js.map
