import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import React, { Fragment } from 'react';
import { connect } from '../redux/store';
import { compose, withHandlers, withProps } from 'recompose';
import { CoreForm } from '@kineticdata/react';
import { ErrorNotFound, ErrorUnauthorized, ErrorUnexpected } from '@kineticdata/bundle-common';
import { PageTitle } from './shared/PageTitle';
import { Link } from '@reach/router';
import { parse } from 'query-string';
import { I18n } from '@kineticdata/react';
export var FormComponent = function FormComponent(_ref) {
  var formSlug = _ref.formSlug,
      id = _ref.id,
      form = _ref.form,
      relativeHomePath = _ref.relativeHomePath,
      handleCreated = _ref.handleCreated,
      handleCompleted = _ref.handleCompleted,
      handleLoaded = _ref.handleLoaded,
      handleDelete = _ref.handleDelete,
      values = _ref.values,
      kappSlug = _ref.kappSlug;
  return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(PageTitle, {
    parts: [form ? form.name : '']
  }), /*#__PURE__*/React.createElement("div", {
    className: "page-container page-container--tech-bar container"
  }, /*#__PURE__*/React.createElement("div", {
    className: "page-panel"
  }, /*#__PURE__*/React.createElement("div", {
    className: "page-title"
  }, /*#__PURE__*/React.createElement("div", {
    role: "navigation",
    "aria-label": "breadcrumbs",
    className: "page-title__breadcrumbs"
  }, /*#__PURE__*/React.createElement("span", {
    className: "breadcrumb-item"
  }, /*#__PURE__*/React.createElement(Link, {
    to: relativeHomePath
  }, /*#__PURE__*/React.createElement(I18n, null, "tech bar")), ' ', "/", ' '), form && /*#__PURE__*/React.createElement("h1", null, /*#__PURE__*/React.createElement(I18n, {
    context: "kapps.".concat(kappSlug, ".forms.").concat(formSlug)
  }, form.name)))), /*#__PURE__*/React.createElement("div", {
    className: "form-description"
  }, form && form.description && /*#__PURE__*/React.createElement("p", null, /*#__PURE__*/React.createElement(I18n, {
    context: "kapps.".concat(kappSlug, ".forms.").concat(formSlug)
  }, form.description))), /*#__PURE__*/React.createElement(I18n, {
    context: "kapps.".concat(kappSlug, ".forms.").concat(formSlug)
  }, /*#__PURE__*/React.createElement("div", {
    className: "embedded-core-form--wrapper"
  }, id ? /*#__PURE__*/React.createElement(CoreForm, {
    submission: id,
    loaded: handleLoaded,
    completed: handleCompleted
  }) : /*#__PURE__*/React.createElement(CoreForm, {
    kapp: kappSlug,
    form: formSlug,
    loaded: handleLoaded,
    created: handleCreated,
    completed: handleCompleted,
    values: values,
    notFoundComponent: ErrorNotFound,
    unauthorizedComponent: ErrorUnauthorized,
    unexpectedErrorComponent: ErrorUnexpected
  }))))));
};

var valuesFromQueryParams = function valuesFromQueryParams(queryParams) {
  var params = parse(queryParams);
  return Object.entries(params).reduce(function (values, _ref2) {
    var _ref3 = _slicedToArray(_ref2, 2),
        key = _ref3[0],
        value = _ref3[1];

    if (key.startsWith('values[')) {
      var vk = key.match(/values\[(.*?)\]/)[1];
      return _objectSpread(_objectSpread({}, values), {}, _defineProperty({}, vk, value));
    }

    return values;
  }, {});
};

export var handleCompleted = function handleCompleted(props) {
  return function (response) {
    if (!response.submission.currentPage) {
      props.navigate(props.relativeHomePath);
    }
  };
};
export var handleCreated = function handleCreated(props) {
  return function (response) {
    props.navigate(response.submission.coreState === 'Submitted' ? props.relativeHomePath : "submissions/".concat(response.submission.id));
  };
};
export var mapStateToProps = function mapStateToProps(state) {
  return {
    kappSlug: state.app.kappSlug,
    forms: state.techBarApp.forms,
    values: valuesFromQueryParams(state.router.location.search)
  };
};
var enhance = compose(connect(mapStateToProps), withProps(function (props) {
  return {
    form: props.forms.find(function (form) {
      return form.slug === props.formSlug;
    }),
    relativeHomePath: "../../".concat(props.id ? '../../' : '')
  };
}), withHandlers({
  handleCompleted: handleCompleted,
  handleCreated: handleCreated
}));
export var Form = enhance(FormComponent);