"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TypeOrmWalletRepository = void 0;
const wallet_entity_1 = require("../entities/wallet.entity");
const data_source_1 = require("../data-source");
const wallet_mapper_1 = require("./mappers/wallet.mapper");
class TypeOrmWalletRepository {
    get repository() {
        return data_source_1.AppDataSource.getRepository(wallet_entity_1.WalletEntity);
    }
    async findById(id) {
        const entity = await this.repository.findOneBy({ id });
        return entity ? wallet_mapper_1.walletMapper.toDomain(entity) : null;
    }
    async findByDeviceId(deviceId) {
        const entities = await this.repository
            .createQueryBuilder('wallet')
            .where("JSON_CONTAINS(wallet.deviceIds, :deviceId, '$')", {
            deviceId: JSON.stringify(deviceId),
        })
            .getMany();
        return entities.map(wallet_mapper_1.walletMapper.toDomain);
    }
    async findByDeviceIdAndName(deviceId, name) {
        const entity = await this.repository
            .createQueryBuilder('wallet')
            .where("JSON_CONTAINS(wallet.deviceIds, :deviceId, '$')", {
            deviceId: JSON.stringify(deviceId),
        })
            .andWhere('wallet.name = :name', { name })
            .getOne();
        return entity ? wallet_mapper_1.walletMapper.toDomain(entity) : null;
    }
    async save(wallet) {
        const entity = await this.repository.save(wallet_mapper_1.walletMapper.toEntity(wallet));
        return wallet_mapper_1.walletMapper.toDomain(entity);
    }
    async update(wallet) {
        const entity = await this.repository.save(wallet_mapper_1.walletMapper.toEntity(wallet));
        return wallet_mapper_1.walletMapper.toDomain(entity);
    }
    async delete(id) {
        await this.repository.delete(id);
    }
}
exports.TypeOrmWalletRepository = TypeOrmWalletRepository;
//# sourceMappingURL=typeorm-wallet.repository.js.map