"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mapStateToProps = exports.handleCreated = exports.handleCompleted = exports.AppointmentFormComponent = exports.AppointmentForm = void 0;
var _react = _interopRequireDefault(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 _constants = require("../constants");
var AppointmentFormComponent = exports.AppointmentFormComponent = function AppointmentFormComponent(_ref) {
  var techBarId = _ref.techBarId,
    id = _ref.id,
    techBar = _ref.techBar,
    past = _ref.past,
    relativeHomePath = _ref.relativeHomePath,
    handleCreated = _ref.handleCreated,
    handleCompleted = _ref.handleCompleted,
    handleLoaded = _ref.handleLoaded,
    handleDelete = _ref.handleDelete,
    kappSlug = _ref.kappSlug;
  return /*#__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: ['Appointment'],
    breadcrumbs: [{
      label: 'Home',
      to: '/'
    }, {
      label: 'Tech Bar',
      to: relativeHomePath
    }, past && {
      label: 'Past Appointments',
      to: "".concat(relativeHomePath, "past")
    }],
    title: techBar && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, techBar.values['Name']), ' ', /*#__PURE__*/_react["default"].createElement("small", null, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "Appointment")))
  }), techBar ? /*#__PURE__*/_react["default"].createElement(_react2.I18n, {
    context: "kapps.".concat(kappSlug, ".forms.").concat(_constants.APPOINTMENT_FORM_SLUG)
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "embedded-core-form--wrapper"
  }, id ? /*#__PURE__*/_react["default"].createElement(_react2.CoreForm, {
    submission: id,
    review: true,
    loaded: handleLoaded,
    completed: handleCompleted
  }) : /*#__PURE__*/_react["default"].createElement(_react2.CoreForm, {
    kapp: kappSlug,
    form: _constants.APPOINTMENT_FORM_SLUG,
    loaded: handleLoaded,
    created: handleCreated,
    completed: handleCompleted,
    values: {
      'Scheduler Id': techBar.values['Id']
    },
    notFoundComponent: _bundleCommon.ErrorNotFound,
    unauthorizedComponent: _bundleCommon.ErrorUnauthorized,
    unexpectedErrorComponent: _bundleCommon.ErrorUnexpected
  }))) : /*#__PURE__*/_react["default"].createElement(_bundleCommon.ErrorNotFound, null)));
};
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("/kapps/".concat(props.kappSlug));
    }
  };
};
var handleCreated = exports.handleCreated = function handleCreated(props) {
  return function (response) {
    props.navigate(response.submission.coreState === 'Submitted' ? "/kapps/".concat(props.kappSlug) : response.submission.id);
  };
};
var mapStateToProps = exports.mapStateToProps = function mapStateToProps(state, props) {
  var past = !!props.path.match(/^\/past/);
  var relativeHomePath = "../".concat(props.techBarId ? '../' : '').concat(props.id ? '../' : '').concat(past ? '../' : '');
  return {
    kappSlug: state.app.kappSlug,
    techBar: state.techBarApp.schedulers.find(function (scheduler) {
      return scheduler.values['Id'] === props.techBarId;
    }),
    past: past,
    relativeHomePath: relativeHomePath
  };
};
var enhance = (0, _recompose.compose)((0, _store.connect)(mapStateToProps), (0, _recompose.withHandlers)({
  handleCompleted: handleCompleted,
  handleCreated: handleCreated
}));
var AppointmentForm = exports.AppointmentForm = enhance(AppointmentFormComponent);