import { buildFields } from './field-builder.js';
import { FormContextValue } from './FormContextValue.js';
import { FormRepeatableVariable } from './FormVariable.js';
export class JinjaFormModel {
    constructor(config) {
        Object.defineProperty(this, "config", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "fields", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "name", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "context", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.name = config.name;
        this.config = config;
        this.context = new FormContextValue(config.name, config.fields, config.computed, config.parent);
        this.fields = buildFields(this.context.variables);
    }
    get(name) {
        return this.context.findByKey(name)?.get();
    }
    async getResolved(name) {
        const variable = this.context.findByKey(name);
        return this.context.getResolvedVariable(variable);
    }
    set(name, value) {
        const variable = this.context.findByKey(name);
        variable?.set(value);
        variable?.patchState({ touched: true });
    }
    getAll() {
        return this.context.getAllInContext();
    }
    async getAllResolved() {
        const result = {};
        for (const key in this.context.variables) {
            result[key] = await this.getResolved(key);
        }
        return result;
    }
    setAll(value) {
        for (const key in value) {
            this.set(key, value[key]);
        }
    }
    onChange(name, listener) {
        const variable = this.context.findByKey(name);
        if (!variable)
            return () => { };
        const removeChangeValueListener = variable?.onChange(value => listener({ value, ...variable.getState() }));
        const removeChangeStateListener = variable?.onChangeState(state => listener({ value: variable.get(), ...state }));
        return () => {
            removeChangeStateListener();
            removeChangeValueListener();
        };
    }
    async submit() {
        const valid = await this.isValid();
        if (valid)
            this.config.onSubmit(this);
    }
    isValid() {
        return this.context.isValid();
    }
    reset(value) {
        if (value) {
            this.config.fields.forEach((field) => {
                field.default = value[field.name];
            });
        }
        for (const key in this.context.variables) {
            for (const variable of this.context.variables[key]) {
                variable.set(variable.config.default);
                variable.patchState({ error: undefined, touched: false });
            }
        }
    }
    isRepeating(name) {
        const variable = this.context.findByKey(name);
        return variable instanceof FormRepeatableVariable && variable.isRepeating();
    }
}
//# sourceMappingURL=JinjaFormModel.js.map