import { sql } from "kysely";
import { internalQueryBuilder } from "../../engine/internal-query-builder.js";
import { schemaKeyToCacheTableName } from "./create-schema-cache-table.js";
export const CACHE_COLUMNS = [
    "entity_id",
    "schema_key",
    "file_id",
    "version_id",
    "plugin_key",
    "snapshot_content",
    "schema_version",
    "created_at",
    "updated_at",
    "inherited_from_version_id",
    "is_tombstone",
    "change_id",
    "commit_id",
];
const ROUTED_ALIAS = "lix_internal_state_cache_routed";
/**
 * Returns a Kysely query builder routed to the materialized table for the
 * provided schema key. Pass `undefined` to opt into a no-op SELECT (useful when
 * the physical table might not exist yet).
 */
export function selectFromStateCache(schemaKey, columns = CACHE_COLUMNS) {
    const selectSql = schemaKey
        ? buildSelectStatement(schemaKeyToCacheTableName(schemaKey), columns)
        : buildEmptySelect(columns);
    const tableExpression = sql `(${sql.raw(selectSql)})`.as(ROUTED_ALIAS);
    return internalQueryBuilder.selectFrom(tableExpression);
}
function buildSelectStatement(tableName, columns) {
    const quoted = quoteIdentifier(tableName);
    const projection = columns.map(quoteIdentifier).join(",\n\t");
    return `SELECT
	${projection}
FROM ${quoted}`;
}
function buildEmptySelect(columns) {
    const projection = columns
        .map((column) => `CAST(NULL AS TEXT) AS ${quoteIdentifier(column)}`)
        .join(",\n\t");
    return `SELECT
	${projection}
WHERE 0`;
}
function quoteIdentifier(value) {
    return `"${value.replace(/"/g, '""')}"`;
}
//# sourceMappingURL=select-from-state-cache.js.map