var _excluded = ["slot"];
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { h, Fragment } from 'preact';
import { getDocument, getAttributeObject, selfClosingTags, getPropKey } from "./shared";

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

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

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

function convertToVDom(node) {
  var _this = this;
  if (node.nodeType === 3) {
    return node.textContent || '';
  }
  if (node.nodeType !== 1) {
    return null;
  }
  var nodeName = String(node.nodeName).toLowerCase();
  var childNodes = Array.from(node.childNodes);
  var children = function children() {
    return childNodes.map(function (child) {
      return convertToVDom.call(_this, child);
    });
  };
  var _getAttributeObject = getAttributeObject(node.attributes),
    slot = _getAttributeObject.slot,
    props = _objectWithoutProperties(_getAttributeObject, _excluded);
  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) {
  var isString = function isString(item) {
    return typeof item === 'string';
  };
  if (children.every(isString)) {
    return children.join(' ');
  }
  return h(Fragment, {}, children);
}

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

export { parseHtml };