"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseAuthProvider = void 0;
const auth_config_service_1 = require("../services/auth-config.service");
class BaseAuthProvider {
    userRepository;
    authIdentityRepository;
    enabled;
    options;
    skipMfa = false;
    constructor(userRepository, authIdentityRepository) {
        this.userRepository = userRepository;
        this.authIdentityRepository = authIdentityRepository;
        this.options = auth_config_service_1.AuthConfigService.getOptions();
    }
    async linkToUser(userId, providerUserId, metadata) {
        const existingIdentity = await this.authIdentityRepository.findOne({
            where: {
                userId,
                provider: this.providerName,
                providerId: providerUserId,
            },
        });
        if (existingIdentity) {
            if (metadata && Object.keys(metadata).length > 0) {
                existingIdentity.metadata = { ...existingIdentity.metadata, ...metadata };
                await this.authIdentityRepository.save(existingIdentity);
            }
            return;
        }
        const identity = this.authIdentityRepository.create({
            userId,
            provider: this.providerName,
            providerId: providerUserId,
            metadata: metadata || {},
        });
        await this.authIdentityRepository.save(identity);
    }
    async findIdentityByUserId(userId) {
        return this.authIdentityRepository.findOne({
            where: {
                userId,
                provider: this.providerName,
            },
            relations: ['user'],
        });
    }
    async findIdentity(providerUserId) {
        return this.authIdentityRepository.findOne({
            where: {
                provider: this.providerName,
                providerId: providerUserId,
            },
            relations: ['user'],
        });
    }
    linkUserWith() {
        return 'email';
    }
}
exports.BaseAuthProvider = BaseAuthProvider;
//# sourceMappingURL=base-auth.provider.js.map