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 getBusinessTripsCarRentalExpensesByBusinessTripIds = sql `
  SELECT *
  FROM accounter_schema.business_trips_transactions_car_rental a
  LEFT JOIN accounter_schema.extended_business_trip_transactions t
    USING (id)
  WHERE ($isBusinessTripIds = 0 OR t.business_trip_id IN $$businessTripIds);`;
const getBusinessTripsCarRentalExpensesByIds = sql `
  SELECT *
  FROM accounter_schema.business_trips_transactions_car_rental a
  LEFT JOIN accounter_schema.extended_business_trip_transactions t
    USING (id)
  WHERE ($isIds = 0 OR t.id IN $$expenseIds);`;
const updateBusinessTripCarRentalExpense = sql `
  UPDATE accounter_schema.business_trips_transactions_car_rental
  SET
  days = COALESCE(
    $days,
    days
  ),
  is_fuel_expense = COALESCE(
    $isFuelExpense,
    is_fuel_expense
  )
  WHERE
    id = $businessTripExpenseId
  RETURNING *;
`;
const insertBusinessTripCarRentalExpense = sql `
  INSERT INTO accounter_schema.business_trips_transactions_car_rental (id, days, is_fuel_expense, owner_id)
  VALUES($id, $days, $isFuelExpense, $ownerId)
  RETURNING *;`;
const deleteBusinessTripCarRentalExpense = sql `
  DELETE FROM accounter_schema.business_trips_transactions_car_rental
  WHERE id = $businessTripExpenseId
  RETURNING id;
`;
let BusinessTripCarRentalExpensesProvider = class BusinessTripCarRentalExpensesProvider {
    db;
    adminContextProvider;
    constructor(db, adminContextProvider) {
        this.db = db;
        this.adminContextProvider = adminContextProvider;
    }
    async batchBusinessTripsCarRentalExpensesByBusinessTripIds(businessTripIds) {
        const businessTripsCarRentalExpenses = await getBusinessTripsCarRentalExpensesByBusinessTripIds.run({
            isBusinessTripIds: businessTripIds.length > 0 ? 1 : 0,
            businessTripIds,
        }, this.db);
        return businessTripIds.map(id => businessTripsCarRentalExpenses.filter(record => record.business_trip_id === id));
    }
    getBusinessTripsCarRentalExpensesByBusinessTripIdLoader = new DataLoader((ids) => this.batchBusinessTripsCarRentalExpensesByBusinessTripIds(ids));
    async batchBusinessTripsCarRentalExpensesByIds(expenseIds) {
        const businessTripsCarRentalExpenses = await getBusinessTripsCarRentalExpensesByIds.run({
            isIds: expenseIds.length > 0 ? 1 : 0,
            expenseIds,
        }, this.db);
        return expenseIds.map(id => businessTripsCarRentalExpenses.find(record => record.id === id));
    }
    getBusinessTripsCarRentalExpensesByIdLoader = new DataLoader((ids) => this.batchBusinessTripsCarRentalExpensesByIds(ids));
    async updateBusinessTripCarRentalExpense(params) {
        if (params.businessTripExpenseId) {
            this.invalidateById(params.businessTripExpenseId);
        }
        return updateBusinessTripCarRentalExpense.run(params, this.db);
    }
    async insertBusinessTripCarRentalExpense(params) {
        const { ownerId } = await this.adminContextProvider.getVerifiedAdminContext();
        return insertBusinessTripCarRentalExpense.run(reassureOwnerIdExists(params, ownerId), this.db);
    }
    async deleteBusinessTripCarRentalExpense(params) {
        if (params.businessTripExpenseId) {
            this.invalidateById(params.businessTripExpenseId);
        }
        return deleteBusinessTripCarRentalExpense.run(params, this.db);
    }
    async invalidateById(expenseId) {
        const expense = await this.getBusinessTripsCarRentalExpensesByIdLoader.load(expenseId);
        if (expense?.business_trip_id) {
            this.getBusinessTripsCarRentalExpensesByBusinessTripIdLoader.clear(expense.business_trip_id);
        }
        this.getBusinessTripsCarRentalExpensesByIdLoader.clear(expenseId);
    }
    async invalidateByBusinessTripId(businessTripId) {
        const expenses = await this.getBusinessTripsCarRentalExpensesByBusinessTripIdLoader.load(businessTripId);
        for (const expense of expenses ?? []) {
            this.getBusinessTripsCarRentalExpensesByIdLoader.clear(expense.id);
        }
        this.getBusinessTripsCarRentalExpensesByBusinessTripIdLoader.clear(businessTripId);
    }
    clearCache() {
        this.getBusinessTripsCarRentalExpensesByBusinessTripIdLoader.clearAll();
        this.getBusinessTripsCarRentalExpensesByIdLoader.clearAll();
    }
};
BusinessTripCarRentalExpensesProvider = __decorate([
    Injectable({
        scope: Scope.Operation,
        global: true,
    }),
    __metadata("design:paramtypes", [TenantAwareDBClient,
        AdminContextProvider])
], BusinessTripCarRentalExpensesProvider);
export { BusinessTripCarRentalExpensesProvider };
//# sourceMappingURL=business-trips-expenses-car-rental.provider.js.map