"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.submitForm = exports.FormWrapper = void 0;
var _react = _interopRequireDefault(require("react"));
var _eui = require("@elastic/eui");
var _addonActions = require("@storybook/addon-actions");
var _use_form = require("../../hooks/use_form");
var _form = require("../form");
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

/**
 * Validate the form and return its data.
 *
 * @param form The FormHook instance
 */
const submitForm = async form => {
  const {
    isValid,
    data
  } = await form.submit();
  (0, _addonActions.action)('Send form')({
    isValid,
    data: JSON.stringify(data)
  });
};
exports.submitForm = submitForm;
const FormWrapper = ({
  formConfig,
  children
}) => {
  const {
    form
  } = (0, _use_form.useForm)(formConfig);
  return /*#__PURE__*/_react.default.createElement(_form.Form, {
    form: form
  }, /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, children, /*#__PURE__*/_react.default.createElement(_eui.EuiSpacer, null), /*#__PURE__*/_react.default.createElement(_eui.EuiButton, {
    onClick: () => submitForm(form)
  }, "Send")));
};
exports.FormWrapper = FormWrapper;