import { Model } from 'front';
import { kebabToProperCase, smartfill } from '../../etc/util';

export const ACCOUNT_TYPE_INDIVIDUAL = 'individual';
export const ACCOUNT_TYPE_BUSINESS = 'business';

const ACCOUNT_TYPES = [
        ACCOUNT_TYPE_INDIVIDUAL,
        ACCOUNT_TYPE_BUSINESS
    ],
    accountTypes = [];

for (const type of ACCOUNT_TYPES) {
    accountTypes.push({
        title: kebabToProperCase(type),
        value: type
    });
}

/**\

    title: BankAccount
    category: model
    description: A model for bank accounts

\**/
class BankAccount extends Model {
    static accountTypes = accountTypes;

    // ABA routing number validation
    static validateRoutingNumber(number) {
        let sum = 0, i;

        if (!number || number.length < 9) {
            return false;
        }

        for (i = 0; i < number.length; i += 3) {
            sum += parseInt(number.charAt(i), 10) * 3 +
                parseInt(number.charAt(i + 1), 10) * 7 +
                parseInt(number.charAt(i + 2), 10);
        }

        return (sum && sum % 10 === 0);
    }

    constructor(data) {
        super({
            id: '',
            uuid: '',
            label: '',

            taxId: '',
            accountType: '',
            accountHolder: '',
            accountNumber: '',
            routingNumber: '',

            bankName: '',
            country: ''
        });

        this.fill(data);
    }

    fill(data) {
        smartfill(this, data);
    }
}

export default BankAccount;
