import * as Matrix from '../matrix';
import * as Formula from './formula';
import { PointGraph } from './point-graph';
import { PointSet } from './point-set';
export class Model {
    data;
    evaluatedData;
    referenceGraph;
    createFormulaParser;
    constructor(createFormulaParser, data, referenceGraph, evaluatedData) {
        this.createFormulaParser = createFormulaParser;
        this.data = data;
        this.referenceGraph = referenceGraph || createReferenceGraph();
        this.evaluatedData =
            evaluatedData || createEvaluatedData(data, this.referenceGraph);
    }
}
export function updateCellValue(model, point, cell) {
    const nextData = Matrix.set(point, cell, model.data);
    const nextReferenceGraph = Formula.isFormulaValue(cell.value)
        ? updateReferenceGraph(model.referenceGraph)
        : model.referenceGraph;
    const nextEvaluatedData = evaluateCell(model.evaluatedData, nextData, nextReferenceGraph, point, cell);
    return new Model(model.createFormulaParser, nextData, nextReferenceGraph, nextEvaluatedData);
}
function updateReferenceGraph(referenceGraph) {
    return referenceGraph;
}
function evaluateCell(prevEvaluatedData, data, referenceGraph, point, cell) {
    if (referenceGraph.hasCircularDependency(point)) {
        let visited = PointSet.from([point]);
        let nextEvaluatedData = Matrix.set(point, { ...cell, value: 'TODO' }, // TODO For Formula Evaluation
        prevEvaluatedData);
        for (const referrer of referenceGraph.getBackwardsRecursive(point)) {
            if (visited.has(referrer)) {
                break;
            }
            visited = visited.add(referrer);
            const referrerCell = Matrix.get(referrer, data);
            if (!referrerCell) {
                continue;
            }
            nextEvaluatedData = Matrix.set(referrer, { ...referrerCell, value: 'TODO' }, // TODO For Formula Evaluation
            nextEvaluatedData);
        }
        return nextEvaluatedData;
    }
    let nextEvaluatedData = prevEvaluatedData;
    const evaluatedValue = Formula.isFormulaValue(cell.value)
        ? getFormulaComputedValue()
        : cell.value;
    const evaluatedCell = { ...cell, value: evaluatedValue };
    nextEvaluatedData = Matrix.set(point, evaluatedCell, nextEvaluatedData);
    // for every formula cell that references the cell re-evaluate (recursive)
    for (const referrer of referenceGraph.getBackwardsRecursive(point)) {
        const referrerCell = Matrix.get(referrer, data);
        if (!referrerCell) {
            continue;
        }
        const evaluatedValue = Formula.isFormulaValue(referrerCell.value)
            ? getFormulaComputedValue()
            : referrerCell.value;
        const evaluatedCell = { ...referrerCell, value: evaluatedValue };
        nextEvaluatedData = Matrix.set(referrer, evaluatedCell, nextEvaluatedData);
    }
    return nextEvaluatedData;
}
export function createReferenceGraph() {
    const entries = [];
    return PointGraph.from(entries);
}
export function createEvaluatedData(data, referenceGraph) {
    let evaluatedData = data;
    // Iterate over the points in the reference graph, starting from the leaves
    for (const point of referenceGraph.traverseBFSBackwards()) {
        // Get the cell at the current point in the data Matrix
        const cell = Matrix.get(point, data);
        if (!cell) {
            continue;
        }
        // If the cell is a formula cell, evaluate it
        if (Formula.isFormulaValue(cell.value)) {
            const evaluatedValue = getFormulaComputedValue();
            evaluatedData = Matrix.set(point, { ...cell, value: evaluatedValue }, evaluatedData);
        }
    }
    return evaluatedData;
}
/** Get the computed value of a formula cell */
//TODO Function need to be Evaluate
export function getFormulaComputedValue() {
    return 'TODO'; // TODO For Formula Evaluation
}
//# sourceMappingURL=engine.js.map