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

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

const ComputedDao = store => {
  extendObservable(store, {
    get computedDao() {
      const { dao, payments, invoices, settings } = store;
      const t = dao ? dao.slice() : [];
      //const p = payments ? payments.slice() : [];
      //const i = invoices ? invoices.slice() : [];
      const all = [].concat(t);
      all.sort((a, b) => b.date.getTime() - a.date.getTime());
      all.forEach((t, i) => {
        t.key = String(i);
        t.idName = t.type === 'algorand' ? 'Algorand' : 'Ethereum';
        //t.icon = '../asset/icon/'+t.type+'.png';
        t.icon = '../asset/icon/lightning.svg';
        t.typeLabel = toCaps(t.type);
        t.statusLabel = toCaps(t.status);
        t.merchantLabel = t.title;
        t.dateLabel = t.date.toLocaleDateString();
        t.dateTimeLabel = t.date.toLocaleString();
        t.amountLabel = toAmountLabel(t.amount, settings);
        t.raised = toAmountLabel(0, settings);
        if (Number.isInteger(t.confirmations)) {
          t.confirmationsLabel = t.confirmations.toString();
        }
      });
      return all.slice(0, 100);
    },
    get poolStatus() {
      const {
        poolBalanceOpenSatoshis: opened,
        poolBalanceInactiveSatoshis: inactive,
        poolBalancePendingSatoshis: pending,
      } = store;
      return opened
        ? 'success'
        : inactive
        ? 'inactive'
        : pending
        ? 'info'
        : 'error';
    },
    get poolBalanceOpenSatoshis() {
      return (store.channels || [])
        .filter(c => c.active)
        .map(c => c.localBalance + c.commitFee)
        .reduce((a, b) => a + b, 0);
    },
    get poolBalanceOpenLabel() {
      const { channelBalanceOpenSatoshis, settings } = store;
      return toAmountLabel(channelBalanceOpenSatoshis, settings);
    },
    get poolBalancePendingLabel() {
      const { channelBalanceOpenSatoshis, settings } = store;
      return toAmountLabel(channelBalanceOpenSatoshis, settings);
    },
    get poolBalanceInactiveLabel() {
      const { channelBalanceOpenSatoshis, settings } = store;
      return toAmountLabel(channelBalanceOpenSatoshis, settings);
    },
    get poolBalanceClosingLabel() {
      const { channelBalanceOpenSatoshis, settings } = store;
      return toAmountLabel(channelBalanceOpenSatoshis, settings);
    },
  });
};

export default ComputedDao;
