"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports["default"] = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

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

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

var _lodash = require("lodash");

var _title = _interopRequireDefault(require("../title"));

var _context = _interopRequireDefault(require("../context"));

var _Utils = require("../Utils");

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

/**
 * 条形堆叠占比图
 * @param props IProps
 */
var BarStackChart = function BarStackChart(props) {
  var _useContext = (0, _react.useContext)(_context["default"]),
      _cssPrefix = _useContext._cssPrefix;

  var className = props.className,
      universalStyle = props.universalStyle,
      _props$dataSource = props.dataSource,
      dataSource = _props$dataSource === void 0 ? [] : _props$dataSource,
      title = props.title,
      total = props.total,
      _props$titleOptions = props.titleOptions,
      titleOptions = _props$titleOptions === void 0 ? {} : _props$titleOptions,
      _props$normOptions = props.normOptions,
      normOptions = _props$normOptions === void 0 ? {} : _props$normOptions,
      _props$barOptions = props.barOptions,
      barOptions = _props$barOptions === void 0 ? {} : _props$barOptions;
  var defaultTotal = (0, _lodash.get)(props, 'total', (0, _lodash.every)(dataSource, function (el) {
    return (0, _lodash.isNumber)(el.value);
  }) ? (0, _lodash.reduce)(dataSource, function (sum, el) {
    return sum + el.value;
  }, 0) : total);
  var barNodes = dataSource.map(function (item, index) {
    var value = item.value;
    var color = (0, _lodash.get)(item, 'color', "var(--color-data1-" + (index + 1) + ")"); // DATA_COLORS[index]

    var tmp;

    if ((0, _lodash.every)(dataSource, function (el) {
      return (0, _lodash.isNumber)(el.value);
    })) {
      tmp = value / defaultTotal * 100;
    } else if ((0, _lodash.isString)(value) && value.endsWith('%')) {
      tmp = value.slice(0, -1);
    }

    var rate = (0, _lodash.get)(item, 'rate', tmp);
    return /*#__PURE__*/_react["default"].createElement("div", {
      key: index,
      className: _cssPrefix + "bar-stack-chart-bar-inner",
      style: {
        backgroundColor: color,
        width: rate + "%"
      }
    });
  });
  var normNodes = dataSource.map(function (item, index) {
    var label = item.label,
        value = item.value;
    var color = (0, _lodash.get)(item, 'color', "var(--color-data1-" + (index + 1) + ")"); // DATA_COLORS[index]

    return /*#__PURE__*/_react["default"].createElement("div", {
      key: index,
      className: _cssPrefix + "bar-stack-chart-norm",
      style: {
        marginBottom: (0, _lodash.get)(normOptions, 'lineSpacing')
      }
    }, /*#__PURE__*/_react["default"].createElement(_title["default"], (0, _extends2["default"])({}, (0, _lodash.get)(normOptions, 'labelOptions', {}), {
      className: (0, _classnames["default"])(_cssPrefix + "bar-stack-chart-norm-label", (0, _lodash.get)(normOptions, 'labelOptions.className')) // `${_cssPrefix}sub-chart-title`, 

    }), (0, _lodash.get)(normOptions, 'dots', true) && /*#__PURE__*/_react["default"].createElement("span", {
      className: _cssPrefix + "bar-stack-chart-norm-label-dot",
      style: {
        backgroundColor: color
      }
    }), label), /*#__PURE__*/_react["default"].createElement(_title["default"], (0, _extends2["default"])({}, (0, _lodash.get)(normOptions, 'valueOptions', {
      textStyle: {
        fontFamily: 'HelveticaNeue-CondensedBold'
      }
    }), {
      className: (0, _classnames["default"])(_cssPrefix + "bar-stack-chart-norm-value", (0, _lodash.get)(normOptions, 'valueOptions.className')) // `${_cssPrefix}sub-chart-title`, 

    }), value));
  });
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])(className, _cssPrefix + "bar-stack-chart-wrapper"),
    style: {
      width: (0, _lodash.get)(universalStyle, 'width'),
      height: (0, _lodash.get)(universalStyle, 'height'),
      backgroundColor: (0, _lodash.get)(universalStyle, 'backgroundColor')
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: _cssPrefix + "bar-stack-chart-top"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: _cssPrefix + "bar-stack-chart-top-inner-left"
  }, title && /*#__PURE__*/_react["default"].createElement(_title["default"], (0, _extends2["default"])({}, titleOptions, {
    className: (0, _classnames["default"])(_cssPrefix + "bar-stack-chart-title", _cssPrefix + "main-chart-title", (0, _lodash.get)(titleOptions, 'className')),
    align: (0, _lodash.get)(titleOptions, 'align', 'left')
  }), title), (0, _lodash.isNumber)(total) ? /*#__PURE__*/_react["default"].createElement("div", {
    className: _cssPrefix + "data " + _cssPrefix + "bar-stack-chart-total"
  }, (0, _Utils.NWC)(defaultTotal)) : total), /*#__PURE__*/_react["default"].createElement("div", {
    className: _cssPrefix + "bar-stack-chart-norm-wrapper",
    style: {
      width: (0, _lodash.get)(normOptions, 'widthRate', 50) + "%"
    }
  }, normNodes)), /*#__PURE__*/_react["default"].createElement("div", {
    className: _cssPrefix + "bar-stack-chart-bar",
    style: {
      backgroundColor: (0, _lodash.get)(barOptions, 'backgroundColor', '#112854'),
      height: (0, _lodash.get)(barOptions, 'height', 8),
      marginTop: (0, _lodash.get)(barOptions, 'spacing', 8)
    }
  }, barNodes));
};

var _default = BarStackChart;
exports["default"] = _default;