"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LineageVisitor = void 0;
const insert_query_visitor_1 = require("./insert-query.visitor");
class LineageVisitor extends insert_query_visitor_1.InsertQueryVisitor {
    constructor() {
        super();
        this.tables = [];
        this.edges = [];
    }
    defaultResult() {
        return;
    }
    aggregateResult(_aggregate, _nextResult) {
        return;
    }
    onColumnReference(tableId, columnId) {
        if (this.currentQueryRelation) {
            this.edges.push({
                type: 'edge',
                edgeType: this.currentQueryRelation.currentClause,
                source: {
                    tableId: tableId,
                    columnId: columnId
                },
                target: {
                    tableId: this.currentQueryRelation.id,
                    columnId: this.currentQueryRelation.currentColumnId
                }
            });
        }
    }
    onRelation(relation, alias) {
        let label = alias ?? relation.id;
        const columns = relation.columns.map(c => {
            return {
                id: c.id,
                label: c.label,
                range: c.range,
                data: c.data,
                isAssumed: c.isAssumed
            };
        });
        let tablePrimary;
        if (relation instanceof insert_query_visitor_1.TableRelation) {
            label =
                alias !== undefined && alias != relation.tablePrimary.tableName
                    ? `${relation.tablePrimary.tableName} -> ${alias}`
                    : relation.tablePrimary.tableName;
            tablePrimary = relation.tablePrimary;
        }
        this.tables.push({
            tablePrimary,
            table: {
                type: 'table',
                id: relation.id,
                label,
                range: relation.range,
                data: relation instanceof insert_query_visitor_1.TableRelation ? relation.data : undefined,
                isSourceOnly: relation instanceof insert_query_visitor_1.TableRelation ? true : undefined,
                columns
            }
        });
    }
}
exports.LineageVisitor = LineageVisitor;
//# sourceMappingURL=lineage.visitor.js.map