import { manage_schema_exports } from "../db/manage/manage-schema.js";
import { getTableConfig } from "drizzle-orm/pg-core";

//#region src/dolt/pk-map.ts
function buildPkMapFromSchema() {
	const pkMap = {};
	for (const value of Object.values(manage_schema_exports)) {
		if (value == null || typeof value !== "object") continue;
		let config;
		try {
			config = getTableConfig(value);
		} catch {
			continue;
		}
		if (!config?.name || !config.primaryKeys?.length) continue;
		const pkColumns = config.primaryKeys[0]?.columns.map((col) => col.name);
		if (pkColumns && pkColumns.length > 0) pkMap[config.name] = pkColumns;
	}
	return pkMap;
}
const managePkMap = buildPkMapFromSchema();
const isValidManageTable = (tableName) => {
	return tableName in managePkMap;
};
function buildTableMapFromSchema() {
	const tableMap = {};
	for (const value of Object.values(manage_schema_exports)) {
		if (value == null || typeof value !== "object") continue;
		let config;
		try {
			config = getTableConfig(value);
		} catch {
			continue;
		}
		if (!config?.name) continue;
		tableMap[config.name] = value;
	}
	return tableMap;
}
const manageTableMap = buildTableMapFromSchema();

//#endregion
export { isValidManageTable, managePkMap, manageTableMap };