import React, { useMemo } from 'react';
import { locales } from '@pisell/utils';
import "./index.less";
import { useStore } from "../../hooks";
import Decimal from 'decimal.js';
var TitleExtra = function TitleExtra(props) {
  var _ref = props || {},
    values = _ref.values;
  var symbol = useStore(function (s) {
    return s.symbol;
  });
  var transformText = function transformText(textKey, tag, value) {
    var text = locales.getText(textKey);
    return text.replace("{{".concat(tag, "}}"), value);
  };
  var numText = useMemo(function () {
    var quantity = 0;
    if (Array.isArray(values)) {
      quantity = values.reduce(function (sum, i) {
        return sum + ((i === null || i === void 0 ? void 0 : i.quantity) || 0);
      }, 0) || 0;
    } else {
      quantity = (values === null || values === void 0 ? void 0 : values.quantity) || 0;
    }
    return transformText('pisell2.skuOptionsSelection.titleExtra.alreadySelected', 'num', quantity);
  }, [props === null || props === void 0 ? void 0 : props.values]);
  var priceText = useMemo(function () {
    var price = 0;
    if (Array.isArray(values)) {
      price = values === null || values === void 0 ? void 0 : values.reduce(function (sum, i) {
        return sum.plus((i === null || i === void 0 ? void 0 : i.totalPrice) || 0);
      }, new Decimal(0)).toNumber();
    } else {
      price = Number((values === null || values === void 0 ? void 0 : values.totalPrice) || 0);
    }
    return transformText('pisell2.skuOptionsSelection.titleExtra.totalPrice', 'price', "".concat(price >= 0 ? '' : '-').concat(symbol).concat(Math.abs(price)));
  }, [values, symbol]);
  return /*#__PURE__*/React.createElement("div", {
    className: "sku-options-selection-title-extra"
  }, /*#__PURE__*/React.createElement("span", {
    className: "sku-options-selection-title-extra-price"
  }, priceText), /*#__PURE__*/React.createElement("span", {
    className: "sku-options-selection-title-extra-num"
  }, numText));
};
export default TitleExtra;