"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.parseHtml = parseHtml;
var _preact = require("preact");
var _shared = require("./shared");
/* -----------------------------------
 *
 * parseHtml
 *
 * -------------------------------- */

function parseHtml() {
  const dom = (0, _shared.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
  } = (0, _shared.getAttributeObject)(node.attributes);
  if (nodeName === 'script') {
    return null;
  }
  if (nodeName === 'body') {
    return (0, _preact.h)(_preact.Fragment, {}, children());
  }
  if (_shared.selfClosingTags.includes(nodeName)) {
    return (0, _preact.h)(nodeName, props);
  }
  if (slot) {
    this.__slots[(0, _shared.getPropKey)(slot)] = getSlotChildren(children());
    return null;
  }
  return (0, _preact.h)(nodeName, props, children());
}

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

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

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