import { h, Fragment } from 'preact';
import { getDocument, getAttributeObject, selfClosingTags, getPropKey } from "./shared";

/* -----------------------------------
 *
 * parseHtml
 *
 * -------------------------------- */

function parseHtml() {
  const dom = getDocument(this.innerHTML);
  if (!dom) {
    return void 0;
  }
  const result = convertToVDom.call(this, dom);
  return result;
}

/* -----------------------------------
 *
 * convertToVDom
 *
 * -------------------------------- */

function convertToVDom(node) {
  if (node.nodeType === 3) {
    return node.textContent || '';
  }
  if (node.nodeType !== 1) {
    return null;
  }
  const nodeName = String(node.nodeName).toLowerCase();
  const childNodes = Array.from(node.childNodes);
  const children = () => childNodes.map(child => convertToVDom.call(this, child));
  const {
    slot,
    ...props
  } = getAttributeObject(node.attributes);
  if (nodeName === 'script') {
    return null;
  }
  if (nodeName === 'body') {
    return h(Fragment, {}, children());
  }
  if (selfClosingTags.includes(nodeName)) {
    return h(nodeName, props);
  }
  if (slot) {
    this.__slots[getPropKey(slot)] = getSlotChildren(children());
    return null;
  }
  return h(nodeName, props, children());
}

/* -----------------------------------
 *
 * getSlotChildren
 *
 * -------------------------------- */

function getSlotChildren(children) {
  const isString = item => typeof item === 'string';
  if (children.every(isString)) {
    return children.join(' ');
  }
  return h(Fragment, {}, children);
}

/* -----------------------------------
 *
 * Export
 *
 * -------------------------------- */

export { parseHtml };