"use strict";

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

var _propTypes = _interopRequireDefault(require("prop-types"));

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

var _classnames = _interopRequireDefault(require("classnames"));

var _Row = _interopRequireDefault(require("../Layout/Row"));

var _HelpPopover = _interopRequireDefault(require("../HelpPopover"));

var _Col = _interopRequireDefault(require("../Layout/Col"));

var _Container = _interopRequireDefault(require("../Layout/Container"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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 SummaryBar = _react["default"].forwardRef(function (_ref, ref) {
  var className = _ref.className,
      columns = _ref.columns,
      extraInfo = _ref.extraInfo,
      testSection = _ref.testSection,
      title = _ref.title,
      props = _objectWithoutProperties(_ref, ["className", "columns", "extraInfo", "testSection", "title"]);

  var maxNumColumns = Math.min(columns.length, 5);
  var columnsToRender = columns.slice(0, 5);
  var columnHeaders = columnsToRender.map(function (child, idx) {
    var border;

    if (idx !== maxNumColumns - 1) {
      border = "right";
    }

    return _react["default"].createElement(_Col["default"], {
      key: idx,
      border: border,
      "data-test-section": "".concat(testSection, "-col-").concat(idx, "-header")
    }, _react["default"].createElement("div", {
      className: "flex min-width--100"
    }, child.header && _react["default"].createElement("div", {
      className: "muted micro weight--bold push-half--bottom"
    }, child.header), child.headerHelpTooltip && _react["default"].createElement("div", {
      className: "muted oui-summary-bar-col-header__help-popover"
    }, _react["default"].createElement(_HelpPopover["default"], {
      horizontalAttachment: "left",
      verticalAttachment: "middle"
    }, child.headerHelpTooltip))));
  });
  var columnContent = columnsToRender.map(function (child, idx) {
    var border;

    if (idx !== maxNumColumns - 1) {
      border = "right";
    }

    var colorClass = child.bodyContent ? child.bodyContent.color === "green" ? "color--good-news" : child.bodyContent.color === "red" ? "color--bad-news" : "color--base" : "";
    return _react["default"].createElement(_Col["default"], {
      key: idx,
      border: border,
      "data-test-section": "".concat(testSection, "-col-").concat(idx, "-content")
    }, _react["default"].createElement("div", {
      className: "flex min-width--100"
    }, child.bodyContent && _react["default"].createElement("div", {
      className: (0, _classnames["default"])(colorClass, {
        "beta weight--normal": child.bodyContent.isNumber,
        micro: !child.bodyContent.isNumber
      })
    }, child.bodyContent.value), child.bodyContent && child.bodyContent.helpTooltip && _react["default"].createElement("div", {
      className: "muted push-half--left push-half--top"
    }, _react["default"].createElement(_HelpPopover["default"], {
      horizontalAttachment: "left",
      verticalAttachment: "middle"
    }, child.bodyContent.helpTooltip))));
  });
  return _react["default"].createElement("div", _extends({
    ref: ref,
    className: (0, _classnames["default"])("width--1-1", className),
    "data-test-section": testSection
  }, props), _react["default"].createElement("div", {
    className: "flex flex-align--end"
  }, _react["default"].createElement("div", {
    className: "color--base push-half--bottom push-double--right gamma"
  }, title), extraInfo && _react["default"].createElement("p", {
    className: "muted micro anchor--right push-half--bottom"
  }, extraInfo)), _react["default"].createElement(_Container["default"], {
    paddedContent: "around",
    fluid: true
  }, _react["default"].createElement(_Row["default"], {
    shouldWrap: false,
    border: "top",
    paddedContent: "top"
  }, columnHeaders), _react["default"].createElement(_Row["default"], {
    shouldWrap: false,
    border: "bottom",
    paddedContent: "bottom"
  }, columnContent)));
});

exports.SummaryBar = SummaryBar;
SummaryBar.displayName = "SummaryBar";
SummaryBar.propTypes = {
  title: _propTypes["default"].string.isRequired,
  testSection: _propTypes["default"].string.isRequired,
  extraInfo: _propTypes["default"].string,
  className: _propTypes["default"].string,

  /** Content columns where summary info is displayed. Requires 2-5 columns.
   *  @param {Object} props Object of props
   *  @returns {Error} Error or null
   */
  columns: function verifyNumberOfColumns(props) {
    if (props.columns.length > 5) {
      return new Error("Number of columns must not exceed 5, only 5 will be shown.");
    } else if (props.columns.length < 2) {
      return new Error("Number of columns must be greater than 1.");
    }

    return null;
  }
};
var _default = SummaryBar;
exports["default"] = _default;