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 getFinancialAccountTaxCategoriesByAccountIds = sql `
    SELECT *
    FROM accounter_schema.financial_accounts_tax_categories
    WHERE financial_account_id IN $$accountIds;`;
const updateFinancialAccountTaxCategory = sql `
      UPDATE accounter_schema.financial_accounts_tax_categories
      SET
      tax_category_id = COALESCE(
        $taxCategoryId,
        tax_category_id
      )
      WHERE
        financial_account_id = $financialAccountId
        AND currency = $currency
      RETURNING *;
    `;
const insertFinancialAccountTaxCategories = sql `
      INSERT INTO accounter_schema.financial_accounts_tax_categories (
        financial_account_id, currency, tax_category_id, owner_id
      )
      VALUES $$financialAccountsTaxCategories(
        financialAccountId, currency, taxCategoryId, ownerId
      )
      RETURNING *;`;
const deleteFinancialAccountTaxCategories = sql `
        DELETE FROM accounter_schema.financial_accounts_tax_categories
        WHERE financial_account_id = $financialAccountId
          AND currency IN $$currencies
        
        RETURNING *;
      `;
let FinancialAccountsTaxCategoriesProvider = class FinancialAccountsTaxCategoriesProvider {
    db;
    adminContextProvider;
    constructor(db, adminContextProvider) {
        this.db = db;
        this.adminContextProvider = adminContextProvider;
    }
    async batchFinancialAccountTaxCategoriesByAccountIds(accountIds) {
        const taxCategories = await getFinancialAccountTaxCategoriesByAccountIds.run({
            accountIds,
        }, this.db);
        return accountIds.map(accountId => taxCategories.filter(tc => tc.financial_account_id === accountId));
    }
    getFinancialAccountTaxCategoriesByFinancialAccountIdLoader = new DataLoader((financialAccountId) => this.batchFinancialAccountTaxCategoriesByAccountIds(financialAccountId));
    async updateFinancialAccountTaxCategory(params) {
        if (params.financialAccountId) {
            this.invalidateByAccountId(params.financialAccountId);
        }
        return updateFinancialAccountTaxCategory.run(params, this.db);
    }
    async deleteFinancialAccountTaxCategories(params) {
        if (params.financialAccountId) {
            this.invalidateByAccountId(params.financialAccountId);
        }
        return deleteFinancialAccountTaxCategories.run(params, this.db);
    }
    async insertFinancialAccountTaxCategories(params) {
        params.financialAccountsTaxCategories.map(tc => {
            if (tc.financialAccountId) {
                this.invalidateByAccountId(tc.financialAccountId);
            }
        });
        const { ownerId } = await this.adminContextProvider.getVerifiedAdminContext();
        const taxCategoriesWithOwnerId = params.financialAccountsTaxCategories.map(tc => reassureOwnerIdExists(tc, ownerId));
        return insertFinancialAccountTaxCategories.run({ financialAccountsTaxCategories: taxCategoriesWithOwnerId }, this.db);
    }
    invalidateByAccountId(financialAccountId) {
        this.getFinancialAccountTaxCategoriesByFinancialAccountIdLoader.clear(financialAccountId);
    }
    clearCache() {
        this.getFinancialAccountTaxCategoriesByFinancialAccountIdLoader.clearAll();
    }
};
FinancialAccountsTaxCategoriesProvider = __decorate([
    Injectable({
        scope: Scope.Operation,
        global: true,
    }),
    __metadata("design:paramtypes", [TenantAwareDBClient,
        AdminContextProvider])
], FinancialAccountsTaxCategoriesProvider);
export { FinancialAccountsTaxCategoriesProvider };
//# sourceMappingURL=financial-accounts-tax-categories.provider.js.map