"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(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

(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() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

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 = function SubPage(_ref) {
  var _props$title, _props$title2, _props$title3;

  var config = _ref.config,
      match = _ref.match,
      location = _ref.location,
      history = _ref.history,
      root = _ref.root,
      modulePrependPath = _ref.modulePrependPath;
  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 basePathArr = path.split("/");
  basePathArr.pop();
  var basePath = basePathArr.join("/");
  var WrappedView = (0, _react.useMemo)(function () {
    return (0, _withErrorBoundary.default)(path)((0, _withWaypointing.default)(View));
  }, [path, View]);

  var closeSubPage = function closeSubPage() {
    history.push(basePath);
    dispatch((0, _navigation.mapHref)(basePath, basePath));
  };

  var message = /*#__PURE__*/_react.default.createElement(WrappedView, _extends({
    match: match,
    location: location,
    mapFrom: baseHref
  }, props, {
    modulePrependPath: modulePrependPath
  }));

  var modalProps = new _modalProps.default();
  var titleComponent = props != null && (_props$title = props.title) != null && _props$title.id ? /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: props == null ? void 0 : (_props$title2 = props.title) == null ? void 0 : _props$title2.id,
    defaultMesaage: props == null ? void 0 : (_props$title3 = props.title) == null ? void 0 : _props$title3.defaultMessage
  }) : 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");
  modalProps.set(_modalProps.default.propNames.backdropClickCallback, closeSubPage);

  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)));

  modalProps.set(_modalProps.default.propNames.actionPanel, actionPanel);
  return /*#__PURE__*/_react.default.createElement(_Modal.default, {
    message: message,
    modalProps: modalProps
  });
};

exports.SubPage = SubPage;

__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 = _default2;
exports.default = _default3;
;

(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);
})();