"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mapStateToProps = exports.mapDispatchToProps = exports.TechBarSettingsFormComponent = exports.TechBarSettingsForm = void 0;
var _react = _interopRequireDefault(require("react"));
var _store = require("../../../redux/store");
var _recompose = require("recompose");
var _bundleCommon = require("@kineticdata/bundle-common");
var _PageTitle = require("../../shared/PageTitle");
var _react2 = require("@kineticdata/react");
var _techBarApp = require("../../../redux/modules/techBarApp");
var _constants = require("../../../constants");
var TechBarSettingsFormComponent = exports.TechBarSettingsFormComponent = function TechBarSettingsFormComponent(_ref) {
  var kapp = _ref.kapp,
    techBar = _ref.techBar,
    handleSaved = _ref.handleSaved,
    hasManagerAccess = _ref.hasManagerAccess;
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "page-container"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "page-panel"
  }, /*#__PURE__*/_react["default"].createElement(_PageTitle.PageTitle, {
    parts: [techBar && "".concat(techBar.values['Name'], " Settings")],
    settings: true,
    breadcrumbs: [{
      label: 'Home',
      to: '/'
    }, {
      label: "".concat(kapp.name, " Settings"),
      to: '../../..'
    }, {
      label: 'Tech Bars',
      to: '../..'
    }, techBar && {
      label: techBar.values['Name'],
      to: '..'
    }],
    title: techBar && techBar.values['Name'],
    actions: [techBar && {
      label: 'Cancel Edit',
      to: '..',
      className: 'btn-outline-danger'
    }]
  }), techBar ? hasManagerAccess ? /*#__PURE__*/_react["default"].createElement("div", {
    className: "content-wrapper form-unstyled"
  }, /*#__PURE__*/_react["default"].createElement(_react2.I18n, {
    context: "kapps.datastore.forms.".concat(_constants.TECH_BAR_SETTINGS_FORM_SLUG)
  }, techBar.settings.submissionId ? /*#__PURE__*/_react["default"].createElement(_react2.CoreForm, {
    submission: techBar.settings.submissionId,
    created: handleSaved,
    updated: handleSaved
  }) : /*#__PURE__*/_react["default"].createElement(_react2.CoreForm, {
    kapp: "datastore",
    form: _constants.TECH_BAR_SETTINGS_FORM_SLUG,
    created: handleSaved,
    updated: handleSaved,
    values: {
      'Scheduler Id': techBar.values['Id']
    }
  }))) : /*#__PURE__*/_react["default"].createElement(_bundleCommon.ErrorUnauthorized, null) : /*#__PURE__*/_react["default"].createElement(_bundleCommon.ErrorNotFound, null)));
};
var mapStateToProps = exports.mapStateToProps = function mapStateToProps(state, props) {
  var isSchedulerAdmin = (0, _bundleCommon.selectHasRoleSchedulerAdmin)(state.app.profile);
  var techBar = state.techBarApp.schedulers.find(function (scheduler) {
    return scheduler.id === props.techBarId && (isSchedulerAdmin || _bundleCommon.Utils.isMemberOf(state.app.profile, "Role::Scheduler::".concat(scheduler.values['Name'])) || _bundleCommon.Utils.isMemberOf(state.app.profile, "Scheduler::".concat(scheduler.values['Name'])));
  });
  return {
    kapp: state.app.kapp,
    techBar: techBar,
    hasManagerAccess: isSchedulerAdmin || techBar && _bundleCommon.Utils.isMemberOf(state.app.profile, "Role::Scheduler::".concat(techBar.values['Name']))
  };
};
var mapDispatchToProps = exports.mapDispatchToProps = {
  updateTechBarSettingsSuccess: _techBarApp.actions.updateTechBarSettingsSuccess
};
var TechBarSettingsForm = exports.TechBarSettingsForm = (0, _recompose.compose)((0, _store.connect)(mapStateToProps, mapDispatchToProps), (0, _recompose.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);