"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _styles = require("@mui/material/styles");

var _react = _interopRequireWildcard(require("react"));

var _reactPolyglot = require("react-polyglot");

var _reactRedux = require("react-redux");

var _registry = require("../../lib/registry");

var _ui = require("../../ui");

var _Sidebar = _interopRequireDefault(require("../Collection/Sidebar"));

var _react2 = require("@emotion/react");

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 (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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const StylePage = (0, _styles.styled)('div')`
  margin: ${_ui.lengths.pageMargin};
`;
const StyledPageContent = (0, _styles.styled)('div')`
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
`;

const Page = _ref => {
  let {
    match,
    collections,
    isSearchEnabled,
    searchTerm,
    filterTerm
  } = _ref;
  const {
    id
  } = match.params;
  const Content = (0, _react.useMemo)(() => {
    const page = (0, _registry.getAdditionalLink)(id);

    if (!page) {
      return '';
    }

    return page.data;
  }, []);
  const pageContent = (0, _react.useMemo)(() => {
    if (!Content) {
      return (0, _react2.jsx)(StyledPageContent, null, "Page not found");
    }

    return (0, _react2.jsx)(StyledPageContent, null, (0, _react2.jsx)(Content, null));
  }, [Content]);
  return (0, _react2.jsx)(StylePage, null, (0, _react2.jsx)(_Sidebar.default, {
    collections: collections,
    collection: false,
    isSearchEnabled: isSearchEnabled,
    searchTerm: searchTerm,
    filterTerm: filterTerm
  }), pageContent);
};

function mapStateToProps(state, ownProps) {
  const {
    collections
  } = state;
  const isSearchEnabled = state.config && state.config.search != false;
  const {
    match
  } = ownProps;
  const {
    searchTerm = '',
    filterTerm = ''
  } = match.params;
  return {
    collections,
    isSearchEnabled,
    searchTerm,
    filterTerm
  };
}

const mapDispatchToProps = {};

function mergeProps(stateProps, dispatchProps, ownProps) {
  return _objectSpread(_objectSpread(_objectSpread({}, stateProps), dispatchProps), ownProps);
}

const ConnectedPage = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps, mergeProps)(Page);

var _default = (0, _reactPolyglot.translate)()(ConnectedPage);

exports.default = _default;