"use strict";

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

var _preact = require("preact");

var _classnames = _interopRequireDefault(require("classnames"));

var _object = require("./object");

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

const alwaysPassThroughProps = ['disabled'];
/**
 * @param {{
 *   tagName?: string
 *   baseClasses?: string
 *   options?: any
 *   style?: any
 *   fn?: any
 *   passThroughProps?: any
 *   defaultProps?: any
 *   renderChildren?: any
 * }} args
 * @returns {import('preact').Component}
 */

var _default = args => {
  const {
    tagName = 'div',
    baseClasses = '',
    options = {},
    style,
    fn,
    passThroughProps = [],
    defaultProps,
    renderChildren
  } = args;
  const defaults = Object.assign({}, defaultProps);
  const propsToRemove = ['className', 'tagName'];
  const allOptionValues = Object.keys(options).reduce((result, key) => {
    const value = options[key];

    if (typeof value !== 'object') {
      defaults[key] = value;
      propsToRemove.push(key);
      result.push(value);
    } else {
      result.push(...value);
    }

    return result;
  }, []);
  propsToRemove.push(...allOptionValues.filter(key => !passThroughProps.includes(key) && !alwaysPassThroughProps.includes(key)));

  const componentFn = incomingProps => {
    const props = Object.assign({}, defaults, incomingProps);
    const finalTagName = props.tagName || tagName;
    const passThroughProps = (0, _object.omit)(props, propsToRemove);
    const className = (0, _classnames.default)(baseClasses, fn && fn(props), props.className);
    const combinedStyle = Object.assign({}, style, props.style);
    const renderedChildren = renderChildren ? renderChildren(props) : props.children;
    return (0, _preact.h)(finalTagName, Object.assign({
      className,
      style: combinedStyle
    }, passThroughProps), renderedChildren);
  };

  componentFn.definition = args;
  return componentFn;
};

exports.default = _default;