/**
 * @fileOverview computed values that are used in transaction UI components.
 */

import { extendObservable } from 'mobx';
import { toAmountLabel, toCaps } from '../helper';

const ComputedPromotion = store => {
  extendObservable(store, {
    get computedPromotions() {
      const { promotions, settings } = store;
      const o = promotions ? promotions.slice() : [];
      //const p = payments ? payments.slice() : [];
      //const i = invoices ? invoices.slice() : [];
      const all = [].concat(o);
      all.sort((a, b) => b.date.getTime() - a.date.getTime());
      all.forEach((t, i) => {
        t.key = String(i);
        t.idName = t.type === 'BITS' ? 'Transaction ID' : 'Invoice ID';
        t.typeLabel = toCaps(t.type);
        //t.statusLabel = toCaps(t.status);
        t.dateLabel = t.date.toLocaleDateString();
        t.dateTimeLabel = t.date.toLocaleString();
        t.amountLabel = toAmountLabel(t.amount, settings);
        t.feeLabel = '100';
        if (Number.isInteger(t.rewarded)) {
          t.rewardedLabel = t.rewarded.toString();
        }
        if (t.type === 'BITS') {
          t.status = store.balanceSatoshis > t.hodl ? 'ready' : 'waiting';
          t.statusLabel = toCaps(t.status);
        }
      });
      return all.slice(0, 100);
    },
  });
};

export default ComputedPromotion;
