import Action from "./Action";
import RuleService from "./RuleService";

export default class ActionProbabilityMap {

    private map: Map<string, number>;

    constructor(map: Map<string, number>) {
        this.map = map
    }

    getProbability(action: Action) {
        const actionText = action.toString();
        return this.map.get(actionText);
    }

    getActions() {
        let actions = [];
        for (const key of this.map.keys()) {
            actions.push(Action.parseAction(key))
        }
    }

    equals(map2: Map<string, number>) {
        var testVal;
        if (!this.map || !map2) {
            return false;
        } 
        if (this.map.size !== map2.size) {
            return false;
        }
        for (var [key, val] of this.map) {
            testVal = map2.get(key);
            if (testVal !== val || (testVal === undefined && !map2.has(key))) {
                return false;
            }
        }
        return true;
    }


    getActionsForElement(ruleService : RuleService): Map<Element, Action[]> {
        let elementActionsMap = new Map();
        for (let [actionText, probability] of this.map) {
            const action = Action.parseAction(actionText);
            const elements = ruleService.actionToElements(action);
            elements.forEach((element) => {
                if (!elementActionsMap.has(element)) {
                    elementActionsMap.set(element, []);
                }
                let actions = elementActionsMap.get(element);
                action.probability = probability
                actions.push(action);
            })
        }
        return elementActionsMap;
    }

}