import _ from 'lodash';

import { convertIntoDates } from '../helpers/column-desciptions.helper';
import { ECollaterals } from '../enums/collaterals.enum';

import { IUploadedBankTransaction } from '../models/UploadedBankTransaction.model';

export class BankUploadedTransactionItem implements IUploadedBankTransaction {

  date = new Date();
  accountNumber = '';
  amount = 0;
  transactionType = '';
  reference = '';
  detail1 = '';
  detail2 = '';
  detail3 = '';
  isConverted = false;
  splitTransactions = [];
  order: number;

  constructor(item: Partial<IUploadedBankTransaction>, options: { [extraOption: string]: string }) {
    if (options && options.bankAccountNumber && (!item.accountNumber || item.accountNumber.toString().trim() === '')) {
      item.accountNumber = options.bankAccountNumber;
    }
    const itemWithDates = convertIntoDates(item, ECollaterals.UPLOADED_BANK_TRANSACTIONS);
    const itemWithoutNulls = _.omitBy(itemWithDates, (value) => value === null);
    Object.assign(this, itemWithoutNulls);
    this.amount = item.amount ?? 0;
    if (!this.amount) {
      this.amount = item.credit - item.debit;
    }
  }

}
