import { ConditionItemsResultObserver } from '../../model';
import { BaseElement } from './base-element';
export class ConditionalDisplayElement extends BaseElement {
    constructor(element, permissionChecker, variableCommunicator, variableStore, signalRAppId) {
        super(element, signalRAppId);
        this.permissionChecker = permissionChecker;
        this.variableCommunicator = variableCommunicator;
        this.variableStore = variableStore;
        this.initDisplay();
    }
    initDisplay() {
        if (false !== this.model.isAwaysDisplay) {
            return;
        }
        // If this element is conditional display, hide this element by default
        this.hide();
        if (this.model.version == null || this.model.version < 3) {
            this.updateVisibilityByCondition();
        }
        else {
            const isDisplayControl = this.model.isDisplayControl || this.model.conditionItems.length > 0;
            if (this.model.isDisplayPermission && !isDisplayControl) {
                this.updateVisibilityByPermission();
            }
            if (!this.model.isDisplayPermission && isDisplayControl) {
                this.updateVisibilityByCondition();
            }
            if (this.model.isDisplayPermission && isDisplayControl) {
                this.checkDisplayConditionAndPermission();
            }
        }
    }
    checkDisplayConditionAndPermission() {
        if (null == this.model.conditionItems || !this.model.conditionItems.length) {
            return;
        }
        const conditionResult = new ConditionItemsResultObserver(this.model.conditionItems, this.variableCommunicator, this.variableStore);
        conditionResult.conditionResultChanged.subscribe(result => {
            if (result.value) {
                this.updateVisibilityByPermission();
            }
            else {
                this.hide();
            }
        });
        conditionResult.requestData(this.signalRAppId);
    }
    updateVisibilityByCondition() {
        if (null == this.model.conditionItems || !this.model.conditionItems.length) {
            return;
        }
        const conditionResult = new ConditionItemsResultObserver(this.model.conditionItems, this.variableCommunicator, this.variableStore);
        conditionResult.conditionResultChanged.subscribe(result => {
            if (result.value) {
                this.show();
            }
            else {
                this.hide();
            }
        });
        conditionResult.requestData(this.signalRAppId);
    }
    updateVisibilityByPermission() {
        if (!this.model.displayPermissionCodes || !this.model.displayPermissionCodes.length) {
            return;
        }
        if (this.permissionChecker.isGranted(this.model.displayPermissionCodes)) {
            this.show();
        }
        else {
            this.hide();
        }
    }
    /**
     * Hide this element
     */
    hide() {
        this.rootElement.style('display', 'none');
    }
    /**
     * Show this element
     */
    show() {
        this.rootElement.style('display', 'block');
    }
}
