"use strict";

exports.__esModule = true;
exports.default = exports.SegmentItem = exports.Item = exports.List = exports.Wrapper = 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(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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

(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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

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 {
    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 = _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"));

exports.Wrapper = Wrapper;

var 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;"], (0, _utils.getThemeProp)(["colors", "borderLight"], "#cccccc"));

exports.List = List;

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 = (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;}"])));
exports.Item = Item;

var 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
  }, "*");

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

exports.SegmentItem = SegmentItem;

__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) {
      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
            }, 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 = _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/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);
})();