import { __decorate, __metadata } from "tslib";
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';
import { BusinessTripAttendeesProvider } from '../../business-trips/providers/business-trips-attendees.provider.js';
import { DividendsProvider } from '../../dividends/providers/dividends.provider.js';
import { BalanceCancellationProvider } from '../../ledger/providers/balance-cancellation.provider.js';
import { UnbalancedBusinessesProvider } from '../../ledger/providers/unbalanced-businesses.provider.js';
import { EmployeesProvider } from '../../salaries/providers/employees.provider.js';
import { FundsProvider } from '../../salaries/providers/funds.provider.js';
import { BusinessesProvider } from './businesses.provider.js';
import { ClientsProvider } from './clients.provider.js';
import { TaxCategoriesProvider } from './tax-categories.provider.js';
const deleteBusiness = sql `
  DELETE FROM accounter_schema.businesses
  WHERE id = $businessId
  RETURNING id;
`;
let BusinessesOperationProvider = class BusinessesOperationProvider {
    db;
    adminContextProvider;
    businessesProvider;
    taxCategoryProvider;
    clientsProvider;
    employeesProvider;
    fundsProvider;
    dividendsProvider;
    businessTripAttendeesProvider;
    balanceCancellationProvider;
    unbalancedBusinessesProvider;
    constructor(db, adminContextProvider, businessesProvider, taxCategoryProvider, clientsProvider, employeesProvider, fundsProvider, dividendsProvider, businessTripAttendeesProvider, balanceCancellationProvider, unbalancedBusinessesProvider) {
        this.db = db;
        this.adminContextProvider = adminContextProvider;
        this.businessesProvider = businessesProvider;
        this.taxCategoryProvider = taxCategoryProvider;
        this.clientsProvider = clientsProvider;
        this.employeesProvider = employeesProvider;
        this.fundsProvider = fundsProvider;
        this.dividendsProvider = dividendsProvider;
        this.businessTripAttendeesProvider = businessTripAttendeesProvider;
        this.balanceCancellationProvider = balanceCancellationProvider;
        this.unbalancedBusinessesProvider = unbalancedBusinessesProvider;
    }
    async deleteBusinessById(businessId) {
        this.businessesProvider.invalidateBusinessById(businessId);
        // remove employee
        const employeePromise = this.employeesProvider.getEmployeesByIdLoader
            .load(businessId)
            .then(employee => {
            if (employee)
                throw new Error('Cannot delete business as it represent an employee');
        });
        // remove pension funds
        const fundPromise = this.fundsProvider.getAllFunds().then(funds => {
            const fund = funds.find(f => f.id === businessId);
            if (fund)
                throw new Error('Cannot delete business as it represent a pension/training fund');
        });
        // remove business trips attendees
        const tripsPromise = this.businessTripAttendeesProvider
            .getBusinessTripsByAttendeeId(businessId)
            .then(trips => {
            if (trips.length)
                throw new Error('Cannot delete business as it represent business trip attendee');
        });
        // remove from dividends
        const dividendsPromise = this.dividendsProvider.getDividendsByBusinessIdLoader
            .load(businessId)
            .then(dividends => {
            if (dividends.length)
                throw new Error('Cannot delete business as it represent dividends receiver');
        });
        // some validations before deleting the business
        const [{ ownerId }] = await Promise.all([
            this.adminContextProvider.getVerifiedAdminContext(),
            employeePromise,
            tripsPromise,
            fundPromise,
            dividendsPromise,
        ]);
        // remove from charge unbalanced ledger businesses
        const deleteUnbalancedChargesBusinesses = this.unbalancedBusinessesProvider.deleteChargeUnbalancedBusinessesByBusinessId(businessId);
        // remove from charge balance cancellation
        const deleteBalanceCancellation = this.balanceCancellationProvider.deleteBalanceCancellationByBusinessId(businessId);
        // remove from business green invoice match
        const deleteGreenInvoiceMatch = this.clientsProvider.deleteClient(businessId);
        // TODO: remove when corporate: corporate-tax-variables
        // TODO: remove when owner: financial entities, charges, business-tax-category, ledger
        // TODO: should remove transactions, documents?
        // remove business-tax-category matches
        const deleteMatchingTaxCategory = this.taxCategoryProvider.deleteBusinessTaxCategory({
            businessId,
            ownerId,
        });
        await Promise.all([
            deleteUnbalancedChargesBusinesses,
            deleteBalanceCancellation,
            deleteGreenInvoiceMatch,
            deleteMatchingTaxCategory,
        ]);
        // delete businesses
        await deleteBusiness.run({ businessId }, this.db);
    }
};
BusinessesOperationProvider = __decorate([
    Injectable({
        scope: Scope.Operation,
        global: true,
    }),
    __metadata("design:paramtypes", [TenantAwareDBClient,
        AdminContextProvider,
        BusinessesProvider,
        TaxCategoriesProvider,
        ClientsProvider,
        EmployeesProvider,
        FundsProvider,
        DividendsProvider,
        BusinessTripAttendeesProvider,
        BalanceCancellationProvider,
        UnbalancedBusinessesProvider])
], BusinessesOperationProvider);
export { BusinessesOperationProvider };
//# sourceMappingURL=businesses-operation.provider.js.map