1 import Form, { FormClass, isFormT } from './Form';
2 import Person, { Relation } from './Person';
3 import { NotFoundError, InconsistencyError, UnsupportedFeatureError } from './Errors';
5 export default class TaxReturn {
8 private _people: Person[] = [];
9 private _forms: Form<any, unknown>[] = [];
11 constructor(year: number) {
19 addPerson(person: Person) {
20 if (person.relation == Relation.Dependent) {
21 throw new UnsupportedFeatureError('Dependents are not supported');
23 if (person.relation == Relation.Self || person.relation == Relation.Spouse) {
24 const others = this._people.filter(p => p.relation == person.relation);
25 if (others.length > 0) {
26 throw new InconsistencyError('Cannot have more than one Self or Spouse');
29 this._people.push(person);
32 getPerson(name: RegExp | string): Person {
33 const people = this._people.filter(p => p.name.search(name) !== -1);
34 if (people.length != 1) {
35 throw new Error(`Person ${name} not found or too imprecise`);
40 addForm(form: Form<any>) {
41 if (!form.supportsMultipleCopies) {
42 const other = this.findForms(form.constructor as FormClass<Form<any>>);
43 if (other.length > 0) {
44 throw new InconsistencyError(`Cannot have more than one type of form ${form.name}`);
48 this._forms.push(form);
51 findForm<T extends Form<any>>(cls: FormClass<T>): T | null {
52 const forms = this.findForms(cls);
53 if (forms.length == 0)
56 throw new InconsistencyError(`Form ${forms[0].name} has multiple copies`);
60 findForms<T extends Form<any>>(cls: FormClass<T>): T[] {
61 const forms: T[] = this._forms.filter((form: Form<any>): form is T => isFormT(form, cls));
65 getForm<T extends Form<any>>(cls: FormClass<T>): T {
66 const form = this.findForm(cls);
68 throw new NotFoundError(`No form ${cls.name}`);