import { Table, TableCombination } from '@backend/domain'
import { removeNulls } from '@core/utils'
import { TableGraph } from '../services'
import { TableCombinationResponse, TableGraphResponse, TableResponse } from '../types'

export class TableMapper {
	static entityToResponse(table: Table): TableResponse {
		return {
			id: table.id,
			name: table.name,
			capacity: table.capacity,
			width: table.width,
			height: table.height,
			columnStart: table.columnStart,
			rowStart: table.rowStart,
			shape: table.shape,
			priority: table.priority,
			subscribers: table.subscribers?.map((subscriber) => subscriber.user?.id).filter(removeNulls),
		}
	}
}

export class TableCombinationMapper {
	static entityToResponse(combination: TableCombination): TableCombinationResponse {
		return {
			id: combination.id,
			name: combination.name,
			tableIds: combination.tables?.map((table) => table.id),
		}
	}
}

export class TableGraphMapper {
	static entityToResponse(graph: TableGraph): TableGraphResponse {
		return {
			edges: [...graph.edges.entries()].reduce(
				(acc, [from, to]) => {
					const toNodes = [...to.values()].map((toNode) => toNode.id)

					acc[from.id] = toNodes

					return acc
				},
				{} as { [key: string]: string[] },
			),
		}
	}
}
