import { State } from '../../model';
import { VariableState } from '../../communication';
import { ConditionalEnableElement } from './conditional-enable-element';
import { forEach } from 'lodash';
import { VariableUtil } from '../../utils/variable-util';
export class ReadableElement extends ConditionalEnableElement {
    constructor(element, permissionChecker, variableCommunicator, variableStore, localization, signalRAppId) {
        super(element, permissionChecker, variableCommunicator, variableStore, localization, signalRAppId);
    }
    init(variableName) {
        if (variableName) {
            let hasVariable = false;
            forEach(this.elementStates, elementState => {
                if (elementState.variableName === variableName) {
                    hasVariable = true;
                }
            });
            if (!hasVariable) {
                this.elementStates.push(new VariableState(variableName, undefined));
            }
        }
        this.initState();
    }
    get readVariableName() {
        if (!this.model) {
            throw new Error('Model cannot be null');
        }
        if (null == this.model.version || this.model.version < 2) {
            return this.model.variableName;
        }
        else {
            const variable = this.model.readVariable;
            if (!variable) {
                return undefined;
            }
            return VariableUtil.getConvertedVariableName(this.variableStore, variable);
        }
    }
    get minVariableName() {
        if (!this.model.minValueType) {
            return undefined;
        }
        return this.getVariableName(this.model.minVariable);
    }
    get minValueType() {
        return this.model.minValueType;
    }
    get maxVariableName() {
        if (!this.model.maxValueType) {
            return undefined;
        }
        return this.getVariableName(this.model.maxVariable);
    }
    get maxValueType() {
        return this.model.maxValueType;
    }
    getVariableName(variable) {
        if (!this.model) {
            throw new Error('Model cannot be null');
        }
        if (!variable) {
            return undefined;
        }
        return VariableUtil.getConvertedVariableName(this.variableStore, variable);
    }
    reportStateChanged(variableState) {
        forEach(this.elementStates, elementState => {
            if (elementState.variableName === variableState.variableName) {
                elementState.state = variableState.state;
                this.changeStates();
            }
        });
    }
    reportValueChanged(value) {
        forEach(this.elementStates, elementState => {
            if (elementState.variableName === value.variableName) {
                elementState.state = value.state;
                this.changeStates();
            }
        });
        if (this.state === State.Normal || this.state === State.Disable
            || value.variableName === this.minVariableName
            || value.variableName === this.maxVariableName) {
            this.updateVariableValue(value.value, value.variableName);
        }
    }
}
