import { Model } from 'front';
import ManifestItem from './manifest-item.js';

/**\

    title: Manifest
    category: model
    description:
        A pretty sweet model representing an
        order manifest

\**/
class Manifest extends Model {
    constructor(data) {
        super({
            id: '',
            items: [ ManifestItem ]
        });

        this.fill(data);
    }

    subtotal() {
        let amount = 0,
            ni;

        for (ni = 0; ni < this.items.length; ni++) {
            amount += this.items[ni].total();
        }

        return amount;
    }

    tax(amount = 0) {
        return this.subtotal() * amount;
    }

    total(tax = 0) {
        return this.subtotal() + this.tax(tax);
    }

    asManifest() {
        let out = '',
            ni;

        for (ni = 0; ni < this.items.length; ni++) {
            out += this.items[ni].asLine() + '\n';
        }

        return out.trim('\n');
    }
}

export default Manifest;
