import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectSpread from "@babel/runtime/helpers/objectSpread";

/**
 * Web FormSet Component
 *
 */
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { isTrue, extend, registerElement, validateDOMAttributes, processChildren, dispatchCustomElementEvent } from '../../shared/component-helper';
import Context from '../../shared/Context';
const renderProps = {
  render_content: null
};
export const propTypes = {
  id: PropTypes.string,
  element: PropTypes.string,
  no_form: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
  prevent_submit: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
  size: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
  direction: PropTypes.oneOf(['vertical', 'horizontal']),
  vertical: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
  disabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
  class: PropTypes.string,

  /** React props */
  className: PropTypes.string,
  children: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.node]),
  // Web Component props
  render_content: PropTypes.func
};
export const defaultProps = _objectSpread({
  id: null,
  element: 'form',
  no_form: false,
  prevent_submit: false,
  size: null,
  direction: 'horizontal',
  vertical: null,
  disabled: null,
  class: null,

  /** React props */
  className: null,
  children: null
}, renderProps);
export default class FormSet extends PureComponent {
  static enableWebComponent() {
    registerElement(FormSet.tagName, FormSet, defaultProps);
  }

  static getContent(props) {
    if (typeof props.render_content === 'function') props.render_content(props);
    return processChildren(props);
  }

  constructor(props) {
    super(props);

    _defineProperty(this, "onSubmitHandler", event => {
      const {
        prevent_submit
      } = this.props;

      if (isTrue(prevent_submit)) {
        event.preventDefault();
      }

      dispatchCustomElementEvent(this, 'on_submit', {
        event
      });
    });

    this._id = props.id || "dnb-form-set-".concat(Math.round(Math.random() * 999)); // cause we need an id anyway
  }

  render() {
    const _this$props = this.props,
          {
      element,
      size,
      direction,
      vertical,
      no_form,
      prevent_submit,
      // eslint-disable-line
      disabled,
      // eslint-disable-line
      id,
      // eslint-disable-line
      className,
      class: _className
    } = _this$props,
          attributes = _objectWithoutProperties(_this$props, ["element", "size", "direction", "vertical", "no_form", "prevent_submit", "disabled", "id", "className", "class"]);

    const content = FormSet.getContent(this.props);

    const params = _objectSpread({
      className: classnames('dnb-form-set', // (isTrue(vertical) || direction) && `dnb-form-set--${isTrue(vertical) ? 'vertical' : direction}`,
      // size && `dnb-form-set__size--${isTrue(size) ? 'default' : size}`,
      className, _className)
    }, attributes);

    if (!isTrue(no_form)) {
      params.onSubmit = this.onSubmitHandler;
    } // also used for code markup simulation


    validateDOMAttributes(this.props, params);
    const context = extend(this.context, {
      formRow: {
        size,
        direction,
        vertical,
        disabled
      }
    });
    return React.createElement(Context.Provider, {
      value: context
    }, React.createElement(Element, _extends({
      is: isTrue(no_form) ? 'div' : element
    }, params), content));
  }

}

_defineProperty(FormSet, "tagName", 'dnb-form-set');

_defineProperty(FormSet, "defaultProps", defaultProps);

_defineProperty(FormSet, "contextType", Context);

FormSet.propTypes = process.env.NODE_ENV !== "production" ? propTypes : {};

const Element = (_ref) => {
  let {
    is: Element,
    children
  } = _ref,
      rest = _objectWithoutProperties(_ref, ["is", "children"]);

  return React.createElement(Element, rest, children);
};

Element.propTypes = process.env.NODE_ENV !== "production" ? {
  is: PropTypes.string.isRequired,
  children: PropTypes.node.isRequired
} : {};