import { ExpressionValue } from "../ExpressionValue";
import { StateManager } from "../../CommonUI/StateManagement/StateManager";
export class ExpressionEditContext {
    constructor(expression) {
        this.ownership = {};
        this.emptyExpressionText = null;
        this.expression = expression;
    }
    clearSelection() {
        this.selected = undefined;
        StateManager.propertyChanged(this, "selected");
    }
    setSelection(id) {
        this.selected = id;
        StateManager.propertyChanged(this, "selected");
        if (this.onSelection) {
            this.onSelection(this.selected);
        }
    }
    getSelection() {
        return this.selected;
    }
    register(owned, owner) {
        // console.log('owned')
        // console.log(owned)
        // console.log('owner')
        // console.log(owned)
        if (owned._id === owner._id)
            throw new Error("cannot set owner as owned");
        this.ownership[owned._id] = { owned: owned, owner: owner };
    }
    getParent(id) {
        var _a;
        return (_a = this.ownership[id]) === null || _a === void 0 ? void 0 : _a.owner;
    }
    getParents(id) {
        let parents = [];
        let parent = this.getParent(id);
        if (parent) {
            let type = Reflect.get(parent, "__type");
            if (type === "__RootExpression") {
                return parents;
            }
            parents.push(parent);
            return [...parents, ...this.getParents(parent._id)];
        }
        else {
            return parents;
        }
    }
    getElement(id) {
        var _a;
        return (_a = this.ownership[id]) === null || _a === void 0 ? void 0 : _a.owned;
    }
    canSelectParent(id) {
        let parent = this.getParent(id);
        while (parent != null) {
            if (parent.canSelect)
                return true;
            parent = this.getParent(parent._id);
        }
        return false;
    }
    selectParent(id) {
        let parent = this.getParent(id);
        while (parent != null) {
            if (parent.canSelect) {
                this.setSelection(parent._id);
            }
            parent = this.getParent(parent._id);
        }
    }
    refresh() {
        StateManager.changed(this);
        if (this.onExpressionUpdate) {
            this.onExpressionUpdate();
        }
    }
    getParentExpressionValue(id) {
        let parent = this.ownership[id];
        if (!parent) {
            // no parent
            return null;
        }
        if (ExpressionValue.isExpressionValue(parent.owner)) {
            return parent.owner;
        }
        return this.getParentExpressionValue(parent.owner._id);
    }
    checkVariableUsed(variable) {
        return !!Object.values(this.ownership).find((o) => {
            if (o.owned instanceof ExpressionValue && o.owned.variableName === variable.name) {
                return true;
            }
            if (o.owner instanceof ExpressionValue && o.owner.variableName === variable.name) {
                return true;
            }
            return false;
        });
    }
}
//# sourceMappingURL=ExpressionEditContext.js.map