"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TypeOrmTokenRepository = void 0;
const typeorm_1 = require("typeorm");
const token_entity_1 = require("../entities/token.entity");
const data_source_1 = require("../data-source");
const token_mapper_1 = require("./mappers/token.mapper");
class TypeOrmTokenRepository {
    get repository() {
        return data_source_1.AppDataSource.getRepository(token_entity_1.TokenEntity);
    }
    async findById(id) {
        const entity = await this.repository.findOneBy({ id });
        return entity ? token_mapper_1.tokenMapper.toDomain(entity) : null;
    }
    async findBySymbol(symbol) {
        const entity = await this.repository.findOneBy({ symbol });
        return entity ? token_mapper_1.tokenMapper.toDomain(entity) : null;
    }
    async findByNetworkId(networkId) {
        const entities = await this.repository.find({
            where: { networkId },
            order: { createdAt: 'DESC' },
        });
        return entities.map(token_mapper_1.tokenMapper.toDomain);
    }
    async findByCode(code) {
        const entity = await this.repository.findOneBy({ code });
        return entity ? token_mapper_1.tokenMapper.toDomain(entity) : null;
    }
    async findByAddressAndNetworkId(address, networkId) {
        const entity = await this.repository.findOneBy({
            address: (0, typeorm_1.ILike)(address),
            networkId,
        });
        return entity ? token_mapper_1.tokenMapper.toDomain(entity) : null;
    }
    async findActive(options) {
        const entities = await this.repository.find({
            where: { isActive: true },
            take: options?.limit,
            skip: options?.offset,
            order: { createdAt: 'DESC' },
        });
        return entities.map(token_mapper_1.tokenMapper.toDomain);
    }
    async findAll(options) {
        const entities = await this.repository.find({
            take: options?.limit,
            skip: options?.offset,
            order: { createdAt: 'DESC' },
        });
        return entities.map(token_mapper_1.tokenMapper.toDomain);
    }
    async save(token) {
        const entity = await this.repository.save(token_mapper_1.tokenMapper.toEntity(token));
        return token_mapper_1.tokenMapper.toDomain(entity);
    }
    async update(token) {
        const entity = await this.repository.save(token_mapper_1.tokenMapper.toEntity(token));
        return token_mapper_1.tokenMapper.toDomain(entity);
    }
    async delete(id) {
        await this.repository.delete(id);
    }
}
exports.TypeOrmTokenRepository = TypeOrmTokenRepository;
//# sourceMappingURL=typeorm-token.repository.js.map