import { assert } from '@ember/debug';

/*
This util was extracted from https://github.com/ampatspell/ember-cli-remark-static/blob/v3.0.5/addon/util/to-dom.js
*/

const attributes = ['src', 'alt', 'href', 'target', 'title'];
const createDocument = () => {
  return document;
};
const toDOM = (tree, options) => {
  let document = createDocument();
  let components = [];
  const toElements = (parent, nodes = []) => {
    nodes?.forEach(node => {
      let el = toElement(node);
      if (el) {
        parent.appendChild(el);
      }
    });
    return parent;
  };
  const createElement = (name, node, classesToAdd) => {
    let element = document.createElement(name);
    let properties = node.properties;
    let finalClassNames = [];
    if (properties) {
      if (properties['className']) {
        finalClassNames.push(...properties['className']);
      }
      for (let key in properties) {
        if (attributes.includes(key)) {
          let value = properties[key];
          element.setAttribute(key, value);
        } else {
          // temporary
          if (key !== 'className') {
            console.warn('Unmapped node property', key);
          }
        }
      }
    }
    if (classesToAdd) {
      if (Array.isArray(classesToAdd)) {
        finalClassNames.push(...classesToAdd);
      } else {
        finalClassNames.push(classesToAdd);
      }
    }
    element.classList.add(...finalClassNames);
    return element;
  };
  const toElement = node => {
    if (node) {
      let {
        type
      } = node;
      if (type === 'root') {
        let element = createElement('div', node, options?.rootClasses || ['root']);
        return toElements(element, node.children);
      } else if (type === 'element') {
        let element = createElement(node.tagName, node);
        return toElements(element, node.children);
      } else if (type === 'text') {
        return document.createTextNode(node.value);
      } else if (type === 'component') {
        let {
          inline
        } = node.properties;
        let element = createElement(inline ? 'span' : 'div', node, ['component']);
        let {
          _children,
          ...properties
        } = node.properties;
        let content = toElements(document.createElement('span'), node.children);
        components.push({
          element,
          content,
          ...properties
        });
        return element;
      } else if (type === 'raw') {
        return document.createTextNode(node.value);
      }
      assert(`Unsupported node '${type}'`, false);
    }
    return;
  };
  let element = toElement(tree);
  return {
    element,
    components
  };
};

export { toDOM };
//# sourceMappingURL=to-dom.js.map
