import React, { Fragment } from 'react';
import { connect } from '../../../redux/store';
import { compose, withHandlers } from 'recompose';
import { Utils, ErrorNotFound, ErrorUnauthorized, selectHasRoleSchedulerAdmin } from '@kineticdata/bundle-common';
import { PageTitle } from '../../shared/PageTitle';
import { Link } from '@reach/router';
import { CoreForm } from '@kineticdata/react';
import { actions } from '../../../redux/modules/techBarApp';
import { TECH_BAR_SETTINGS_FORM_SLUG } from '../../../constants';
import { I18n } from '@kineticdata/react';
export var TechBarSettingsFormComponent = function TechBarSettingsFormComponent(_ref) {
  var techBar = _ref.techBar,
      handleSaved = _ref.handleSaved,
      hasManagerAccess = _ref.hasManagerAccess;
  return techBar ? hasManagerAccess ? /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(PageTitle, {
    parts: ['Settings', techBar.values['Name']],
    settings: true
  }), /*#__PURE__*/React.createElement("div", {
    className: "page-container"
  }, /*#__PURE__*/React.createElement("div", {
    className: "page-panel page-panel--white"
  }, /*#__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: "../../../../"
  }, /*#__PURE__*/React.createElement(I18n, null, "tech bar"))), ' ', /*#__PURE__*/React.createElement("span", {
    "aria-hidden": "true"
  }, "/ "), /*#__PURE__*/React.createElement("span", {
    className: "breadcrumb-item"
  }, /*#__PURE__*/React.createElement(Link, {
    to: "../../../"
  }, /*#__PURE__*/React.createElement(I18n, null, "settings"))), ' ', /*#__PURE__*/React.createElement("span", {
    "aria-hidden": "true"
  }, "/ "), /*#__PURE__*/React.createElement("span", {
    className: "breadcrumb-item"
  }, /*#__PURE__*/React.createElement(Link, {
    to: "../../"
  }, /*#__PURE__*/React.createElement(I18n, null, "tech bars"))), ' ', /*#__PURE__*/React.createElement("span", {
    "aria-hidden": "true"
  }, "/ "), /*#__PURE__*/React.createElement("span", {
    className: "breadcrumb-item"
  }, /*#__PURE__*/React.createElement(Link, {
    to: "../"
  }, /*#__PURE__*/React.createElement(I18n, null, techBar.values['Name']))), ' ', /*#__PURE__*/React.createElement("span", {
    "aria-hidden": "true"
  }, "/ "), /*#__PURE__*/React.createElement("h1", null, /*#__PURE__*/React.createElement(I18n, null, "Edit Settings"))), /*#__PURE__*/React.createElement(Link, {
    to: "../",
    className: "btn btn-secondary"
  }, /*#__PURE__*/React.createElement(I18n, null, "Cancel Edit"))), /*#__PURE__*/React.createElement("div", {
    className: "content-wrapper form-unstyled"
  }, /*#__PURE__*/React.createElement(I18n, {
    context: "datastore.forms.".concat(TECH_BAR_SETTINGS_FORM_SLUG)
  }, techBar.settings.submissionId ? /*#__PURE__*/React.createElement(CoreForm, {
    datastore: true,
    submission: techBar.settings.submissionId,
    created: handleSaved,
    updated: handleSaved
  }) : /*#__PURE__*/React.createElement(CoreForm, {
    datastore: true,
    form: TECH_BAR_SETTINGS_FORM_SLUG,
    created: handleSaved,
    updated: handleSaved,
    values: {
      'Scheduler Id': techBar.values['Id']
    }
  })))))) : /*#__PURE__*/React.createElement(ErrorUnauthorized, null) : /*#__PURE__*/React.createElement(ErrorNotFound, null);
};
export var mapStateToProps = function mapStateToProps(state, props) {
  var isSchedulerAdmin = selectHasRoleSchedulerAdmin(state.app.profile);
  var techBar = state.techBarApp.schedulers.find(function (scheduler) {
    return scheduler.id === props.techBarId && (isSchedulerAdmin || Utils.isMemberOf(state.app.profile, "Role::Scheduler::".concat(scheduler.values['Name'])) || Utils.isMemberOf(state.app.profile, "Scheduler::".concat(scheduler.values['Name'])));
  });
  return {
    kapp: state.app.kapp,
    techBar: techBar,
    hasManagerAccess: isSchedulerAdmin || techBar && Utils.isMemberOf(state.app.profile, "Role::Scheduler::".concat(techBar.values['Name']))
  };
};
export var mapDispatchToProps = {
  updateTechBarSettingsSuccess: actions.updateTechBarSettingsSuccess
};
export var TechBarSettingsForm = compose(connect(mapStateToProps, mapDispatchToProps), withHandlers({
  handleSaved: function handleSaved(_ref2) {
    var navigate = _ref2.navigate,
        kapp = _ref2.kapp,
        techBarId = _ref2.techBarId,
        updateTechBarSettingsSuccess = _ref2.updateTechBarSettingsSuccess;
    return function (_ref3) {
      var submission = _ref3.submission;
      updateTechBarSettingsSuccess({
        techBarId: techBarId,
        submission: submission
      });
      navigate("../");
    };
  }
}))(TechBarSettingsFormComponent);