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 getBusinessTripsAccommodationsExpensesByBusinessTripIds = sql `
  SELECT *
  FROM accounter_schema.business_trips_transactions_accommodations a
  LEFT JOIN accounter_schema.extended_business_trip_transactions t
    USING (id)
  WHERE t.business_trip_id IN $$businessTripIds;`;
const getBusinessTripsAccommodationsExpensesByIds = sql `
  SELECT *
  FROM accounter_schema.business_trips_transactions_accommodations a
  LEFT JOIN accounter_schema.extended_business_trip_transactions t
    USING (id)
  WHERE a.id IN $$expenseIds;`;
const updateBusinessTripAccommodationsExpense = sql `
  UPDATE accounter_schema.business_trips_transactions_accommodations
  SET
  country = COALESCE(
    $country,
    country
  ),
  nights_count = COALESCE(
    $nightsCount,
    nights_count
  ),
  attendees_stay = COALESCE(
    $attendeesStay,
    attendees_stay
  )
  WHERE
    id = $businessTripExpenseId
  RETURNING *;
`;
const insertBusinessTripAccommodationsExpense = sql `
  INSERT INTO accounter_schema.business_trips_transactions_accommodations (id, country, nights_count, attendees_stay, owner_id)
  VALUES($id, $country, $nightsCount, $attendeesStay, $ownerId)
  RETURNING *;`;
const deleteBusinessTripAccommodationsExpense = sql `
  DELETE FROM accounter_schema.business_trips_transactions_accommodations
  WHERE id = $businessTripExpenseId
  RETURNING id;
`;
let BusinessTripAccommodationsExpensesProvider = class BusinessTripAccommodationsExpensesProvider {
    db;
    adminContextProvider;
    constructor(db, adminContextProvider) {
        this.db = db;
        this.adminContextProvider = adminContextProvider;
    }
    async batchBusinessTripsAccommodationsExpensesByBusinessTripIds(businessTripIds) {
        const businessTripsAccommodationsExpenses = await getBusinessTripsAccommodationsExpensesByBusinessTripIds.run({
            businessTripIds,
        }, this.db);
        return businessTripIds.map(id => businessTripsAccommodationsExpenses.filter(record => record.business_trip_id === id));
    }
    getBusinessTripsAccommodationsExpensesByBusinessTripIdLoader = new DataLoader((ids) => this.batchBusinessTripsAccommodationsExpensesByBusinessTripIds(ids));
    async batchBusinessTripsAccommodationsExpensesByIds(expenseIds) {
        const businessTripsAccommodationsExpenses = await getBusinessTripsAccommodationsExpensesByIds.run({
            expenseIds,
        }, this.db);
        return expenseIds.map(id => businessTripsAccommodationsExpenses.find(record => record.id === id));
    }
    getBusinessTripsAccommodationsExpensesByIdLoader = new DataLoader((ids) => this.batchBusinessTripsAccommodationsExpensesByIds(ids));
    async updateBusinessTripAccommodationsExpense(params) {
        if (params.businessTripExpenseId) {
            this.invalidateById(params.businessTripExpenseId);
        }
        return updateBusinessTripAccommodationsExpense.run(params, this.db);
    }
    async insertBusinessTripAccommodationsExpense(params) {
        params.attendeesStay ||= [];
        const { ownerId } = await this.adminContextProvider.getVerifiedAdminContext();
        return insertBusinessTripAccommodationsExpense.run(reassureOwnerIdExists(params, ownerId), this.db);
    }
    async deleteBusinessTripAccommodationsExpense(params) {
        if (params.businessTripExpenseId) {
            this.invalidateById(params.businessTripExpenseId);
        }
        return deleteBusinessTripAccommodationsExpense.run(params, this.db);
    }
    async invalidateById(expenseId) {
        const expense = await this.getBusinessTripsAccommodationsExpensesByIdLoader.load(expenseId);
        if (expense?.business_trip_id) {
            this.getBusinessTripsAccommodationsExpensesByBusinessTripIdLoader.clear(expense.business_trip_id);
        }
        this.getBusinessTripsAccommodationsExpensesByIdLoader.clear(expenseId);
    }
    async invalidateByBusinessTripId(businessTripId) {
        const expenses = await this.getBusinessTripsAccommodationsExpensesByBusinessTripIdLoader.load(businessTripId);
        for (const expense of expenses ?? []) {
            this.getBusinessTripsAccommodationsExpensesByIdLoader.clear(expense.id);
        }
        this.getBusinessTripsAccommodationsExpensesByBusinessTripIdLoader.clear(businessTripId);
    }
    clearCache() {
        this.getBusinessTripsAccommodationsExpensesByBusinessTripIdLoader.clearAll();
        this.getBusinessTripsAccommodationsExpensesByIdLoader.clearAll();
    }
};
BusinessTripAccommodationsExpensesProvider = __decorate([
    Injectable({
        scope: Scope.Operation,
        global: true,
    }),
    __metadata("design:paramtypes", [TenantAwareDBClient,
        AdminContextProvider])
], BusinessTripAccommodationsExpensesProvider);
export { BusinessTripAccommodationsExpensesProvider };
//# sourceMappingURL=business-trips-expenses-accommodations.provider.js.map