"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetWalletAddressForNetworkUseCase = void 0;
const uuid_1 = require("uuid");
const address_1 = require("../../../domain/entities/address");
const not_found_error_1 = require("../../../domain/errors/not-found-error");
class GetWalletAddressForNetworkUseCase {
    walletRepository;
    networkRepository;
    addressRepository;
    addressGenerator;
    constructor(walletRepository, networkRepository, addressRepository, addressGenerator) {
        this.walletRepository = walletRepository;
        this.networkRepository = networkRepository;
        this.addressRepository = addressRepository;
        this.addressGenerator = addressGenerator;
    }
    async execute(walletId, networkId) {
        const wallet = await this.walletRepository.findById(walletId);
        if (!wallet) {
            throw new not_found_error_1.NotFoundError('Wallet', walletId);
        }
        const network = await this.networkRepository.findById(networkId);
        if (!network) {
            throw new not_found_error_1.NotFoundError('Network', networkId);
        }
        const existing = await this.addressRepository.findByWalletIdAndType(walletId, network.addressType);
        if (existing.length > 0) {
            return existing[0];
        }
        const generated = this.addressGenerator.generate(network, wallet);
        const address = address_1.Address.create((0, uuid_1.v4)(), generated.address, generated.type, walletId, false);
        return this.addressRepository.save(address);
    }
}
exports.GetWalletAddressForNetworkUseCase = GetWalletAddressForNetworkUseCase;
//# sourceMappingURL=get-wallet-address-for-network.use-case.js.map