Remove id from Line constructor.
[ustaxlib.git] / src / Form.ts
1 import TaxReturn from './TaxReturn';
2 import { Line } from './Line';
3 import { InconsistencyError, NotFoundError } from './Errors';
4
5 export default abstract class Form<L extends { [key: string]: Line<any> },
6 I = unknown> {
7 abstract readonly name: string;
8
9 protected abstract readonly _lines: L;
10
11 readonly supportsMultipleCopies: boolean = false;
12
13 private readonly _input?: I;
14
15 constructor(input?: I) {
16 this._input = input;
17 }
18
19 init() {
20 for (const id in this._lines) {
21 let l = this._lines[id];
22 l._id = id;
23 l.form = this;
24 }
25 }
26
27 getLine<K extends keyof L>(id: K): L[K] {
28 if (!(id in this._lines))
29 throw new NotFoundError(`Form ${this.name} does not have line ${id}`);
30 return this._lines[id];
31 }
32
33 getValue<K extends keyof L>(tr: TaxReturn, id: K): ReturnType<L[K]['value']> {
34 const line: L[K] = this.getLine(id);
35 return line.value(tr);
36 }
37
38 getInput<K extends keyof I>(name: K): I[K] {
39 if (!(name in this._input)) {
40 throw new NotFoundError(`No input with key ${name} on form ${this.name}`);
41 }
42 return this._input[name];
43 }
44 };