"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TypeOrmAddressRepository = void 0;
const address_entity_1 = require("../entities/address.entity");
const data_source_1 = require("../data-source");
const address_mapper_1 = require("./mappers/address.mapper");
class TypeOrmAddressRepository {
    get repository() {
        return data_source_1.AppDataSource.getRepository(address_entity_1.AddressEntity);
    }
    async findById(id) {
        const entity = await this.repository.findOneBy({ id });
        return entity ? address_mapper_1.addressMapper.toDomain(entity) : null;
    }
    async findByIds(ids) {
        if (ids.length === 0) {
            return [];
        }
        const entities = await this.repository
            .createQueryBuilder('address')
            .where('address.id IN (:...ids)', { ids })
            .getMany();
        return entities.map(address_mapper_1.addressMapper.toDomain);
    }
    async findByWalletId(walletId) {
        const entities = await this.repository.find({
            where: { walletId },
            order: { createdAt: 'DESC' },
        });
        return entities.map(address_mapper_1.addressMapper.toDomain);
    }
    async findByWalletIdAndType(walletId, type) {
        const entities = await this.repository.find({
            where: { walletId, type },
            order: { createdAt: 'DESC' },
        });
        return entities.map(address_mapper_1.addressMapper.toDomain);
    }
    async findByAddress(address) {
        const isEvm = address.toLowerCase().startsWith('0x');
        if (!isEvm) {
            const entity = await this.repository.findOneBy({ address });
            return entity ? address_mapper_1.addressMapper.toDomain(entity) : null;
        }
        const entity = await this.repository
            .createQueryBuilder('address')
            .where('LOWER(address.address) = LOWER(:address)', { address })
            .getOne();
        return entity ? address_mapper_1.addressMapper.toDomain(entity) : null;
    }
    async save(address) {
        const entity = await this.repository.save(address_mapper_1.addressMapper.toEntity(address));
        return address_mapper_1.addressMapper.toDomain(entity);
    }
    async update(address) {
        const entity = await this.repository.save(address_mapper_1.addressMapper.toEntity(address));
        return address_mapper_1.addressMapper.toDomain(entity);
    }
    async delete(id) {
        await this.repository.delete(id);
    }
}
exports.TypeOrmAddressRepository = TypeOrmAddressRepository;
//# sourceMappingURL=typeorm-address.repository.js.map