"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProviderData = getProviderData;
const db_1 = require("../../lib/db");
const sql_1 = require("./sql");
async function getProviderData(consumes, originId) {
    if (!consumes.length || !originId)
        return {};
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.GET_PROVIDER_DATA, [originId, consumes]);
    const result = {};
    for (const row of rows) {
        // Keep first (most recent) per workflow type
        if (result[row.workflow_type])
            continue;
        let parsed = {};
        try {
            parsed = row.data ? JSON.parse(row.data) : {};
        }
        catch {
            parsed = {};
        }
        result[row.workflow_type] = {
            data: parsed,
            completedAt: row.completed_at?.toISOString() || '',
            workflowType: row.workflow_type,
        };
    }
    return result;
}
