import { __decorate, __metadata } from "tslib";
import DataLoader from 'dataloader';
import { Injectable, Scope } from 'graphql-modules';
import { sql } from '@pgtyped/runtime';
import { reassureOwnerIdExists } from '../../../shared/helpers/index.js';
import { AdminContextProvider } from '../../admin-context/providers/admin-context.provider.js';
import { TenantAwareDBClient } from '../../app-providers/tenant-db-client.js';
const getBusinessTripsExpenseMatchesByTransactionIds = sql `
  SELECT *
  FROM accounter_schema.business_trips_transactions_match
  WHERE transaction_id in $$transactionIds;`;
const getBusinessTripsExpenseMatchesByExpenseIds = sql `
  SELECT *
  FROM accounter_schema.business_trips_transactions_match
  WHERE business_trip_transaction_id in $$expenseIds;`;
const insertBusinessTripExpenseMatch = sql `
  INSERT INTO accounter_schema.business_trips_transactions_match (business_trip_transaction_id, transaction_id, amount, owner_id)
  VALUES($businessTripExpenseId, $transactionId, $amount, $ownerId)
  RETURNING *;`;
const deleteBusinessTripExpenseMatch = sql `
  DELETE FROM accounter_schema.business_trips_transactions_match
  WHERE business_trip_transaction_id = $businessTripExpenseId
  RETURNING *;`;
const deleteSpecificBusinessTripExpenseMatch = sql `
  DELETE FROM accounter_schema.business_trips_transactions_match
  WHERE business_trip_transaction_id = $businessTripExpenseId
    AND transaction_id = $transactionId
  RETURNING *;`;
let BusinessTripExpensesTransactionsMatchProvider = class BusinessTripExpensesTransactionsMatchProvider {
    db;
    adminContextProvider;
    constructor(db, adminContextProvider) {
        this.db = db;
        this.adminContextProvider = adminContextProvider;
    }
    async batchBusinessTripsExpenseMatchesByTransactionIds(transactionIds) {
        const businessTrips = await getBusinessTripsExpenseMatchesByTransactionIds.run({
            transactionIds: transactionIds,
        }, this.db);
        return transactionIds.map(id => businessTrips.filter(record => record.transaction_id === id));
    }
    getBusinessTripsExpenseMatchesByTransactionIdLoader = new DataLoader((ids) => this.batchBusinessTripsExpenseMatchesByTransactionIds(ids));
    async batchBusinessTripsExpenseMatchesByExpenseIds(expenseIds) {
        const businessTrips = await getBusinessTripsExpenseMatchesByExpenseIds.run({
            expenseIds: expenseIds,
        }, this.db);
        return expenseIds.map(id => businessTrips.filter(record => record.business_trip_transaction_id === id));
    }
    getBusinessTripsExpenseMatchesByExpenseIdLoader = new DataLoader((ids) => this.batchBusinessTripsExpenseMatchesByExpenseIds(ids));
    async insertBusinessTripExpenseMatch(params) {
        if (params.businessTripExpenseId) {
            this.getBusinessTripsExpenseMatchesByExpenseIdLoader.clear(params.businessTripExpenseId);
        }
        if (params.transactionId) {
            this.getBusinessTripsExpenseMatchesByTransactionIdLoader.clear(params.transactionId);
        }
        const { ownerId } = await this.adminContextProvider.getVerifiedAdminContext();
        return insertBusinessTripExpenseMatch.run(reassureOwnerIdExists(params, ownerId), this.db);
    }
    async deleteBusinessTripExpenseMatch(params) {
        if (params.businessTripExpenseId) {
            const expenses = await this.getBusinessTripsExpenseMatchesByExpenseIdLoader.load(params.businessTripExpenseId);
            expenses.map(expense => {
                this.getBusinessTripsExpenseMatchesByTransactionIdLoader.clear(expense.transaction_id);
            });
            this.getBusinessTripsExpenseMatchesByExpenseIdLoader.clear(params.businessTripExpenseId);
        }
        return deleteBusinessTripExpenseMatch.run(params, this.db);
    }
    async deleteSpecificBusinessTripExpenseMatch(params) {
        if (params.businessTripExpenseId) {
            this.getBusinessTripsExpenseMatchesByExpenseIdLoader.clear(params.businessTripExpenseId);
        }
        if (params.transactionId) {
            this.getBusinessTripsExpenseMatchesByTransactionIdLoader.clear(params.transactionId);
        }
        return deleteSpecificBusinessTripExpenseMatch.run(params, this.db);
    }
    clearCache() {
        this.getBusinessTripsExpenseMatchesByTransactionIdLoader.clearAll();
        this.getBusinessTripsExpenseMatchesByExpenseIdLoader.clearAll();
    }
};
BusinessTripExpensesTransactionsMatchProvider = __decorate([
    Injectable({
        scope: Scope.Operation,
        global: true,
    }),
    __metadata("design:paramtypes", [TenantAwareDBClient,
        AdminContextProvider])
], BusinessTripExpensesTransactionsMatchProvider);
export { BusinessTripExpensesTransactionsMatchProvider };
//# sourceMappingURL=business-trips-expenses-transactions-match.provider.js.map