import { ConditionItemsResultObserver } from '../../model';
import { StateControlElement } from './state-control-element';
export class ConditionalEnableElement extends StateControlElement {
    constructor(element, permissionChecker, variableCommunicator, variableStore, localization, signalRAppId) {
        super(element, permissionChecker, variableCommunicator, variableStore, localization, signalRAppId);
        this.permissionChecker = permissionChecker;
        this.variableCommunicator = variableCommunicator;
        this.variableStore = variableStore;
        this.localization = localization;
        this.isGrantedPermission = false;
        this.isInitialized = false; // 逻辑控制专用
        setTimeout(() => {
            this.initialize();
        });
    }
    initialize() {
        if (false !== this.model.isAwaysEnable) {
            this.isInitialized = true;
            return;
        }
        if (this.model.version == null || this.model.version < 3) {
            this.updateEnableStateByCondition();
        }
        else {
            if (this.model.isEnablePermission || this.model.isEnablePermission === undefined) {
                if (this.model.enablePermissionCodes) {
                    this.isGrantedPermission = this.permissionChecker.isGranted(this.model.enablePermissionCodes);
                }
                else {
                    this.isGrantedPermission = true;
                }
            }
            else {
                this.isGrantedPermission = true;
            }
            if (this.model.isEnablePermission && !this.model.isEnableControl) {
                this.updateEnableStateByPermission();
            }
            if (!this.model.isEnablePermission && this.model.isEnableControl) {
                this.updateEnableStateByCondition();
            }
            if (this.model.isEnablePermission && this.model.isEnableControl) {
                this.updateEnableStateByConditionAndPermission();
            }
            if (!this.model.isEnablePermission && !this.model.isEnableControl) {
                this.updateEnableStateByConditionAndPermission();
            }
        }
        if (!this.isGrantedPermission) {
            this.isInitialized = true;
            this.isEnable = false;
            this.changeStates();
        }
    }
    updateEnableStateByConditionAndPermission() {
        if (null == this.model.enableItems || !this.model.enableItems.length) {
            this.isInitialized = true;
            return;
        }
        const enableConditionResult = new ConditionItemsResultObserver(this.model.enableItems, this.variableCommunicator, this.variableStore);
        enableConditionResult.conditionResultChanged.subscribe(result => {
            if (result.value) {
                this.updateEnableStateByPermission();
            }
            else {
                this.setEnableByConditionOrPermission(false);
            }
            this.isInitialized = true;
            this.changeStates();
        });
        enableConditionResult.requestData(this.signalRAppId);
    }
    updateEnableStateByCondition() {
        if (null == this.model.enableItems || !this.model.enableItems.length) {
            this.isInitialized = true;
            return;
        }
        const enableConditionResult = new ConditionItemsResultObserver(this.model.enableItems, this.variableCommunicator, this.variableStore);
        enableConditionResult.conditionResultChanged.subscribe(result => {
            this.setEnableByConditionOrPermission(result.value);
            this.isInitialized = true;
            this.changeStates();
        });
        enableConditionResult.requestData(this.signalRAppId);
    }
    updateEnableStateByPermission() {
        let isEnable = false;
        if (this.model.enablePermissionCodes) {
            isEnable = this.permissionChecker.isGranted(this.model.enablePermissionCodes);
        }
        else {
            isEnable = true;
        }
        this.setEnableByConditionOrPermission(isEnable);
        this.isInitialized = true;
    }
    setEnableByConditionOrPermission(isEnable) {
        this.isEnable = isEnable;
    }
}
