"use strict";

exports.__esModule = true;
exports.default = exports.Wrapper = exports.SegmentItem = exports.List = exports.Item = void 0;
var _react = _interopRequireDefault(require("react"));
var _styledComponents = _interopRequireWildcard(require("styled-components"));
var _reactRouterDom = require("react-router-dom");
var _urlPattern = _interopRequireDefault(require("url-pattern"));
var _utils = require("../../utils");
var _Text = _interopRequireDefault(require("../Text"));
var _FullPage = _interopRequireDefault(require("./FullPage"));
var _SubPage = _interopRequireDefault(require("./SubPage"));
var _Segment = _interopRequireDefault(require("./Segment"));
var _view = require("./../../selectors/view");
var _reactRedux = require("react-redux");
var _styles = require("@material-ui/core/styles");
var _Grid = _interopRequireDefault(require("@material-ui/core/Grid"));
var _TooltippedTypography = _interopRequireDefault(require("./../MaterialUI/DataDisplay/TooltippedElements/TooltippedTypography"));
var _urlHelper = require("./../../utils/urlHelper");
var _classnames = _interopRequireDefault(require("classnames"));
var _excluded = ["active"];
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 _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
(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 _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
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 {
    asterix: {
      marginLeft: theme.spacing(0.5)
    },
    label: {
      color: theme.palette.text.primary,
      fontWeight: theme.typography.fontWeightSemiBold,
      fontSize: theme.typography.fontSize,
      maxWidth: theme.spacing(15)
    },
    modifiedLabel: {
      color: theme.palette.text.primary,
      fontWeight: theme.typography.fontWeightBold
    },
    errorLabel: {
      color: theme.palette.error.main,
      fontWeight: theme.typography.fontWeightBold
    },
    disabledLabel: {
      color: theme.palette.grey.icon
    },
    labelComponent: {
      margin: "0 ".concat(theme.spacing(1)),
      justifyContent: "flex-end"
    },
    labelContainer: {
      display: "flex"
    },
    wrapper: {
      "div[class^='AppFrame__ViewPort'] > div&:nth-child(3)": {
        marginTop: function marginTop(props) {
          return props.isComponentNull ? null : theme.spacing(6);
        }
      }
    }
  };
});
var Wrapper = exports.Wrapper = _styledComponents.default.div.withConfig({
  displayName: "SegmentPage__Wrapper",
  componentId: "sc-1b2lq1m-0"
})(["box-sizing:border-box;display:flex;border-top:1px solid ", ";flex:0 1 100%;height:calc(100% - 90px);min-height:0;"], (0, _utils.getThemeProp)(["colors", "borderLight"], "#cccccc"));
var List = exports.List = _styledComponents.default.div.withConfig({
  displayName: "SegmentPage__List",
  componentId: "sc-1b2lq1m-1"
})(["flex:0 0.1 15%;border-right:1px solid ", ";display:flex;flex-direction:column;overflow-y:auto;"], (0, _utils.getThemeProp)(["colors", "borderLight"], "#cccccc"));
var FilteredLink = function FilteredLink(_ref) {
  var active = _ref.active,
    props = _objectWithoutProperties(_ref, _excluded);
  return props.to ? /*#__PURE__*/_react.default.createElement(_reactRouterDom.Link, props) : /*#__PURE__*/_react.default.createElement("div", props);
};
var Item = exports.Item = (0, _styledComponents.default)(FilteredLink).withConfig({
  displayName: "SegmentPage__Item",
  componentId: "sc-1b2lq1m-2"
})(["display:block;white-space:nowrap;min-width:max-content;padding:15px 20px;font-weight:bold;font-size:13px;text-decoration:none;cursor:pointer;color:", ";", ";"], (0, _utils.getThemeProp)(["colors", "text"], "#333333"), (0, _utils.ifFlag)("active", (0, _styledComponents.css)(["background-color:#b4cfe3;"]), (0, _styledComponents.css)(["&:hover{background-color:#f7f7f7;}"])));
var SegmentItem = exports.SegmentItem = function SegmentItem(_ref2) {
  var isModified = _ref2.isModified,
    isError = _ref2.isError,
    isActive = _ref2.isActive,
    segpath = _ref2.segpath,
    config = _ref2.config,
    baseHref = _ref2.baseHref,
    params = _ref2.params;
  var classes = useStyles();
  var hideSelector = function hideSelector(state) {
    var _config$hide;
    return typeof config.hide === "function" ? config.hide(params)(state) : (_config$hide = config.hide) != null ? _config$hide : false;
  };
  var isHide = (0, _reactRedux.useSelector)(hideSelector);
  var asterix = /*#__PURE__*/_react.default.createElement("span", {
    className: classes.asterix
  }, "*");
  if (config.labelValueSelector) {
    var values = config.labelValueSelector(params);
    if (typeof values === "function") {
      config.label.values = values;
    } else if (config.label.values) {
      delete config.label.values;
    }
  }
  var text = /*#__PURE__*/_react.default.createElement(_Text.default, {
    message: config.label
  });
  var getSectionLabelClassName = function getSectionLabelClassName(isModified, isError, isDisabled) {
    var className = classes.label;
    if (isModified) className = "".concat(className, " ").concat(classes.modifiedLabel);
    if (isError) className = "".concat(className, " ").concat(classes.errorLabel);
    if (isDisabled) className = "".concat(className, " ").concat(classes.disabledLabel);
    return className;
  };
  var disableSelector = function disableSelector(state) {
    var _config$disabled;
    return typeof config.disabled === "function" ? config.disabled(params)(state) : (_config$disabled = config.disabled) != null ? _config$disabled : false;
  };
  var isDisabled = (0, _reactRedux.useSelector)(disableSelector);
  var sectionLabelClassName = getSectionLabelClassName(isModified, isError, isDisabled);
  var basicLabel = config.labelComponent != null ? /*#__PURE__*/_react.default.createElement(_TooltippedTypography.default, {
    titleValue: text,
    children: text,
    noWrap: true,
    className: sectionLabelClassName
  }) : text;
  var finalLabel = /*#__PURE__*/_react.default.createElement(_Grid.default, {
    container: true,
    alignItems: "center",
    wrap: "nowrap"
  }, /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 8,
    className: (0, _classnames.default)(sectionLabelClassName, config.labelComponent != null ? classes.labelContainer : null)
  }, basicLabel, isModified ? asterix : null), /*#__PURE__*/_react.default.createElement(_Grid.default, {
    item: true,
    xs: 4,
    container: true,
    className: classes.labelComponent
  }, config.labelComponent));
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, !isHide && isDisabled && /*#__PURE__*/_react.default.createElement(Item, null, finalLabel), !isHide && !isDisabled && /*#__PURE__*/_react.default.createElement(Item, {
    to: baseHref + segpath,
    active: isActive
  }, finalLabel));
};
__signature__(SegmentItem, "useStyles{classes}\nuseSelector{isHide}\nuseSelector{isDisabled}", function () {
  return [useStyles, _reactRedux.useSelector, _reactRedux.useSelector];
});
__signature__(SegmentItem, "useStyles{classes}\nuseSelector{isHide}\nuseSelector{isDisabled}", function () {
  return [useStyles, _reactRedux.useSelector, _reactRedux.useSelector];
});
var defaultEntityIdResolver = function defaultEntityIdResolver(_ref3) {
  var match = _ref3.match,
    baseHref = _ref3.baseHref;
  var entityIdKey = Object.keys(match.params).find(function (p) {
    return p !== "scope";
  });
  var entityId = match.params[entityIdKey];
  if (!entityId) {
    entityId = (0, _urlHelper.tryGetNewEntityIdKey)(baseHref);
  }
  return entityId;
};
var SegmentPage = function SegmentPage(_ref4) {
  var path = _ref4.path,
    View = _ref4.component,
    componentProps = _ref4.componentProps,
    segments = _ref4.segments,
    location = _ref4.location,
    match = _ref4.match,
    modulePrependPath = _ref4.modulePrependPath,
    entityIdResolver = _ref4.entityIdResolver;
  var classes = useStyles({
    isComponentNull: !View
  });
  var pattern = new _urlPattern.default(path);
  var baseHref = pattern.stringify(match.params);
  var pages = [],
    subpages = [];
  var entityId = entityIdResolver ? entityIdResolver({
    match: match
  }) : defaultEntityIdResolver({
    match: match,
    baseHref: baseHref
  });
  var modifiedSections = (0, _reactRedux.useSelector)((0, _view.getModifiedSections)(entityId));
  var sectionsWithErrors = (0, _reactRedux.useSelector)((0, _view.getSectionsWithErrors)(entityId));
  var segmentEntries = Object.entries(segments);
  var segmentElements = segmentEntries.map(function (_ref5) {
    var _ref6 = _slicedToArray(_ref5, 2),
      segpath = _ref6[0],
      config = _ref6[1];
    if (config.pages) {
      pages.push.apply(pages, Object.entries(config.pages).map(function (_ref7) {
        var _ref8 = _slicedToArray(_ref7, 2),
          subpath = _ref8[0],
          pageConfig = _ref8[1];
        var pagePath = segpath + subpath;
        return /*#__PURE__*/_react.default.createElement(_reactRouterDom.Route, {
          key: pagePath,
          path: path + pagePath,
          render: function render(route) {
            return /*#__PURE__*/_react.default.createElement(_FullPage.default, _extends({
              path: path + pagePath,
              config: pageConfig
            }, route, {
              modulePrependPath: modulePrependPath
            }));
          }
        });
      }));
    }
    if (config.subpages) {
      var parentUrl = path + segpath;
      var parentUrlPattern = new _urlPattern.default(parentUrl);
      subpages.push.apply(subpages, Object.entries(config.subpages).map(function (_ref9) {
        var _ref10 = _slicedToArray(_ref9, 2),
          subpath = _ref10[0],
          config = _ref10[1];
        var pagePath = segpath + subpath;
        return /*#__PURE__*/_react.default.createElement(_reactRouterDom.Route, {
          key: pagePath,
          path: path + pagePath,
          render: function render(route) {
            return /*#__PURE__*/_react.default.createElement(_SubPage.default, _extends({
              root: path,
              config: config,
              parentUrlPattern: parentUrlPattern
            }, route, {
              modulePrependPath: modulePrependPath
            }));
          }
        });
      }));
    }
    return /*#__PURE__*/_react.default.createElement(_reactRouterDom.Route, {
      key: segpath,
      path: path + segpath,
      render: function render(route) {
        return /*#__PURE__*/_react.default.createElement(_Segment.default, _extends({
          path: path + segpath,
          config: config,
          root: baseHref
        }, route, {
          modulePrependPath: modulePrependPath
        }));
      }
    });
  });
  return /*#__PURE__*/_react.default.createElement(_reactRouterDom.Switch, null, pages, /*#__PURE__*/_react.default.createElement(_reactRouterDom.Route, {
    render: function render() {
      return [View ? /*#__PURE__*/_react.default.createElement(View, _extends({
        key: "View"
      }, componentProps)) : null, /*#__PURE__*/_react.default.createElement(Wrapper, {
        className: classes.wrapper,
        key: "Segments"
      }, /*#__PURE__*/_react.default.createElement(List, null, segmentEntries.map(function (_ref11) {
        var _ref12 = _slicedToArray(_ref11, 2),
          segpath = _ref12[0],
          config = _ref12[1];
        var isModified = modifiedSections.includes(segpath.replace("/", ""));
        var isError = sectionsWithErrors.includes(segpath.replace("/", ""));
        var isActive = location.pathname === baseHref + segpath;
        return /*#__PURE__*/_react.default.createElement(SegmentItem, {
          key: segpath,
          isModified: isModified,
          isError: isError,
          segpath: segpath,
          config: config,
          isActive: isActive,
          baseHref: baseHref,
          params: match.params
        });
      })), /*#__PURE__*/_react.default.createElement(_reactRouterDom.Switch, null, segmentElements, /*#__PURE__*/_react.default.createElement(_reactRouterDom.Redirect, {
        exact: true,
        path: path,
        to: baseHref + Object.keys(segments)[0]
      })), /*#__PURE__*/_react.default.createElement(_reactRouterDom.Switch, null, subpages))];
    }
  }));
};
__signature__(SegmentPage, "useStyles{classes}\nuseSelector{modifiedSections}\nuseSelector{sectionsWithErrors}", function () {
  return [useStyles, _reactRedux.useSelector, _reactRedux.useSelector];
});
__signature__(SegmentPage, "useStyles{classes}\nuseSelector{modifiedSections}\nuseSelector{sectionsWithErrors}", function () {
  return [useStyles, _reactRedux.useSelector, _reactRedux.useSelector];
});
var _default = SegmentPage;
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/SegmentPage.js");
  reactHotLoader.register(Wrapper, "Wrapper", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
  reactHotLoader.register(List, "List", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
  reactHotLoader.register(FilteredLink, "FilteredLink", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
  reactHotLoader.register(Item, "Item", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
  reactHotLoader.register(SegmentItem, "SegmentItem", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
  reactHotLoader.register(defaultEntityIdResolver, "defaultEntityIdResolver", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
  reactHotLoader.register(SegmentPage, "SegmentPage", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.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/SegmentPage.js");
  reactHotLoader.register(Wrapper, "Wrapper", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
  reactHotLoader.register(List, "List", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
  reactHotLoader.register(FilteredLink, "FilteredLink", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
  reactHotLoader.register(Item, "Item", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
  reactHotLoader.register(SegmentItem, "SegmentItem", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
  reactHotLoader.register(defaultEntityIdResolver, "defaultEntityIdResolver", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
  reactHotLoader.register(SegmentPage, "SegmentPage", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/Routing/SegmentPage.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();