import React from 'react';
import { STANDARD_WEIGHT_UNIT, formatWeightValue, getWeightUnitSymbol, locales } from '@pisell/utils';
import { PREFIX } from "../../index";
import "./index.less";
var formatWeight = function formatWeight(value, unit) {
  var parsedValue = Number(value);
  var displayValue = Number.isFinite(parsedValue) ? parsedValue : 0;
  var displayUnit = getWeightUnitSymbol(unit);
  return "".concat(formatWeightValue(displayValue), " ").concat(displayUnit);
};
var Weighing = function Weighing(props) {
  var _ref, _tare$value, _tare$value2;
  var dataSource = props.dataSource,
    symbol = props.symbol;
  if (!dataSource) {
    return null;
  }
  var mode = dataSource.mode,
    weight_source = dataSource.weight_source,
    weight = dataSource.weight,
    tare = dataSource.tare,
    tare_weight = dataSource.tare_weight,
    net_weight = dataSource.net_weight,
    unit_price = dataSource.unit_price,
    _dataSource$unit = dataSource.unit,
    unit = _dataSource$unit === void 0 ? STANDARD_WEIGHT_UNIT : _dataSource$unit;
  var unitLabel = getWeightUnitSymbol(unit);
  var tareValue = Number((_ref = (_tare$value = tare === null || tare === void 0 ? void 0 : tare.value) !== null && _tare$value !== void 0 ? _tare$value : tare_weight) !== null && _ref !== void 0 ? _ref : 0);
  var tareUnit = (tare === null || tare === void 0 ? void 0 : tare.unit) || unit;
  var isManualWeight = (mode !== null && mode !== void 0 ? mode : weight_source) === 'manual';
  var grossLabel = locales.getText('pisell2.cart.sku-card.weighing.gross');
  var tareLabel = locales.getText('pisell2.cart.sku-card.weighing.tare');
  var netLabel = locales.getText('pisell2.cart.sku-card.weighing.net');
  var unitPriceText = "".concat(symbol || '').concat(unit_price !== null && unit_price !== void 0 ? unit_price : '0.00');
  var manualPrefix = isManualWeight ? '(M) ' : '';
  var netWeightText = "".concat(tareValue > 0 ? '' : manualPrefix).concat(formatWeight(net_weight, unit), " ").concat(netLabel);
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__weighing")
  }, tareValue > 0 ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__weighing-row ").concat(PREFIX, "__weighing-row--large")
  }, /*#__PURE__*/React.createElement("span", null, "".concat(manualPrefix).concat(grossLabel, ": ").concat(formatWeight(weight, unit)))) : null, tareValue > 0 ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__weighing-row ").concat(PREFIX, "__weighing-row--large")
  }, /*#__PURE__*/React.createElement("span", null, "".concat(tareLabel, ": ").concat(formatWeight((_tare$value2 = tare === null || tare === void 0 ? void 0 : tare.value) !== null && _tare$value2 !== void 0 ? _tare$value2 : tare_weight, tareUnit)))) : null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__weighing-row ").concat(PREFIX, "__weighing-row--large")
  }, /*#__PURE__*/React.createElement("span", null, netWeightText), /*#__PURE__*/React.createElement("span", {
    className: "".concat(PREFIX, "__weighing-unit-price")
  }, "@ ".concat(unitPriceText, "/").concat(unitLabel))));
};
export default Weighing;