import { formatAmount } from '@pisell/utils';
import { getServiceTime } from "./formatTime";
import { locales } from '@pisell/utils';
var UNIT_LABEL = {
  KILOGRAMS: 'kg',
  GRAMS: 'g',
  POUNDS: 'lb',
  OUNCES: 'oz'
};
var DEFAULT_WEIGHT_PRECISION = 3;
var formatWeightValue = function formatWeightValue(value) {
  var parsedValue = Number(value);
  var displayValue = Number.isFinite(parsedValue) ? parsedValue : 0;
  return displayValue.toFixed(DEFAULT_WEIGHT_PRECISION);
};
var formatProductMessage = function formatProductMessage(product, utils) {
  var _product$_origin;
  var product_option_string = product.product_option_string,
    options = product.options,
    bundle = product.bundle,
    currency_symbol = product.currency_symbol,
    start_date = product.start_date,
    end_date = product.end_date,
    holder_id = product.holder_id,
    holders = product.holders,
    multiResources = product.multiResources;
  var str = '';
  if (start_date && end_date) {
    str += "".concat(getServiceTime(product, start_date, utils), " \n");
  }
  if (holder_id && Array.isArray(holders)) {
    var targetHolder = holders.find(function (holder) {
      return holder.id === holder_id;
    });
    if (targetHolder !== null && targetHolder !== void 0 && targetHolder.label) {
      str += "".concat(targetHolder.label, " \n");
    }
  }
  if (Array.isArray(multiResources) && multiResources.length > 0) {
    multiResources.forEach(function (resource) {
      var ids = resource.id,
        lists = resource.lists;
      if (Array.isArray(ids)) {
        ids.forEach(function (id) {
          var targetRes = lists.find(function (l) {
            return l.id === id;
          });
          if (targetRes !== null && targetRes !== void 0 && targetRes.label) str += "".concat(targetRes === null || targetRes === void 0 ? void 0 : targetRes.label, " \n");
        });
      } else {
        var targetRes = lists.find(function (l) {
          return l.id === ids;
        });
        if (targetRes !== null && targetRes !== void 0 && targetRes.label) str += "".concat(targetRes === null || targetRes === void 0 ? void 0 : targetRes.label, " \n");
      }
    });
  }

  // 重量信息
  var weighing = product === null || product === void 0 || (_product$_origin = product._origin) === null || _product$_origin === void 0 || (_product$_origin = _product$_origin._extend) === null || _product$_origin === void 0 ? void 0 : _product$_origin.weighing;
  if (weighing) {
    var mode = weighing.mode,
      unit_price = weighing.unit_price,
      unit = weighing.unit,
      weight = weighing.weight,
      tare = weighing.tare,
      net_weight = weighing.net_weight;
    var unitLabel = UNIT_LABEL[unit] || unit;
    var weightText = formatWeightValue(weight);
    var tareText = formatWeightValue(tare === null || tare === void 0 ? void 0 : tare.value);
    var netWeightText = formatWeightValue(net_weight);
    var tareValue = Number((tare === null || tare === void 0 ? void 0 : tare.value) || 0);
    var isManualWeight = mode === 'manual';
    var unitPriceText = "".concat(currency_symbol || '').concat(unit_price !== null && unit_price !== void 0 ? unit_price : '0.00');
    if (isManualWeight) {
      str += "".concat(locales.getText('pisell2.cart.sku-card.weighing.manual'), " \n");
    }
    if (tareValue > 0) {
      str += "".concat(locales.getText('pisell2.cart.sku-card.weighing.gross'), ": ").concat(weightText, " ").concat(unitLabel, " \n");
    }
    if (tareValue > 0) {
      str += "".concat(locales.getText('pisell2.cart.sku-card.weighing.tare'), ": ").concat(tareText, " ").concat(UNIT_LABEL[tare === null || tare === void 0 ? void 0 : tare.unit] || (tare === null || tare === void 0 ? void 0 : tare.unit) || unitLabel, " \n");
    }
    str += "".concat(netWeightText, " ").concat(unitLabel, " ").concat(locales.getText('pisell2.cart.sku-card.weighing.net'), " @ ").concat(unitPriceText, "/").concat(unitLabel, " \n");
  }

  // 组合规格 单规格
  if (product_option_string) str += "".concat(product_option_string, "\n");
  if (Array.isArray(options) && options.length) {
    options.forEach(function (opt) {
      str += "".concat(opt.name, "  ").concat(formatAmount(opt === null || opt === void 0 ? void 0 : opt.price, 2, currency_symbol), " x ").concat(opt.num, "\n");
    });
  }

  // 套餐
  if (Array.isArray(bundle) && bundle.length) {
    bundle.forEach(function (item) {
      str += "".concat(item.title, "  ").concat(formatAmount(item.price, 2, currency_symbol), " x ").concat(item.num, "\n");
      var bundleOptions = item === null || item === void 0 ? void 0 : item.options;
      if (Array.isArray(bundleOptions) && bundleOptions.length) {
        bundleOptions.forEach(function (opt) {
          str += "  ".concat(opt === null || opt === void 0 ? void 0 : opt.name, "  ").concat(formatAmount(opt === null || opt === void 0 ? void 0 : opt.price, 2, currency_symbol), " x ").concat(opt === null || opt === void 0 ? void 0 : opt.num, "\n");
        });
      }
    });
  }
  return str;
};
export default formatProductMessage;