import { GraphQLError } from 'graphql';
import { getChargeBusinesses, getChargeTaxCategoryId, } from '../../charges/helpers/common.helper.js';
import { TransactionsProvider } from '../../transactions/providers/transactions.provider.js';
import { BusinessesProvider } from '../providers/businesses.provider.js';
import { FinancialEntitiesProvider } from '../providers/financial-entities.provider.js';
import { TaxCategoriesProvider } from '../providers/tax-categories.provider.js';
export const commonFinancialEntityFields = {
    id: dbFinancialEntity => dbFinancialEntity.id,
    ownerId: dbFinancialEntity => dbFinancialEntity.owner_id,
    name: dbFinancialEntity => dbFinancialEntity.name,
    irsCode: dbFinancialEntity => dbFinancialEntity.irs_code,
    isActive: dbFinancialEntity => !!dbFinancialEntity.is_active,
    createdAt: dbFinancialEntity => dbFinancialEntity.created_at,
    updatedAt: dbFinancialEntity => dbFinancialEntity.updated_at,
};
export const commonTaxChargeFields = {
    taxCategory: async (DbCharge, _, { injector }) => {
        const taxCategoryId = await getChargeTaxCategoryId(DbCharge.id, injector);
        if (!taxCategoryId) {
            return null;
        }
        return injector
            .get(TaxCategoriesProvider)
            .taxCategoryByIdLoader.load(taxCategoryId)
            .then(taxCategory => taxCategory ?? null);
    },
};
export const commonChargeFields = {
    counterparty: async (DbCharge, _, { injector }) => {
        const { mainBusinessId } = await getChargeBusinesses(DbCharge.id, injector);
        return mainBusinessId
            ? injector
                .get(FinancialEntitiesProvider)
                .getFinancialEntityByIdLoader.load(mainBusinessId)
                .then(res => res ?? null)
            : null;
    },
    owner: (DbCharge, _, { injector }) => injector
        .get(BusinessesProvider)
        .getBusinessByIdLoader.load(DbCharge.owner_id)
        .then(res => {
        if (!res) {
            throw new Error(`Unable to find financial entity for charge ${DbCharge.owner_id}`);
        }
        return res;
    }),
};
export const commonTransactionFields = {
    counterparty: async (transactionId, _, { injector }) => {
        const transaction = await injector
            .get(TransactionsProvider)
            .transactionByIdLoader.load(transactionId);
        if (!transaction.business_id) {
            return null;
        }
        return injector
            .get(FinancialEntitiesProvider)
            .getFinancialEntityByIdLoader.load(transaction.business_id)
            .then(res => res ?? null);
    },
};
export const commonDocumentsFields = {
    creditor: (documentRoot, _, { injector }) => documentRoot.creditor_id
        ? injector
            .get(FinancialEntitiesProvider)
            .getFinancialEntityByIdLoader.load(documentRoot.creditor_id)
            .then(res => res ?? null)
        : null,
    debtor: (documentRoot, _, { injector }) => documentRoot.debtor_id
        ? injector
            .get(FinancialEntitiesProvider)
            .getFinancialEntityByIdLoader.load(documentRoot.debtor_id)
            .then(res => res ?? null)
        : null,
};
export const ledgerCounterparty = (account) => {
    const resolverFn = (DbLedgerRecord, _, { injector }) => {
        let financialEntityId = null;
        switch (account) {
            case 'CreditAccount1':
                financialEntityId = DbLedgerRecord.credit_entity1;
                break;
            case 'CreditAccount2':
                financialEntityId = DbLedgerRecord.credit_entity2;
                break;
            case 'DebitAccount1':
                financialEntityId = DbLedgerRecord.debit_entity1;
                break;
            case 'DebitAccount2':
                financialEntityId = DbLedgerRecord.debit_entity2;
                break;
            default:
                throw new Error(`Invalid account type: ${account}`);
        }
        return financialEntityId
            ? injector
                .get(FinancialEntitiesProvider)
                .getFinancialEntityByIdLoader.load(financialEntityId)
                .then(res => {
                if (!res) {
                    throw new GraphQLError(`Financial entity ID="${financialEntityId}" not found`);
                }
                return res;
            })
            : null;
    };
    return resolverFn;
};
//# sourceMappingURL=common.js.map