import React from 'react';
import { locales } from '@pisell/utils';
import { PREFIX } from "../../index";
import "./index.less";
var UNIT_LABEL_MAP = {
  KILOGRAMS: 'kg',
  GRAMS: 'g',
  POUNDS: 'lb',
  OUNCES: 'oz'
};
var formatWeight = function formatWeight(value, unit) {
  var parsedValue = Number(value);
  var displayValue = Number.isFinite(parsedValue) ? parsedValue : 0;
  var displayUnit = UNIT_LABEL_MAP[unit || 'KILOGRAMS'];
  return "".concat(displayValue.toFixed(3), " ").concat(displayUnit);
};
var Weighing = function Weighing(props) {
  var dataSource = props.dataSource,
    symbol = props.symbol;
  if (!dataSource) {
    return null;
  }
  var mode = dataSource.mode,
    weight = dataSource.weight,
    tare = dataSource.tare,
    net_weight = dataSource.net_weight,
    unit_price = dataSource.unit_price,
    _dataSource$unit = dataSource.unit,
    unit = _dataSource$unit === void 0 ? 'KILOGRAMS' : _dataSource$unit;
  var unitLabel = UNIT_LABEL_MAP[unit];
  var tareValue = Number((tare === null || tare === void 0 ? void 0 : tare.value) || 0);
  var isManualWeight = mode === '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 === null || tare === void 0 ? void 0 : tare.value, (tare === null || tare === void 0 ? void 0 : tare.unit) || unit)))) : 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;