import { UserFormData } from "../UserFormData";
import { StateManager } from "../../CommonUI/StateManagement/StateManager";
export class RuntimeContext {
    constructor(formContext) {
        this.data = new UserFormData();
        this.context = formContext;
        this._hideControls = [];
        this._disabledControls = [];
    }
    get hideControls() {
        return this._hideControls;
    }
    set hideControls(hideControlIds) {
        const previousHides = [...this._hideControls];
        const showControls = previousHides.filter((hide) => hideControlIds.indexOf(hide) < 0);
        const newlyHideControls = hideControlIds.filter((hide) => previousHides.indexOf(hide) < 0);
        const updateControls = [...showControls, ...newlyHideControls];
        this._hideControls = hideControlIds;
        StateManager.propertiesChanged(this, updateControls);
    }
    get disabledControls() {
        return this._disabledControls;
    }
    set disabledControls(disabledControlIds) {
        const previousHides = [...this._disabledControls];
        const enabledControls = previousHides.filter((hide) => disabledControlIds.indexOf(hide) < 0);
        const newlyDisabledControls = disabledControlIds.filter((hide) => previousHides.indexOf(hide) < 0);
        const updateControls = [...enabledControls, ...newlyDisabledControls];
        this._disabledControls = disabledControlIds;
        StateManager.propertiesChanged(this, updateControls);
    }
    computeRuntimeValidations(fieldName = '') {
        var _a;
        let breaks;
        breaks = [];
        for (let c of this.context.form.getAllControls()) {
            let results = c.getRuntimeValidationIssues();
            if (results) {
                breaks.push(...results);
            }
        }
        // call validation provider if it exists for additional validation
        let valBreaks = (_a = this.runtimeValidationProvider) === null || _a === void 0 ? void 0 : _a.getRuntimeValidationIssues();
        if (valBreaks) {
            breaks.push(...valBreaks);
        }
        // todo: call built in rules
        if (fieldName) {
            const ruleBreaks = this.context.form.getRuleValidationIssues(fieldName);
            if (ruleBreaks) {
                breaks.push(...ruleBreaks);
            }
        }
        this.context.controlContexts.updateValidationIssues(breaks, this.context.form.getAllControls(), true);
        return breaks;
    }
    getControlContext(control) {
        return this.context.controlContexts.getControlRenderContext(control);
    }
    setFormDataValue(fieldName, value) {
        if (!this.data)
            this.data = new UserFormData();
        const currentValue = this.getFormDataValue(fieldName);
        if (value === currentValue)
            return;
        this.data[fieldName] = value;
        // this.refreshUserForm();
        if (this.context.onFormDataChange) {
            this.context.onFormDataChange(this.data);
        }
        this.computeRuntimeValidations(fieldName);
        this.context.form.dataChangeByRule(fieldName);
        this.context.form.hideControlByRule();
        this.context.form.disableControlByRule();
        this.context.form.focusControlByRule();
        StateManager.propertyChanged(this, fieldName);
    }
    getFormDataValue(fieldName) {
        if (!this.data)
            return;
        return this.data[fieldName];
    }
    setFormData(data) {
        this.data = data;
        this.computeRuntimeValidations();
    }
    getFormData() {
        return this.data;
    }
}
//# sourceMappingURL=RuntimeContext.js.map