import { __decorate, __metadata } from "tslib";
import DataLoader from 'dataloader';
import { Injectable, Scope } from 'graphql-modules';
import { sql } from '@pgtyped/runtime';
import { AdminContextProvider } from '../../admin-context/providers/admin-context.provider.js';
import { TenantAwareDBClient } from '../../app-providers/tenant-db-client.js';
const getChargesSpreadByChargeIds = sql `
    SELECT *
    FROM accounter_schema.charge_spread
    WHERE charge_id IN $$chargeIds;`;
const updateChargeSpread = sql `
  WITH variables (year) as (
    values ($year::DATE)
  )
  UPDATE accounter_schema.charge_spread
  SET
  year_of_relevance = COALESCE(
    variables.year,
    year_of_relevance
  ),
  amount = COALESCE(
    $amount,
    amount
  )
  FROM variables
  WHERE
    charge_id = $chargeId
    AND year_of_relevance = variables.year
  RETURNING *;
`;
const insertChargeSpread = sql `
  INSERT INTO accounter_schema.charge_spread (charge_id, year_of_relevance, amount, owner_id)
  VALUES $$chargeSpread(
    chargeId,
    yearOfRelevance,
    amount,
    ownerId
  )
  RETURNING *;
`;
const deleteAllChargeSpreadByChargeIds = sql `
    DELETE FROM accounter_schema.charge_spread
    WHERE charge_id IN $$chargeIds;`;
const deleteAllChargeSpreadByChargeIdAndYearOfRelevance = sql `
    DELETE FROM accounter_schema.charge_spread
    WHERE charge_id = $chargeId
    AND year_of_relevance = $yearOfRelevance;`;
let ChargeSpreadProvider = class ChargeSpreadProvider {
    db;
    adminContextProvider;
    constructor(db, adminContextProvider) {
        this.db = db;
        this.adminContextProvider = adminContextProvider;
    }
    async batchChargesSpreadByChargeIds(ids) {
        const charges = await getChargesSpreadByChargeIds.run({
            chargeIds: ids,
        }, this.db);
        return ids.map(id => charges.filter(chargeSpread => chargeSpread.charge_id === id));
    }
    getChargeSpreadByChargeIdLoader = new DataLoader((keys) => this.batchChargesSpreadByChargeIds(keys));
    async updateChargeSpread(params) {
        return updateChargeSpread.run(params, this.db);
    }
    async insertChargeSpread(params) {
        const { ownerId } = await this.adminContextProvider.getVerifiedAdminContext();
        return insertChargeSpread.run({
            chargeSpread: params.chargeSpread.map(spread => ({
                ...spread,
                ownerId: spread.ownerId || ownerId,
            })),
        }, this.db);
    }
    async deleteAllChargeSpreadByChargeIds(params) {
        return deleteAllChargeSpreadByChargeIds.run(params, this.db);
    }
    async deleteAllChargeSpreadByChargeIdAndYearOfRelevance(params) {
        return deleteAllChargeSpreadByChargeIdAndYearOfRelevance.run(params, this.db);
    }
};
ChargeSpreadProvider = __decorate([
    Injectable({
        scope: Scope.Operation,
        global: true,
    }),
    __metadata("design:paramtypes", [TenantAwareDBClient,
        AdminContextProvider])
], ChargeSpreadProvider);
export { ChargeSpreadProvider };
//# sourceMappingURL=charge-spread.provider.js.map