"use strict";

exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _map = _interopRequireDefault(require("lodash/fp/map"));
var _pipe = _interopRequireDefault(require("lodash/fp/pipe"));
var _get = _interopRequireDefault(require("lodash/fp/get"));
var _isEmpty = _interopRequireDefault(require("lodash/fp/isEmpty"));
var _find = _interopRequireDefault(require("lodash/fp/find"));
var _classnames = _interopRequireDefault(require("classnames"));
var _brandTabs = _interopRequireDefault(require("../../../molecule/brand-tabs"));
var _sidebar = require("../../../organism/sidebar");
var _brandForm = _interopRequireDefault(require("../../../organism/brand-form"));
var _brandTable = _interopRequireDefault(require("../../../organism/brand-table"));
var _brandUpload = _interopRequireDefault(require("../../../organism/brand-upload"));
var _brandAnalytics = _interopRequireDefault(require("../../../organism/brand-analytics"));
var _brandDashboard = _interopRequireDefault(require("../../../organism/brand-dashboard"));
var _wizardContents = _interopRequireDefault(require("../../../organism/wizard-contents"));
var _listItems = _interopRequireDefault(require("../../../organism/list-items"));
var _brandLearningPriorities = _interopRequireDefault(require("../../../organism/brand-learning-priorities"));
var _banner = _interopRequireDefault(require("../../../molecule/banner"));
var _setupHeader = _interopRequireDefault(require("../../../organism/setup-header"));
var _headerWithActions = _interopRequireDefault(require("../../../organism/header-with-actions"));
var _loader = _interopRequireDefault(require("../../../atom/loader"));
var _coorpManager = _interopRequireDefault(require("../../../organism/accordion/coorp-manager"));
var _cmPopin = _interopRequireDefault(require("../../../molecule/cm-popin"));
var _buttonLinkIcon = _interopRequireDefault(require("../../../atom/button-link-icon"));
var _expandibleActionableTable = _interopRequireDefault(require("../../../molecule/expandible-actionable-table"));
var _bulkInfos = _interopRequireDefault(require("../../../molecule/bulk-infos"));
var _title = _interopRequireDefault(require("../../../atom/title"));
var _skillEdition = _interopRequireDefault(require("../../../organism/skill-edition"));
var _iconPickerModal = _interopRequireDefault(require("../../../molecule/icon-picker-modal"));
var _translationModal = _interopRequireDefault(require("../../../molecule/translation-modal"));
var _contentSkillModal = _interopRequireDefault(require("../../../organism/content-skill-modal"));
var _style = _interopRequireDefault(require("./style.css"));
var _utils = require("./utils");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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); }
const getStyle = isSelected => isSelected ? _style.default.selectedElement : _style.default.unselectedElement;
const subTabsView = (_subTabs = []) => _map.default.convert({
  cap: false
})((subTab, _index) => /*#__PURE__*/_react.default.createElement("div", {
  key: subTab.title
}, subTab.type === 'iconLink' ? /*#__PURE__*/_react.default.createElement(_sidebar.IconLinkItem, _extends({}, subTab, {
  styles: getStyle(subTab.selected),
  uppercase: false,
  target: '_blank'
})) : /*#__PURE__*/_react.default.createElement(_sidebar.LinkItem, _extends({}, subTab, {
  styles: getStyle(subTab.selected),
  uppercase: false,
  setChildrenAsHtml: false
}))))(_subTabs);

// TODO: this fonction should be replaced by a molecule to avoid this file to be unreadable
const buildLeftNavigation = (logo, items, onItemClick) => {
  const formattedTabs = items.map(({
    key,
    title,
    href,
    selected,
    type = 'simpleTab'
  }, index) => ({
    title,
    selected,
    type,
    href,
    index,
    children: [],
    iconType: key || 'arrow'
  }));
  const formattedTabsViews = (0, _map.default)(tab => /*#__PURE__*/_react.default.createElement("div", {
    key: tab.title,
    className: _style.default.subTabsView
  }, subTabsView(tab.tabs)))(items);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.navigation
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.logo
  }, /*#__PURE__*/_react.default.createElement("a", {
    href: "/"
  }, /*#__PURE__*/_react.default.createElement("img", {
    src: logo
  }))), /*#__PURE__*/_react.default.createElement(_coorpManager.default, {
    tabProps: formattedTabs,
    theme: 'setup',
    onClick: onItemClick
  }, formattedTabsViews));
};
const buildNotifications = notifications => {
  if ((0, _isEmpty.default)(notifications)) {
    return null;
  }
  const notificationsList = notifications.map((notification, index) => {
    return /*#__PURE__*/_react.default.createElement("div", {
      className: !notification.temporary ? _style.default.permanentNotification : null,
      key: index
    }, /*#__PURE__*/_react.default.createElement(_banner.default, notification));
  });
  return /*#__PURE__*/_react.default.createElement("div", {
    className: !(0, _find.default)({
      temporary: true
    }, notifications) ? _style.default.permanentNotifications : null
  }, notificationsList);
};
const buildHeader = header => {
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.header
  }, header.type === 'header-with-actions' ? /*#__PURE__*/_react.default.createElement(_headerWithActions.default, header) : /*#__PURE__*/_react.default.createElement(_setupHeader.default, header));
};
const buildDefaultPopin = popin => {
  const {
    theme,
    icon: popinIcon,
    secondButton: popinSecondButton
  } = popin;
  const {
    icon: themeIcon,
    actionButton: themeActionButton
  } = _utils.POPIN_THEMES[theme] ?? {};
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.popin
  }, /*#__PURE__*/_react.default.createElement(_cmPopin.default, _extends({}, popin, {
    icon: popinIcon || themeIcon,
    secondButton: {
      ...popinSecondButton,
      ...themeActionButton
    }
  })));
};
const buildPopin = popin => {
  if ((0, _isEmpty.default)(popin)) return;
  switch (popin.type) {
    case 'icon-picker':
      return /*#__PURE__*/_react.default.createElement(_iconPickerModal.default, popin);
    case 'translation':
      return /*#__PURE__*/_react.default.createElement(_translationModal.default, popin);
    case 'content':
      return /*#__PURE__*/_react.default.createElement(_contentSkillModal.default, popin);
    default:
      return buildDefaultPopin(popin);
  }
};
const buildDocumentation = documentation => {
  if (!documentation) return null;
  const {
    show = false,
    onClose,
    content = ''
  } = documentation;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(_style.default.documentation, show && _style.default.open)
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.documentationContent
    // eslint-disable-next-line react/no-danger
    ,
    dangerouslySetInnerHTML: {
      __html: content
    }
  }), onClose ? /*#__PURE__*/_react.default.createElement(_buttonLinkIcon.default, {
    className: _style.default.closeButton,
    onClick: onClose,
    "data-name": 'close-icon',
    "aria-label": 'close-icon',
    size: "small",
    icon: "close"
  }) : null);
};
const buildTabs = items => {
  const selectedTab = (0, _pipe.default)((0, _find.default)(e => e.selected), (0, _get.default)('tabs'), (0, _find.default)(e => e.selected))(items);
  const showTabs = selectedTab ? (0, _find.default)(e => e.selected, selectedTab.subTabs) : undefined;
  if (!showTabs) return null;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.tabs
  }, /*#__PURE__*/_react.default.createElement(_brandTabs.default, {
    type: "light",
    tabs: selectedTab.subTabs
  }));
};
const buildContentView = content => {
  if (!content) {
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.loaderContainer
    }, /*#__PURE__*/_react.default.createElement(_loader.default, {
      className: _style.default.loader,
      theme: "coorpmanager"
    }));
  }
  const {
    type
  } = content;
  switch (type) {
    case 'form':
      return /*#__PURE__*/_react.default.createElement(_brandForm.default, content);
    case 'list':
      return /*#__PURE__*/_react.default.createElement(_brandTable.default, content);
    case 'upload':
      return /*#__PURE__*/_react.default.createElement(_brandUpload.default, content);
    case 'analytics-dashboards':
      return /*#__PURE__*/_react.default.createElement(_brandAnalytics.default, content);
    case 'list-content':
    case 'expandible-actionable-table':
      return /*#__PURE__*/_react.default.createElement(_listItems.default, content);
    case 'home':
      return /*#__PURE__*/_react.default.createElement(_brandDashboard.default, content);
    case 'wizard':
    case 'expandible-table':
      return /*#__PURE__*/_react.default.createElement(_wizardContents.default, content);
    case 'bulk-empty-dashboard':
      return /*#__PURE__*/_react.default.createElement(_bulkInfos.default, content);
    case 'table-pending':
      return /*#__PURE__*/_react.default.createElement(_expandibleActionableTable.default, content);
    case 'learning-priorities':
      return /*#__PURE__*/_react.default.createElement(_brandLearningPriorities.default, content);
    case 'skill-edition':
      return /*#__PURE__*/_react.default.createElement(_skillEdition.default, content);
  }
};
const buildDetailsView = details => {
  if (!details) return;
  return /*#__PURE__*/_react.default.createElement(_brandTable.default, details);
};
const buildTitle = title => {
  if ((0, _isEmpty.default)(title)) return;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.title
  }, /*#__PURE__*/_react.default.createElement(_title.default, title));
};
const BrandUpdate = props => {
  const {
    notifications,
    header,
    items,
    content,
    details,
    popin,
    onItemClick,
    documentation,
    contentFixHeight,
    title
  } = props;
  const logo = 'https://static.coorpacademy.com/logo/coorp-manager.svg';
  const leftNavigation = buildLeftNavigation(logo, items, onItemClick);
  const notificationsView = buildNotifications(notifications);
  const documentationView = buildDocumentation(documentation);
  const headerView = buildHeader(header, notifications);
  const tabsView = buildTabs(items);
  const contentView = buildContentView(content);
  const detailsView = buildDetailsView(details);
  const popinView = buildPopin(popin);
  const titleView = buildTitle(title);
  const contentStyle = (0, _classnames.default)([_style.default.content, !(0, _isEmpty.default)(notifications) && _style.default.contentWithNotifications, contentFixHeight && _style.default.contentFixHeight]);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.container
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.left
  }, leftNavigation), /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.contentWrapper
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.headerSticky
  }, headerView), /*#__PURE__*/_react.default.createElement("div", {
    className: contentStyle
  }, notificationsView, titleView, tabsView, contentView, detailsView, documentationView)), popinView);
};
BrandUpdate.defaultProps = {
  notifications: []
};
BrandUpdate.propTypes = process.env.NODE_ENV !== "production" ? {
  notifications: _propTypes.default.arrayOf(_propTypes.default.shape({
    ..._banner.default.propTypes
  })),
  header: _propTypes.default.oneOfType([_propTypes.default.shape({
    ..._setupHeader.default.propTypes
  }), _propTypes.default.shape({
    ..._headerWithActions.default.propTypes,
    type: _propTypes.default.oneOf(['header-with-actions']).isRequired
  })]),
  items: _propTypes.default.arrayOf(_propTypes.default.shape({
    key: _propTypes.default.string,
    title: _propTypes.default.string.isRequired,
    href: _propTypes.default.string.isRequired,
    selected: _propTypes.default.bool.isRequired,
    type: _propTypes.default.string,
    tabs: _propTypes.default.arrayOf(_propTypes.default.shape({
      title: _propTypes.default.string.isRequired,
      href: _propTypes.default.string.isRequired,
      selected: _propTypes.default.bool.isRequired,
      type: _propTypes.default.string,
      subTabs: _propTypes.default.arrayOf(_propTypes.default.shape({
        title: _propTypes.default.string.isRequired,
        href: _propTypes.default.string.isRequired,
        selected: _propTypes.default.bool.isRequired,
        type: _propTypes.default.string
      }))
    }))
  })).isRequired,
  content: _propTypes.default.oneOfType([_propTypes.default.shape({
    ..._brandForm.default.propTypes,
    key: _propTypes.default.string,
    type: _propTypes.default.oneOf(['form'])
  }), _propTypes.default.shape({
    ..._brandTable.default.propTypes,
    key: _propTypes.default.string,
    type: _propTypes.default.oneOf(['list'])
  }), _propTypes.default.shape({
    ..._brandUpload.default.propTypes,
    key: _propTypes.default.string,
    type: _propTypes.default.oneOf(['upload'])
  }), _propTypes.default.shape({
    ..._brandAnalytics.default.propTypes,
    key: _propTypes.default.string,
    type: _propTypes.default.oneOf(['analytics-dashboards'])
  }), _propTypes.default.shape({
    ..._brandDashboard.default.propTypes,
    key: _propTypes.default.string,
    type: _propTypes.default.oneOf(['home'])
  }), _propTypes.default.shape({
    ..._wizardContents.default.propTypes,
    key: _propTypes.default.string,
    type: _propTypes.default.oneOf(['wizard'])
  }), _propTypes.default.shape({
    ..._listItems.default.propTypes,
    key: _propTypes.default.string,
    type: _propTypes.default.oneOf(['list-content', 'expandible-actionable-table'])
  }), _propTypes.default.shape({
    ..._bulkInfos.default.propTypes,
    key: _propTypes.default.string,
    type: _propTypes.default.oneOf(['bulk-empty-dashboard'])
  }), _propTypes.default.shape({
    ..._expandibleActionableTable.default.propTypes,
    key: _propTypes.default.string,
    type: _propTypes.default.oneOf(['table-pending'])
  }), _propTypes.default.shape({
    ..._wizardContents.default.propTypes,
    content: _propTypes.default.shape({
      ..._expandibleActionableTable.default.propTypes,
      ..._wizardContents.default.propTypes.content.propTypes
    }),
    key: _propTypes.default.string,
    type: _propTypes.default.oneOf(['expandible-table'])
  }), _propTypes.default.shape({
    ..._brandLearningPriorities.default.propTypes,
    key: _propTypes.default.string,
    type: _propTypes.default.oneOf(['learning-priorities'])
  }), _propTypes.default.shape({
    ..._skillEdition.default.propTypes,
    key: _propTypes.default.string,
    type: _propTypes.default.oneOf(['skill-edition'])
  })]),
  documentation: _propTypes.default.shape({
    content: _propTypes.default.string,
    show: _propTypes.default.bool,
    onClose: _propTypes.default.func
  }),
  popin: _propTypes.default.oneOfType([_propTypes.default.shape({
    ..._cmPopin.default.propTypes,
    theme: _propTypes.default.oneOf(['published', 'archived', 'deleted'])
  }), _propTypes.default.shape({
    ..._iconPickerModal.default.propTypes,
    type: _propTypes.default.oneOf(['icon-picker'])
  }), _propTypes.default.shape({
    ..._translationModal.default.propTypes,
    type: _propTypes.default.oneOf(['translation'])
  }), _propTypes.default.shape({
    ..._contentSkillModal.default.propTypes,
    type: _propTypes.default.oneOf(['content'])
  })]),
  details: _propTypes.default.shape({
    ..._brandTable.default.propTypes,
    key: _propTypes.default.string,
    type: _propTypes.default.oneOf(['list'])
  }),
  onItemClick: _propTypes.default.func,
  contentFixHeight: _propTypes.default.bool,
  title: _propTypes.default.shape(_title.default.propTypes)
} : {};
var _default = exports.default = BrandUpdate;
//# sourceMappingURL=index.js.map