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 get forms(): Form<any, unknown>[] {
20 return [...this._forms];
23 addPerson(person: Person) {
24 if (person.relation == Relation.Dependent) {
25 throw new UnsupportedFeatureError('Dependents are not supported');
27 if (person.relation == Relation.Self || person.relation == Relation.Spouse) {
28 const others = this._people.filter(p => p.relation == person.relation);
29 if (others.length > 0) {
30 throw new InconsistencyError('Cannot have more than one Self or Spouse');
33 this._people.push(person);
36 getPerson(name: RegExp | string): Person {
37 const people = this._people.filter(p => p.name.search(name) !== -1);
38 if (people.length != 1) {
39 throw new Error(`Person ${name} not found or too imprecise`);
44 addForm(form: Form<any>) {
45 if (!form.supportsMultipleCopies) {
46 const other = this.findForms(form.constructor as FormClass<Form<any>>);
47 if (other.length > 0) {
48 throw new InconsistencyError(`Cannot have more than one type of form ${form.name}`);
52 this._forms.push(form);
55 findForm<T extends Form<any>>(cls: FormClass<T>): T | null {
56 const forms = this.findForms(cls);
57 if (forms.length == 0)
60 throw new InconsistencyError(`Form ${forms[0].name} has multiple copies`);
64 findForms<T extends Form<any>>(cls: FormClass<T>): T[] {
65 const forms: T[] = this._forms.filter((form: Form<any>): form is T => isFormT(form, cls));
69 getForm<T extends Form<any>>(cls: FormClass<T>): T {
70 const form = this.findForm(cls);
72 throw new NotFoundError(`No form ${cls.name}`);