"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.queryPaymentItemsByAssetSymbol = exports.queryPaymentItemsByStatus = exports.getPaymentItemById = exports.createPaymentItem = void 0;
const utils_1 = require("../utils");
const createPaymentItem = async (payment) => {
    const itemWithGenerated = {
        ...payment,
    };
    const itemWithComposite = {
        ...itemWithGenerated,
    };
    const paymentItem = itemWithComposite;
    const item = {
        TableName: utils_1.getEnvParam('PAYMENT_TABLE_NAME'),
        Item: {
            info: paymentItem,
        },
    };
    const putOutput = await utils_1.getDynamoDbDocumentClient().query(item).promise();
    return paymentItem;
};
exports.createPaymentItem = createPaymentItem;
const getPaymentItemById = async (id) => {
    var _a;
    const getItemInput = {
        TableName: utils_1.getEnvParam('PAYMENT_TABLE_NAME'),
        Key: { id },
    };
    const getItemOutput = await utils_1.getDynamoDbDocumentClient().get(getItemInput).promise();
    const item = (_a = getItemOutput === null || getItemOutput === void 0 ? void 0 : getItemOutput.Item) === null || _a === void 0 ? void 0 : _a.info;
    if (item) {
        return item;
    }
    else {
        throw new Error('');
    }
};
exports.getPaymentItemById = getPaymentItemById;
const queryPaymentItemsByStatus = async (status) => {
    const queryInput = {
        TableName: utils_1.getEnvParam('PAYMENT_TABLE_NAME'),
        IndexName: 'by-status-index',
        KeyConditionExpression: 'status = :cStatus',
        ExpressionAttributeValues: {
            ':cStatus': status,
        },
    };
    const queryOutput = await utils_1.getDynamoDbDocumentClient().query(queryInput).promise();
    if (Array.isArray(queryOutput.Items)) {
        return queryOutput.Items.map((item) => item.info);
    }
    else {
        throw new Error('');
    }
};
exports.queryPaymentItemsByStatus = queryPaymentItemsByStatus;
const queryPaymentItemsByAssetSymbol = async (assetSymbol) => {
    const queryInput = {
        TableName: utils_1.getEnvParam('PAYMENT_TABLE_NAME'),
        IndexName: 'by-assetSymbol-index',
        KeyConditionExpression: 'assetSymbol = :cAssetSymbol',
        ExpressionAttributeValues: {
            ':cAssetSymbol': assetSymbol,
        },
    };
    const queryOutput = await utils_1.getDynamoDbDocumentClient().query(queryInput).promise();
    if (Array.isArray(queryOutput.Items)) {
        return queryOutput.Items.map((item) => item.info);
    }
    else {
        throw new Error('');
    }
};
exports.queryPaymentItemsByAssetSymbol = queryPaymentItemsByAssetSymbol;
