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;
  if (!dataSource) {
    return null;
  }
  var weight = dataSource.weight,
    tare = dataSource.tare,
    net_weight = dataSource.net_weight,
    _dataSource$unit = dataSource.unit,
    unit = _dataSource$unit === void 0 ? 'KILOGRAMS' : _dataSource$unit;
  var tareValue = Number((tare === null || tare === void 0 ? void 0 : tare.value) || 0);
  var tareLabel = (tare === null || tare === void 0 ? void 0 : tare.label) || locales.getText('pisell2.cart.sku-card.weighing.tare');
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__weighing")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__weighing-row")
  }, /*#__PURE__*/React.createElement("span", null, locales.getText('pisell2.cart.sku-card.weighing.gross'), ":"), /*#__PURE__*/React.createElement("span", null, formatWeight(weight, unit))), tareValue > 0 ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__weighing-row")
  }, /*#__PURE__*/React.createElement("span", null, tareLabel, ":"), /*#__PURE__*/React.createElement("span", null, "-", formatWeight(tareValue, (tare === null || tare === void 0 ? void 0 : tare.unit) || unit))) : null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(PREFIX, "__weighing-row")
  }, /*#__PURE__*/React.createElement("span", null, locales.getText('pisell2.cart.sku-card.weighing.net'), ":"), /*#__PURE__*/React.createElement("span", null, formatWeight(net_weight, unit))));
};
export default Weighing;