import { createBlock, parse } from "@wordpress/blocks";
import { applyFilters } from "@wordpress/hooks";
function menuItemsToBlocks(menuItems) {
  if (!menuItems) {
    return null;
  }
  const menuTree = createDataTree(menuItems);
  const blocks = mapMenuItemsToBlocks(menuTree);
  return applyFilters(
    "blocks.navigation.__unstableMenuItemsToBlocks",
    blocks,
    menuItems
  );
}
function mapMenuItemsToBlocks(menuItems, level = 0) {
  let mapping = {};
  const sortedItems = [...menuItems].sort(
    (a, b) => a.menu_order - b.menu_order
  );
  const innerBlocks = sortedItems.map((menuItem) => {
    if (menuItem.type === "block") {
      const [block2] = parse(menuItem.content.raw);
      if (!block2) {
        return createBlock("core/freeform", {
          content: menuItem.content
        });
      }
      return block2;
    }
    const blockType = menuItem.children?.length ? "core/navigation-submenu" : "core/navigation-link";
    const attributes = menuItemToBlockAttributes(
      menuItem,
      blockType,
      level
    );
    const {
      innerBlocks: nestedBlocks = [],
      // alias to avoid shadowing
      mapping: nestedMapping = {}
      // alias to avoid shadowing
    } = menuItem.children?.length ? mapMenuItemsToBlocks(menuItem.children, level + 1) : {};
    mapping = {
      ...mapping,
      ...nestedMapping
    };
    const block = createBlock(blockType, attributes, nestedBlocks);
    mapping[menuItem.id] = block.clientId;
    return block;
  });
  return {
    innerBlocks,
    mapping
  };
}
function menuItemToBlockAttributes({
  title: menuItemTitleField,
  xfn,
  classes,
  // eslint-disable-next-line camelcase
  attr_title,
  object,
  // eslint-disable-next-line camelcase
  object_id,
  description,
  url,
  type: menuItemTypeField,
  target
}, blockType, level) {
  if (object && object === "post_tag") {
    object = "tag";
  }
  return {
    label: menuItemTitleField?.rendered || "",
    ...object?.length && {
      type: object
    },
    kind: menuItemTypeField?.replace("_", "-") || "custom",
    url: url || "",
    ...xfn?.length && xfn.join(" ").trim() && {
      rel: xfn.join(" ").trim()
    },
    ...classes?.length && classes.join(" ").trim() && {
      className: classes.join(" ").trim()
    },
    /* eslint-disable camelcase */
    ...attr_title?.length && {
      title: attr_title
    },
    ...object_id && "custom" !== object && {
      id: object_id
    },
    /* eslint-enable camelcase */
    ...description?.length && {
      description
    },
    ...target === "_blank" && {
      opensInNewTab: true
    },
    ...blockType === "core/navigation-submenu" && {
      isTopLevelItem: level === 0
    },
    ...blockType === "core/navigation-link" && {
      isTopLevelLink: level === 0
    }
  };
}
function createDataTree(dataset, id = "id", relation = "parent") {
  const hashTable = /* @__PURE__ */ Object.create(null);
  const dataTree = [];
  for (const data of dataset) {
    hashTable[data[id]] = {
      ...data,
      children: []
    };
    if (data[relation]) {
      hashTable[data[relation]] = hashTable[data[relation]] || {};
      hashTable[data[relation]].children = hashTable[data[relation]].children || [];
      hashTable[data[relation]].children.push(
        hashTable[data[id]]
      );
    } else {
      dataTree.push(hashTable[data[id]]);
    }
  }
  return dataTree;
}
export {
  menuItemsToBlocks as default
};
//# sourceMappingURL=menu-items-to-blocks.js.map
