import { __decorate, __metadata } from "tslib";
import DataLoader from 'dataloader';
import { Injectable, Scope } from 'graphql-modules';
import { sql } from '@pgtyped/runtime';
import { TenantAwareDBClient } from '../../app-providers/tenant-db-client.js';
import { adminBusinessUpdateSchema } from '../helpers/admin-businesses.helper.js';
const getAdminBusinessesByIds = sql `
    SELECT ab.*, f.name, b.vat_number
    FROM accounter_schema.businesses_admin ab
    INNER JOIN accounter_schema.businesses b
      USING (id)
    INNER JOIN accounter_schema.financial_entities f
      USING (id)
    WHERE ab.id IN $$ids;`;
const getAllAdminBusinesses = sql `
    SELECT ab.*, f.name, b.vat_number
    FROM accounter_schema.businesses_admin ab
    INNER JOIN accounter_schema.businesses b
      USING (id)
    INNER JOIN accounter_schema.financial_entities f
      USING (id)
    INNER JOIN accounter_schema.financial_entities fe
      USING (id);`;
const updateAdminBusinesses = sql `
    UPDATE accounter_schema.businesses_admin
    SET
    business_registration_start_date = COALESCE(
      $businessRegistrationStartDate,
      business_registration_start_date
    ),
    company_tax_id = COALESCE(
      $companyTaxId,
      company_tax_id
    ),
    advance_tax_rates = COALESCE(
      $advanceTaxRates,
      advance_tax_rates
    ),
    tax_advances_ids = COALESCE(
      $taxAdvancesIds,
      tax_advances_ids
    ),
    social_security_employer_ids = COALESCE(
      $socialSecurityEmployerIds,
      social_security_employer_ids
    ),
    withholding_tax_annual_ids = COALESCE(
      $withholdingTaxAnnualIds,
      withholding_tax_annual_ids
    )
    WHERE
      id = $id
    RETURNING *;`;
const insertAdminBusiness = sql `
  INSERT INTO accounter_schema.businesses_admin (id, owner_id) VALUES ($id, $id) ON CONFLICT (id) DO NOTHING;`;
let AdminBusinessesProvider = class AdminBusinessesProvider {
    db;
    constructor(db) {
        this.db = db;
    }
    async batchAdminBusinessesByIds(ids) {
        const uniqueIds = [...new Set(ids)];
        const adminBusinesses = await getAdminBusinessesByIds.run({
            ids: uniqueIds,
        }, this.db);
        adminBusinesses.map(adminBusiness => {
            this.getAdminBusinessByIdLoader.prime(adminBusiness.id, adminBusiness);
        });
        return ids.map(id => adminBusinesses.find(admin => admin.id === id));
    }
    getAdminBusinessByIdLoader = new DataLoader((ids) => this.batchAdminBusinessesByIds(ids));
    allAdminBusinessesPromise = null;
    getAllAdminBusinesses() {
        if (this.allAdminBusinessesPromise) {
            return this.allAdminBusinessesPromise;
        }
        this.allAdminBusinessesPromise = getAllAdminBusinesses
            .run(undefined, this.db)
            .then(adminBusinesses => {
            adminBusinesses.map(adminBusiness => {
                this.getAdminBusinessByIdLoader.prime(adminBusiness.id, adminBusiness);
            });
            return adminBusinesses;
        });
        return this.allAdminBusinessesPromise;
    }
    async insertAdminBusiness(id, client) {
        await insertAdminBusiness.run({ id }, client ?? this.db);
    }
    async updateAdminBusiness(params) {
        const inputParams = adminBusinessUpdateSchema.parse(params);
        const [result] = await updateAdminBusinesses.run(inputParams, this.db);
        this.clearCache();
        return result;
    }
    clearCache() {
        this.allAdminBusinessesPromise = null;
        this.getAdminBusinessByIdLoader.clearAll();
    }
};
AdminBusinessesProvider = __decorate([
    Injectable({
        scope: Scope.Operation,
        global: true,
    }),
    __metadata("design:paramtypes", [TenantAwareDBClient])
], AdminBusinessesProvider);
export { AdminBusinessesProvider };
//# sourceMappingURL=admin-businesses.provider.js.map