"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const aws_sdk_1 = require("aws-sdk");
const util_1 = require("./util");
class CheckpointTable {
    constructor(options = {}) {
        this.dynamodb = options.dynamodb || new aws_sdk_1.DynamoDB();
        this.tableName = options.tableName || util_1.getEnv('CHECKPOINT_TABLE_NAME');
        this.keyName = options.keyName || util_1.getEnv('CHECKPOINT_TABLE_KEY_NAME');
    }
    async getLastCheckpoint() {
        const req = {
            TableName: this.tableName,
            Key: {
                [this.keyName]: { S: 'max_id' }
            }
        };
        const response = await this.dynamodb.getItem(req).promise();
        if (!response.Item) {
            console.error('no max_id stored in database yet');
            return undefined; // not found
        }
        return Number.parseInt(response.Item.max_id.N);
    }
    async checkpoint(id) {
        const req = {
            TableName: this.tableName,
            Item: {
                [this.keyName]: { S: 'max_id' },
                max_id: { N: id.toString() }
            }
        };
        await this.dynamodb.putItem(req).promise();
    }
}
exports.CheckpointTable = CheckpointTable;
