import { GraphQLError } from 'graphql';
import { MiscExpensesProvider } from '../../../../misc-expenses/providers/misc-expenses.provider.js';
import { storeInitialGeneratedRecords } from '../../../helpers/ledgrer-storage.helper.js';
import { generateMiscExpensesLedger } from '../../../helpers/misc-expenses-ledger.helper.js';
import { ledgerProtoToRecordsConverter } from '../../../helpers/utils.helper.js';
export const generateLedgerRecordsForBalance = async (charge, { insertLedgerRecordsIfNotExists }, { injector }) => {
    try {
        // validate balance record exists
        const miscExpenses = await injector
            .get(MiscExpensesProvider)
            .getExpensesByChargeIdLoader.load(charge.id);
        if (!miscExpenses?.length) {
            throw new GraphQLError('Balance charge must include balance records');
        }
        const ledgerEntries = [];
        // generate ledger from misc expenses
        const expensesLedgerPromise = generateMiscExpensesLedger(charge, injector).then(entries => {
            entries.map(entry => {
                entry.ownerId = charge.owner_id;
                ledgerEntries.push(entry);
            });
        });
        await Promise.all([expensesLedgerPromise]);
        if (insertLedgerRecordsIfNotExists) {
            await storeInitialGeneratedRecords(charge.id, ledgerEntries, injector);
        }
        return {
            records: ledgerProtoToRecordsConverter(ledgerEntries),
            charge,
            balance: {
                isBalanced: true,
                unbalancedEntities: [],
                balanceSum: 0,
                financialEntities: [],
            },
            errors: [],
        };
    }
    catch (e) {
        return {
            __typename: 'CommonError',
            message: `Failed to generate ledger records for charge ID="${charge.id}"\n${e}`,
        };
    }
};
//# sourceMappingURL=balance-ledger-generation.resolver.js.map