import { __decorate, __metadata } from "tslib";
import DataLoader from 'dataloader';
import { format } from 'date-fns';
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 getReportByBusinessIdAndDates = sql `
SELECT *
FROM accounter_schema.pcn874
WHERE business_id = $businessId AND month_date IN $$monthDates;`;
const updateReport = sql `
  UPDATE accounter_schema.pcn874
  SET content = $content
  WHERE business_id = $businessId AND month_date = $monthDate
  RETURNING *;`;
const insertReport = sql `
  INSERT INTO accounter_schema.pcn874 (business_id, month_date, content, owner_id)
  VALUES ($businessId, $monthDate, $content, $ownerId)
  RETURNING *;`;
let VatReportProvider = class VatReportProvider {
    db;
    adminContextProvider;
    constructor(db, adminContextProvider) {
        this.db = db;
        this.adminContextProvider = adminContextProvider;
    }
    async batchReportsByBusinessIdAndMonthDatesLoader(businessAndDates) {
        const businessIdsMap = new Map();
        businessAndDates.map(([businessId, monthDate]) => {
            if (businessIdsMap.has(businessId)) {
                businessIdsMap.get(businessId)?.add(monthDate);
            }
            else {
                businessIdsMap.set(businessId, new Set([monthDate]));
            }
        });
        const reports = (await Promise.all(Array.from(businessIdsMap.entries()).map(async ([businessId, monthDates]) => getReportByBusinessIdAndDates.run({ businessId, monthDates: Array.from(monthDates) }, this.db)))).flat();
        return businessAndDates.map(([businessId, monthDate]) => reports.find(report => report.business_id === businessId &&
            format(report.month_date, 'yyyy-MM-dd') === monthDate)?.content);
    }
    getReportByBusinessIdAndMonthDateLoader = new DataLoader((businessAndDates) => this.batchReportsByBusinessIdAndMonthDatesLoader(businessAndDates), { cacheKeyFn: ([businessId, monthDate]) => `${businessId}-${monthDate}` });
    async updateReport(params) {
        if (params.businessId && params.monthDate) {
            await this.invalidateByBusinessIdAndMonth(params.businessId, format(params.monthDate, 'yyyy-MM-dd'));
        }
        return updateReport.run(params, this.db);
    }
    async insertReport(params) {
        if (params.businessId && params.monthDate) {
            await this.invalidateByBusinessIdAndMonth(params.businessId, format(params.monthDate, 'yyyy-MM-dd'));
        }
        const { ownerId } = await this.adminContextProvider.getVerifiedAdminContext();
        return insertReport.run(reassureOwnerIdExists(params, ownerId), this.db);
    }
    async invalidateByBusinessIdAndMonth(businessId, monthDate) {
        this.getReportByBusinessIdAndMonthDateLoader.clear([businessId, monthDate]);
    }
    clearCache() {
        this.getReportByBusinessIdAndMonthDateLoader.clearAll();
    }
};
VatReportProvider = __decorate([
    Injectable({
        scope: Scope.Operation,
        global: true,
    }),
    __metadata("design:paramtypes", [TenantAwareDBClient,
        AdminContextProvider])
], VatReportProvider);
export { VatReportProvider };
//# sourceMappingURL=vat-report.provider.js.map