type ColDef = { __type: "column"; id: string | string[]; key: string; name: string | string[]; type: string; operators: string[]; parents?: string[]; }; export function $$columnFactory( roots: Record, tables: Record, ) { return (id: string) => { if (!id) return null; if (id.startsWith("column|")) { id = id.slice(7); } const [rootTable, ...rootParts] = id.split("|"); const parts = [...rootParts]; if (parts.length === 0) { if (rootTable.includes(":")) { return roots[rootTable] ?? null; } return null; } if (parts.length === 1) return roots[`${rootTable}:${parts[0]}`] ?? null; const name: string[] = []; const lastColumn = parts.pop()!; const lastJoin = parts.pop()!; const [columnId, tableId] = lastJoin.split(":"); const key = `${tableId}:${lastColumn}`; const baseCol = roots[key]; if (!baseCol) return null; const tableName = tables[tableId]; if (!tableName) return null; // Build the join chain from root to target let currentTable = rootTable; const joinPath: Array<{ columnId: string; tableId: string; tableName: string; }> = []; // Process all joins in reverse order (from root to target) const allJoins = [...parts.reverse(), `${columnId}:${tableId}`]; for (const join of allJoins) { const [col, table] = join.split(":"); const joinCol = roots[`${currentTable}:${col}`]; if (!joinCol) return null; const joinTableName = tables[table]; if (!joinTableName) return null; joinPath.push({ columnId: col, tableId: table, tableName: joinTableName, }); currentTable = table; } // Build name from join path for (const join of joinPath) { const joinCol = roots[ `${joinPath[joinPath.indexOf(join)] === joinPath[0] ? rootTable : joinPath[joinPath.indexOf(join) - 1].tableId}:${join.columnId}` ]; if (joinCol) { name.push(`${joinCol.name}:${join.tableName}`); } } name.push(baseCol.name as string); const parents: string[] = []; return { __type: "column", id: rootParts, name: name, key: id, type: baseCol.type, operators: baseCol.operators, parents, } as ColDef; }; }