const enabledFeatures = {};

const enabledAbilities = {};

class FeatureManager {
    init = false;

    evt = {};

    onChange(name, evt) {
        if(!this.evt[name]) {
            this.evt[name] = [];
        }
        this.evt[name].push(evt);
    }

    offChange(name) {
        delete this.evt[name];
    }

    triggerChange() {
        for(let name in this.evt) {
            if(this.evt.hasOwnProperty(name)) {
                this.evt[name].forEach((evt) => {
                    evt(this);
                });
            }
        }
    }

    enableFeature(feature) {
        if(feature) {
            enabledFeatures[feature] = true;
        }
        this.triggerChange();
    }

    enableAbility(feature, name) {
        if(feature && name) {
            if(enabledFeatures[feature]) {
                if(!enabledAbilities[feature]) {
                    enabledAbilities[feature] = {};
                }

                enabledAbilities[feature][name] = true;
                this.triggerChange();
            }
        }
    }

    isEnabled = (feature) => {
        return enabledFeatures[feature];
    };

    isAbilityEnabled = (feature, name) => {
        if(enabledFeatures[feature] && enabledAbilities[feature]) {
            return enabledAbilities[feature][name];
        }

        return false
    };
}

const featureManager = new FeatureManager();

export default featureManager;