"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _preact = require("preact");

var _componentCode = _interopRequireDefault(require("./component-code"));

var _getPropVariants = _interopRequireDefault(require("../utils/get-prop-variants"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

const serializeProps = props => {
  if (!props) {
    return '';
  }

  return Object.keys(props).reduce((buffer, prop) => {
    const value = props[prop];
    return "".concat(buffer, " ").concat(prop).concat(value === true ? '' : "=\"".concat(value, "\""));
  }, '');
};

const componentString = (name, props, content) => "<".concat(name).concat(serializeProps(props)).concat(content ? ">".concat(content, "</").concat(name) : " />");

var _default = ({
  Component,
  children,
  name
}) => {
  const {
    options = {}
  } = Component.definition;
  const variants = (0, _getPropVariants.default)(options);
  const code = variants.reduce((buff, props) => buff + componentString(name, props, children) + '\n', '');
  return (0, _preact.h)(_componentCode.default, {
    name: name,
    code: code
  }, variants.map(props => (0, _preact.h)(Component, _extends({
    key: name,
    title: componentString(name, props, children)
  }, Object.assign({}, props, {
    children
  }), {
    className: "mr2 mb2"
  }))));
};

exports.default = _default;