import React, { Fragment } from 'react';
import { connect } from '../redux/store';
import { compose, withHandlers } from 'recompose';
import { CoreForm, I18n } from '@kineticdata/react';
import { Link } from '@reach/router';
import { ErrorNotFound, ErrorUnauthorized, ErrorUnexpected } from '@kineticdata/bundle-common';
import { PageTitle } from './shared/PageTitle';
import { APPOINTMENT_FORM_SLUG } from '../constants';
export var 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.createElement(Fragment, null, /*#__PURE__*/React.createElement(PageTitle, {
    parts: ['Appointment']
  }), /*#__PURE__*/React.createElement("div", {
    className: "page-container page-container--tech-bar container"
  }, /*#__PURE__*/React.createElement("div", {
    className: "page-panel"
  }, techBar ? /*#__PURE__*/React.createElement(Fragment, null, /*#__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")), ' ', "/", ' ', past && /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(Link, {
    to: "".concat(relativeHomePath, "past")
  }, /*#__PURE__*/React.createElement(I18n, null, "past appointments")), ' ', "/", ' ')), /*#__PURE__*/React.createElement("h1", null, /*#__PURE__*/React.createElement(I18n, null, techBar.values['Name']), ' ', /*#__PURE__*/React.createElement("small", null, /*#__PURE__*/React.createElement(I18n, null, "Appointment"))))), /*#__PURE__*/React.createElement(I18n, {
    context: "kapps.".concat(kappSlug, ".forms.").concat(APPOINTMENT_FORM_SLUG)
  }, /*#__PURE__*/React.createElement("div", {
    className: "embedded-core-form--wrapper"
  }, id ? /*#__PURE__*/React.createElement(CoreForm, {
    submission: id,
    review: true,
    loaded: handleLoaded,
    completed: handleCompleted
  }) : /*#__PURE__*/React.createElement(CoreForm, {
    kapp: kappSlug,
    form: APPOINTMENT_FORM_SLUG,
    loaded: handleLoaded,
    created: handleCreated,
    completed: handleCompleted,
    values: {
      'Scheduler Id': techBar.values['Id']
    },
    notFoundComponent: ErrorNotFound,
    unauthorizedComponent: ErrorUnauthorized,
    unexpectedErrorComponent: ErrorUnexpected
  })))) : /*#__PURE__*/React.createElement(ErrorNotFound, null))));
};
export var handleCompleted = function handleCompleted(props) {
  return function (response) {
    if (!response.submission.currentPage) {
      props.navigate("/kapps/".concat(props.kappSlug));
    }
  };
};
export var handleCreated = function handleCreated(props) {
  return function (response) {
    props.navigate(response.submission.coreState === 'Submitted' ? "/kapps/".concat(props.kappSlug) : response.submission.id);
  };
};
export var 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 = compose(connect(mapStateToProps), withHandlers({
  handleCompleted: handleCompleted,
  handleCreated: handleCreated
}));
export var AppointmentForm = enhance(AppointmentFormComponent);