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 getBusinessTripsTravelAndSubsistenceExpensesByBusinessTripIds = sql `
  SELECT *
  FROM accounter_schema.business_trips_transactions_tns a
  LEFT JOIN accounter_schema.extended_business_trip_transactions t
    USING (id)
  WHERE ($isBusinessTripIds = 0 OR t.business_trip_id IN $$businessTripIds);`;
const getBusinessTripsTravelAndSubsistenceExpensesByIds = sql `
  SELECT *
  FROM accounter_schema.business_trips_transactions_tns a
  LEFT JOIN accounter_schema.extended_business_trip_transactions t
    USING (id)
  WHERE ($isIds = 0 OR t.id IN $$expenseIds);`;
const updateBusinessTripTravelAndSubsistenceExpense = sql `
  UPDATE accounter_schema.business_trips_transactions_tns
  SET
  expense_type = COALESCE(
    $expenseType,
    expense_type
  )
  WHERE
    id = $businessTripExpenseId
  RETURNING *;
`;
const insertBusinessTripTravelAndSubsistenceExpense = sql `
  INSERT INTO accounter_schema.business_trips_transactions_tns (id, expense_type, owner_id)
  VALUES($id, $expenseType, $ownerId)
  RETURNING *;`;
const deleteBusinessTripTravelAndSubsistenceExpense = sql `
  DELETE FROM accounter_schema.business_trips_transactions_tns
  WHERE id = $businessTripExpenseId
  RETURNING id;
`;
let BusinessTripTravelAndSubsistenceExpensesProvider = class BusinessTripTravelAndSubsistenceExpensesProvider {
    db;
    adminContextProvider;
    constructor(db, adminContextProvider) {
        this.db = db;
        this.adminContextProvider = adminContextProvider;
    }
    async batchBusinessTripsTravelAndSubsistenceExpensesByBusinessTripIds(businessTripIds) {
        const businessTripsTravelAndSubsistenceExpenses = await getBusinessTripsTravelAndSubsistenceExpensesByBusinessTripIds.run({
            isBusinessTripIds: businessTripIds.length > 0 ? 1 : 0,
            businessTripIds,
        }, this.db);
        return businessTripIds.map(id => businessTripsTravelAndSubsistenceExpenses.filter(record => record.business_trip_id === id));
    }
    getBusinessTripsTravelAndSubsistenceExpensesByBusinessTripIdLoader = new DataLoader((ids) => this.batchBusinessTripsTravelAndSubsistenceExpensesByBusinessTripIds(ids));
    async batchBusinessTripsTravelAndSubsistenceExpensesByIds(expenseIds) {
        const businessTripsTravelAndSubsistenceExpenses = await getBusinessTripsTravelAndSubsistenceExpensesByIds.run({
            isIds: expenseIds.length > 0 ? 1 : 0,
            expenseIds,
        }, this.db);
        return expenseIds.map(id => businessTripsTravelAndSubsistenceExpenses.find(record => record.id === id));
    }
    getBusinessTripsTravelAndSubsistenceExpensesByIdLoader = new DataLoader((ids) => this.batchBusinessTripsTravelAndSubsistenceExpensesByIds(ids));
    updateBusinessTripTravelAndSubsistenceExpense(params) {
        if (params.businessTripExpenseId) {
            this.invalidateById(params.businessTripExpenseId);
        }
        return updateBusinessTripTravelAndSubsistenceExpense.run(params, this.db);
    }
    async insertBusinessTripTravelAndSubsistenceExpense(params) {
        const { ownerId } = await this.adminContextProvider.getVerifiedAdminContext();
        return insertBusinessTripTravelAndSubsistenceExpense.run(reassureOwnerIdExists(params, ownerId), this.db);
    }
    deleteBusinessTripTravelAndSubsistenceExpense(params) {
        if (params.businessTripExpenseId) {
            this.invalidateById(params.businessTripExpenseId);
        }
        return deleteBusinessTripTravelAndSubsistenceExpense.run(params, this.db);
    }
    async invalidateById(expenseId) {
        const expense = await this.getBusinessTripsTravelAndSubsistenceExpensesByIdLoader.load(expenseId);
        if (expense) {
            this.getBusinessTripsTravelAndSubsistenceExpensesByIdLoader.clear(expenseId);
            if (expense.business_trip_id) {
                this.getBusinessTripsTravelAndSubsistenceExpensesByBusinessTripIdLoader.clear(expense.business_trip_id);
            }
        }
    }
    async invalidateByBusinessTripId(businessTripId) {
        const expenses = await this.getBusinessTripsTravelAndSubsistenceExpensesByBusinessTripIdLoader.load(businessTripId);
        for (const expense of expenses ?? []) {
            this.getBusinessTripsTravelAndSubsistenceExpensesByIdLoader.clear(expense.id);
        }
        this.getBusinessTripsTravelAndSubsistenceExpensesByBusinessTripIdLoader.clear(businessTripId);
    }
    clearCache() {
        this.getBusinessTripsTravelAndSubsistenceExpensesByBusinessTripIdLoader.clearAll();
        this.getBusinessTripsTravelAndSubsistenceExpensesByIdLoader.clearAll();
    }
};
BusinessTripTravelAndSubsistenceExpensesProvider = __decorate([
    Injectable({
        scope: Scope.Operation,
        global: true,
    }),
    __metadata("design:paramtypes", [TenantAwareDBClient,
        AdminContextProvider])
], BusinessTripTravelAndSubsistenceExpensesProvider);
export { BusinessTripTravelAndSubsistenceExpensesProvider };
//# sourceMappingURL=business-trips-expenses-travel-and-subsistence.provider.js.map