import { materializeFileData } from "./materialize-file-data.js";
import { updateFileDataCache } from "./cache/update-file-data-cache.js";
/**
 * Selects file data with caching support.
 *
 * First checks the cache, and if not found, materializes the file data
 * and updates the cache for future reads.
 *
 * @example
 * const data = selectFileData({
 *   engine: lix.engine!,
 *   file: { id: "file_123", path: "/test.json", metadata: null },
 *   versionId: "version_456"
 * });
 */
export function selectFileData(args) {
    // Check cache first
    const result = args.engine.sqlite.exec({
        sql: `
			SELECT data 
			FROM lix_internal_file_data_cache 
			WHERE file_id = ? 
			AND version_id = ?
		`,
        bind: [args.file.id, args.versionId],
        returnValue: "resultRows",
    });
    const cachedData = result[0]?.[0];
    if (cachedData) {
        // Cache hit!
        return cachedData;
    }
    // Cache miss - materialize the file data
    const data = materializeFileData({
        engine: args.engine,
        file: args.file,
        versionId: args.versionId,
    });
    // Update cache for next time (write-through)
    updateFileDataCache({
        engine: { sqlite: args.engine.sqlite },
        fileId: args.file.id,
        versionId: args.versionId,
        data,
    });
    return data;
}
//# sourceMappingURL=select-file-data.js.map