"use strict";

exports.__esModule = true;
exports.default = exports.SubPage = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactRedux = require("react-redux");
var _urlPattern = _interopRequireDefault(require("url-pattern"));
var _withErrorBoundary = _interopRequireDefault(require("../../hocs/withErrorBoundary"));
var _navigation = require("../../actions/navigation");
var _withWaypointing = _interopRequireDefault(require("./withWaypointing"));
var _Modal = _interopRequireDefault(require("../MaterialUI/DataDisplay/Modal"));
var _modalProps = _interopRequireDefault(require("../MaterialUI/DataDisplay/modalProps"));
var _Button = _interopRequireDefault(require("@material-ui/core/Button"));
var _styles = require("@material-ui/core/styles");
var _sharedMessages = _interopRequireDefault(require("../../sharedMessages"));
var _reactIntl = require("react-intl");
var _excluded = ["component"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var useStyles = (0, _styles.makeStyles)(function (theme) {
  return {
    actionPanel: {
      display: "flex",
      marginLeft: "auto",
      flex: "1 1 0",
      justifyContent: "flex-end"
    }
  };
});
var SubPage = exports.SubPage = function SubPage(_ref) {
  var _props$title, _props$title2, _props$title3, _props$componentProps;
  var config = _ref.config,
    match = _ref.match,
    location = _ref.location,
    history = _ref.history,
    root = _ref.root,
    modulePrependPath = _ref.modulePrependPath,
    parentUrlPattern = _ref.parentUrlPattern;
  var classes = useStyles();
  var dispatch = (0, _reactRedux.useDispatch)();
  var View = config.component,
    props = _objectWithoutProperties(config, _excluded);
  var pattern = new _urlPattern.default(root);
  var baseHref = pattern.stringify(match.params);
  var path = location.pathname;
  var WrappedView = (0, _react.useMemo)(function () {
    return (0, _withErrorBoundary.default)(path)((0, _withWaypointing.default)(View));
  }, [path, View]);
  var closeSubPage = function closeSubPage() {
    var parentHref = parentUrlPattern.stringify(match.params);
    history.push(parentHref);
    dispatch((0, _navigation.mapHref)(parentHref, parentHref));
  };
  var message = /*#__PURE__*/_react.default.createElement(WrappedView, _extends({
    match: match,
    location: location,
    mapFrom: baseHref
  }, props, {
    modulePrependPath: modulePrependPath
  }));
  var modalProps = new _modalProps.default();
  var formattedMsgProps = {
    id: props == null || (_props$title = props.title) == null ? void 0 : _props$title.id,
    defaultMessage: props == null || (_props$title2 = props.title) == null ? void 0 : _props$title2.defaultMessage
  };
  var titleComponent = props != null && (_props$title3 = props.title) != null && _props$title3.id ? /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, formattedMsgProps) : props == null ? void 0 : props.title;
  modalProps.set(_modalProps.default.propNames.title, titleComponent);
  modalProps.set(_modalProps.default.propNames.open, true);
  modalProps.set(_modalProps.default.propNames.type, "fullwidth");
  var actionPanel = /*#__PURE__*/_react.default.createElement("div", {
    className: classes.actionPanel
  }, /*#__PURE__*/_react.default.createElement(_Button.default, {
    variant: "contained",
    color: "primary",
    disableElevation: true,
    onClick: closeSubPage
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _sharedMessages.default.close)));
  if (((_props$componentProps = props.componentProps) == null || (_props$componentProps = _props$componentProps.actionPanel()) == null ? void 0 : _props$componentProps.length) > 0) {
    var _props$componentProps2;
    actionPanel = /*#__PURE__*/_react.default.createElement("div", {
      className: classes.actionPanel
    }, (_props$componentProps2 = props.componentProps) == null ? void 0 : _props$componentProps2.actionPanel().map(function (action) {
      var _action$label;
      return /*#__PURE__*/_react.default.createElement(_Button.default, {
        key: (_action$label = action.label) == null ? void 0 : _action$label.id,
        variant: action.isPrimary ? "contained" : "outlined",
        color: action.isPrimary ? "primary" : "default",
        disableElevation: action.isPrimary,
        disabled: action.disabled,
        onClick: function onClick(e) {
          if (!action.handler) {
            closeSubPage();
            return;
          }
          var actionHandlerResult = action.handler(e);
          if (typeof actionHandlerResult === "object") {
            // promise
            actionHandlerResult.then(function (actionResult) {
              if (action.validateBeforeClose && !actionResult) return;
              closeSubPage();
            });
          } else {
            if (action.validateBeforeClose && !actionHandlerResult) return;
            closeSubPage();
          }
        }
      }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, action.label));
    }));
  }
  modalProps.set(_modalProps.default.propNames.actionPanel, actionPanel);
  return /*#__PURE__*/_react.default.createElement(_Modal.default, {
    message: message,
    modalProps: modalProps
  });
};
__signature__(SubPage, "useStyles{classes}\nuseDispatch{dispatch}\nuseMemo{WrappedView}", function () {
  return [useStyles, _reactRedux.useDispatch];
});
__signature__(SubPage, "useStyles{classes}\nuseDispatch{dispatch}\nuseMemo{WrappedView}", function () {
  return [useStyles, _reactRedux.useDispatch];
});
SubPage.defaultProps = {
  theme: {
    icons: {
      backArrow: "arrow-left"
    }
  }
};
var _default = SubPage;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/Routing/SubPage.js");
  reactHotLoader.register(SubPage, "SubPage", "/home/vsts/work/1/s/src/components/Routing/SubPage.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/Routing/SubPage.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/Routing/SubPage.js");
  reactHotLoader.register(SubPage, "SubPage", "/home/vsts/work/1/s/src/components/Routing/SubPage.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/Routing/SubPage.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();