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 getChargeUnbalancedBusinessesByChargeIds = sql `
    SELECT * FROM accounter_schema.charge_unbalanced_ledger_businesses
    WHERE charge_id IN $$chargeIds;`;
const insertChargeUnbalancedBusinesses = sql `
  INSERT INTO accounter_schema.charge_unbalanced_ledger_businesses (charge_id, business_id, remark, owner_id)
  VALUES $$unbalancedBusinesses(chargeId, businessId, remark, ownerId)
  ON CONFLICT DO NOTHING
  RETURNING *;
`;
const deleteChargeUnbalancedBusinesses = sql `
    DELETE FROM accounter_schema.charge_unbalanced_ledger_businesses
    WHERE charge_id = $chargeId
        AND business_id IN $$businessIds;`;
const deleteChargeUnbalancedBusinessesByBusinessId = sql `
    DELETE FROM accounter_schema.charge_unbalanced_ledger_businesses
    WHERE business_id = $businessId;`;
const deleteChargeUnbalancedBusinessesByChargeId = sql `
    DELETE FROM accounter_schema.charge_unbalanced_ledger_businesses
    WHERE charge_id = $chargeId;`;
const updateChargeUnbalancedBusiness = sql `
  UPDATE accounter_schema.charge_unbalanced_ledger_businesses
  SET remark = $remark
  WHERE charge_id = $chargeId
    AND business_id = $businessId
  RETURNING *;
`;
let UnbalancedBusinessesProvider = class UnbalancedBusinessesProvider {
    db;
    adminContextProvider;
    constructor(db, adminContextProvider) {
        this.db = db;
        this.adminContextProvider = adminContextProvider;
    }
    async batchChargeUnbalancedBusinessesByChargeIds(chargeIds) {
        const ids = new Set(chargeIds);
        const unbalancedBusinesses = await getChargeUnbalancedBusinessesByChargeIds.run({
            chargeIds: Array.from(ids),
        }, this.db);
        return chargeIds.map(id => unbalancedBusinesses.filter(unbalancedBusiness => unbalancedBusiness.charge_id === id));
    }
    getChargeUnbalancedBusinessesByChargeIds = new DataLoader((keys) => this.batchChargeUnbalancedBusinessesByChargeIds(keys));
    async insertChargeUnbalancedBusinesses(params) {
        if (params.unbalancedBusinesses.length) {
            params.unbalancedBusinesses.map(({ chargeId }) => {
                if (chargeId)
                    this.invalidateByChargeId(chargeId);
            });
        }
        const { ownerId } = await this.adminContextProvider.getVerifiedAdminContext();
        return insertChargeUnbalancedBusinesses.run({
            unbalancedBusinesses: params.unbalancedBusinesses.map(business => ({
                ...business,
                ownerId: business.ownerId || ownerId,
            })),
        }, this.db);
    }
    deleteChargeUnbalancedBusinesses(params) {
        if (params.chargeId) {
            this.invalidateByChargeId(params.chargeId);
        }
        return deleteChargeUnbalancedBusinesses.run(params, this.db);
    }
    deleteChargeUnbalancedBusinessesByChargeId(params) {
        if (params.chargeId) {
            this.invalidateByChargeId(params.chargeId);
        }
        return deleteChargeUnbalancedBusinessesByChargeId.run(params, this.db);
    }
    deleteChargeUnbalancedBusinessesByBusinessId(businessId) {
        return deleteChargeUnbalancedBusinessesByBusinessId.run({ businessId }, this.db);
    }
    updateChargeUnbalancedBusiness(params) {
        if (params.chargeId) {
            this.invalidateByChargeId(params.chargeId);
        }
        return updateChargeUnbalancedBusiness.run(params, this.db);
    }
    async invalidateByChargeId(chargeId) {
        this.getChargeUnbalancedBusinessesByChargeIds.clear(chargeId);
    }
    clearCache() {
        this.getChargeUnbalancedBusinessesByChargeIds.clearAll();
    }
};
UnbalancedBusinessesProvider = __decorate([
    Injectable({
        scope: Scope.Operation,
        global: true,
    }),
    __metadata("design:paramtypes", [TenantAwareDBClient,
        AdminContextProvider])
], UnbalancedBusinessesProvider);
export { UnbalancedBusinessesProvider };
//# sourceMappingURL=unbalanced-businesses.provider.js.map