"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mapStateToProps = exports.handleCreated = exports.handleCompleted = exports.FormComponent = exports.Form = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/defineProperty"));
var _objectSpread3 = _interopRequireDefault(require("@babel/runtime/helpers/esm/objectSpread2"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _store = require("../redux/store");
var _recompose = require("recompose");
var _react2 = require("@kineticdata/react");
var _bundleCommon = require("@kineticdata/bundle-common");
var _PageTitle = require("./shared/PageTitle");
var _queryString = require("query-string");
var FormComponent = exports.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["default"].createElement(_react.Fragment, null, /*#__PURE__*/_react["default"].createElement("div", {
    className: "page-container page-container--tech-bar page-container-lg"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "page-panel"
  }, /*#__PURE__*/_react["default"].createElement(_PageTitle.PageTitle, {
    parts: [form ? form.name : ''],
    breadcrumbs: [{
      label: 'Home',
      to: '/'
    }, {
      label: 'Tech Bar',
      to: relativeHomePath
    }],
    title: form && form.name
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "form-description"
  }, form && form.description && /*#__PURE__*/_react["default"].createElement("p", null, /*#__PURE__*/_react["default"].createElement(_react2.I18n, {
    context: "kapps.".concat(kappSlug, ".forms.").concat(formSlug)
  }, form.description))), /*#__PURE__*/_react["default"].createElement(_react2.I18n, {
    context: "kapps.".concat(kappSlug, ".forms.").concat(formSlug)
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "embedded-core-form--wrapper"
  }, id ? /*#__PURE__*/_react["default"].createElement(_react2.CoreForm, {
    submission: id,
    loaded: handleLoaded,
    completed: handleCompleted
  }) : /*#__PURE__*/_react["default"].createElement(_react2.CoreForm, {
    kapp: kappSlug,
    form: formSlug,
    loaded: handleLoaded,
    created: handleCreated,
    completed: handleCompleted,
    values: values,
    notFoundComponent: _bundleCommon.ErrorNotFound,
    unauthorizedComponent: _bundleCommon.ErrorUnauthorized,
    unexpectedErrorComponent: _bundleCommon.ErrorUnexpected
  }))))));
};
var valuesFromQueryParams = function valuesFromQueryParams(queryParams) {
  var params = (0, _queryString.parse)(queryParams);
  return Object.entries(params).reduce(function (values, _ref2) {
    var _ref3 = (0, _slicedToArray2["default"])(_ref2, 2),
      key = _ref3[0],
      value = _ref3[1];
    if (key.startsWith('values[')) {
      var vk = key.match(/values\[(.*?)\]/)[1];
      return (0, _objectSpread3["default"])((0, _objectSpread3["default"])({}, values), {}, (0, _defineProperty2["default"])({}, vk, value));
    }
    return values;
  }, {});
};
var handleCompleted = exports.handleCompleted = function handleCompleted(props) {
  return function (response) {
    // Check if either currentPage is null (pre form consolidation) or
    // displayedPage.type is not 'confirmation' (post form-consolidation)
    // to determine that there is no confirmation page and we should redirect.
    if (!response.submission.currentPage || response.submission.displayedPage && response.submission.displayedPage.type !== 'confirmation') {
      props.navigate(props.relativeHomePath);
    }
  };
};
var handleCreated = exports.handleCreated = function handleCreated(props) {
  return function (response) {
    props.navigate(response.submission.coreState === 'Submitted' ? props.relativeHomePath : "submissions/".concat(response.submission.id));
  };
};
var mapStateToProps = exports.mapStateToProps = function mapStateToProps(state) {
  return {
    kappSlug: state.app.kappSlug,
    forms: state.techBarApp.forms,
    values: valuesFromQueryParams(state.router.location.search)
  };
};
var enhance = (0, _recompose.compose)((0, _store.connect)(mapStateToProps), (0, _recompose.withProps)(function (props) {
  return {
    form: props.forms.find(function (form) {
      return form.slug === props.formSlug;
    }),
    relativeHomePath: "../../".concat(props.id ? '../../' : '')
  };
}), (0, _recompose.withHandlers)({
  handleCompleted: handleCompleted,
  handleCreated: handleCreated
}));
var Form = exports.Form = enhance(FormComponent);