/**
 * Copyright (c) 2020-present, Goldman Sachs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { Stack } from '@finos/legend-shared';
export class V1_ProcessingContext {
    inferredVariableList = [];
    tags = new Stack();
    constructor(firstTag) {
        this.tags.push(firstTag);
    }
    push(s) {
        this.tags.push(s);
        return this;
    }
    pop() {
        this.tags.pop();
        return this;
    }
    peek() {
        return this.tags.peek();
    }
    getStack() {
        return this.tags;
    }
    get lastInferredVariableList() {
        return this.inferredVariableList.length
            ? this.inferredVariableList[this.inferredVariableList.length - 1]
            : undefined;
    }
    addInferredVariables(name, variable) {
        if (!this.inferredVariableList.length) {
            this.addVariableToNewLevel(name, variable);
        }
        else {
            this.lastInferredVariableList?.set(name, variable);
        }
    }
    flushVariable(name) {
        this.lastInferredVariableList?.delete(name);
    }
    removeLastVariableLevel() {
        this.inferredVariableList.pop();
    }
    addVariableLevel() {
        this.inferredVariableList.push(new Map());
    }
    getInferredVariable(name) {
        return this.inferredVariableList
            .slice()
            .reverse()
            .find((e) => e.get(name))
            ?.get(name);
    }
    addVariableToNewLevel(name, variable) {
        const varIndex = new Map();
        varIndex.set(name, variable);
        this.inferredVariableList.push(varIndex);
    }
    clone() {
        const ctx = new V1_ProcessingContext('');
        ctx.tags = this.tags.clone();
        ctx.inferredVariableList = this.inferredVariableList.map((varMap) => {
            const varIndex = new Map();
            varMap.forEach((valueSpec, key) => {
                varIndex.set(key, valueSpec);
            });
            return varIndex;
        });
        return ctx;
    }
}
//# sourceMappingURL=V1_ProcessingContext.js.map