import { Node } from '../../node.js';

const mapItem = (item) => {
  const nodes = [
    {
        ...item,
        type: 'containerDirective',
        name: item.name ?? 'dsfr-doc-page-item-card',
    }
  ];

  if (item.items) {
    for (const childItem of item.items) {
      nodes.push(...mapItem(childItem));
    }
  }

  return nodes
};

class PageItemListLeafDirective extends Node {
  structure (data) {
    const children = [];
    for (const item of data.items) {
      children.push(...mapItem(item));
    }

    return super.structure({
      type: 'htmlContainer',
      tagName: 'div',
      classes: [
        'fr-grid-row',
        'fr-grid-row--gutters',
        'fr-mb-12v',
      ],
      children: children,
    });
  }
}

PageItemListLeafDirective.NAME = 'dsfr-doc-page-item-list';

export { PageItemListLeafDirective };
