"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-library/src/template-part/edit/utils/transformers.js
var transformers_exports = {};
__export(transformers_exports, {
  transformWidgetToBlock: () => transformWidgetToBlock
});
module.exports = __toCommonJS(transformers_exports);
var import_blocks = require("@wordpress/blocks");
function transformWidgetToBlock(widget) {
  if (widget.id_base !== "block") {
    let attributes;
    if (widget._embedded.about[0].is_multi) {
      attributes = {
        idBase: widget.id_base,
        instance: widget.instance
      };
    } else {
      attributes = {
        id: widget.id
      };
    }
    return switchLegacyWidgetType(
      (0, import_blocks.createBlock)("core/legacy-widget", attributes)
    );
  }
  const parsedBlocks = (0, import_blocks.parse)(widget.instance.raw.content, {
    __unstableSkipAutop: true
  });
  if (!parsedBlocks.length) {
    return void 0;
  }
  const block = parsedBlocks[0];
  if (block.name === "core/widget-group") {
    return (0, import_blocks.createBlock)(
      (0, import_blocks.getGroupingBlockName)(),
      void 0,
      transformInnerBlocks(block.innerBlocks)
    );
  }
  if (block.innerBlocks.length > 0) {
    return (0, import_blocks.cloneBlock)(
      block,
      void 0,
      transformInnerBlocks(block.innerBlocks)
    );
  }
  return block;
}
function switchLegacyWidgetType(block) {
  const transforms = (0, import_blocks.getPossibleBlockTransformations)([block]).filter(
    (item) => {
      if (!item.transforms) {
        return true;
      }
      const hasWildCardFrom = item.transforms?.from?.find(
        (from) => from.blocks && from.blocks.includes("*")
      );
      const hasWildCardTo = item.transforms?.to?.find(
        (to) => to.blocks && to.blocks.includes("*")
      );
      return !hasWildCardFrom && !hasWildCardTo;
    }
  );
  if (!transforms.length) {
    return void 0;
  }
  return (0, import_blocks.switchToBlockType)(block, transforms[0].name);
}
function transformInnerBlocks(innerBlocks = []) {
  return innerBlocks.flatMap((block) => {
    if (block.name === "core/legacy-widget") {
      return switchLegacyWidgetType(block);
    }
    return (0, import_blocks.createBlock)(
      block.name,
      block.attributes,
      transformInnerBlocks(block.innerBlocks)
    );
  }).filter((block) => !!block);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  transformWidgetToBlock
});
//# sourceMappingURL=transformers.js.map
