import db from '@nangohq/database';
import { LogActionEnum } from '../models/Telemetry.js';
import environmentService from './environment.service.js';
import errorManager, { ErrorSourceEnum } from '../utils/error.manager.js';
class AccountService {
    async getAccountById(id) {
        try {
            const result = await db.knex.select('*').from(`_nango_accounts`).where({ id: id }).first();
            return result || null;
        }
        catch (err) {
            errorManager.report(err, {
                source: ErrorSourceEnum.PLATFORM,
                operation: LogActionEnum.DATABASE,
                accountId: id
            });
            return null;
        }
    }
    async editAccount({ name, id }) {
        await db.knex.update({ name, updated_at: new Date() }).from(`_nango_accounts`).where({ id });
    }
    async getAccountByUUID(uuid) {
        const result = await db.knex.select('*').from(`_nango_accounts`).where({ uuid }).first();
        return result || null;
    }
    async getAccountAndEnvironmentIdByUUID(targetAccountUUID, targetEnvironment) {
        const account = await db.knex.select('id').from(`_nango_accounts`).where({ uuid: targetAccountUUID });
        if (account == null || account.length == 0 || account[0] == null) {
            return null;
        }
        const accountId = account[0].id;
        const environment = await db.knex.select('id').from(`_nango_environments`).where({
            account_id: accountId,
            name: targetEnvironment
        });
        if (environment == null || environment.length == 0 || environment[0] == null) {
            return null;
        }
        return { accountId, environmentId: environment[0].id };
    }
    async getUUIDFromAccountId(accountId) {
        const account = await db.knex.select('uuid').from(`_nango_accounts`).where({ id: accountId });
        if (account == null || account.length == 0 || account[0] == null) {
            return null;
        }
        return account[0].uuid;
    }
    async getOrCreateAccount(name) {
        const account = await db.knex.select('id').from(`_nango_accounts`).where({ name });
        if (account == null || account.length == 0 || !account[0]) {
            const newAccount = await db.knex.insert({ name, created_at: new Date() }).into(`_nango_accounts`).returning('*');
            if (!newAccount || newAccount.length == 0 || !newAccount[0]) {
                throw new Error('Failed to create account');
            }
            await environmentService.createDefaultEnvironments(newAccount[0]['id']);
            return newAccount[0];
        }
        return account[0];
    }
    /**
     * Create Account
     * @desc create a new account and assign to the default environments
     */
    async createAccount(name) {
        const result = await db.knex.from(`_nango_accounts`).insert({ name }).returning('*');
        if (result[0]?.id) {
            await environmentService.createDefaultEnvironments(result[0].id);
            return result[0];
        }
        return null;
    }
    async editCustomer(is_capped, accountId) {
        await db.knex.update({ is_capped }).from(`_nango_accounts`).where({ id: accountId });
    }
}
export default new AccountService();
//# sourceMappingURL=account.service.js.map