"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InviteService = void 0;
const common_1 = require("@nestjs/common");
const event_emitter_1 = require("@nestjs/event-emitter");
const user_service_1 = require("../../user/services/user.service");
const tenant_service_1 = require("../../tenant/services/tenant.service");
const jwt_service_1 = require("../../core/services/jwt.service");
const auth_constants_1 = require("../../auth.constants");
const user_invited_event_1 = require("../events/user-invited.event");
const utils_1 = require("../../utils");
let InviteService = class InviteService {
    userService;
    tenantService;
    jwtService;
    eventEmitter;
    constructor(userService, tenantService, jwtService, eventEmitter) {
        this.userService = userService;
        this.tenantService = tenantService;
        this.jwtService = jwtService;
        this.eventEmitter = eventEmitter;
    }
    async inviteUser(input) {
        const email = input.email ? (0, utils_1.normalizedEmail)(input.email) : undefined;
        const phone = input.phone ? (0, utils_1.normalizedPhone)(input.phone) : undefined;
        if (!email && !phone) {
            throw new common_1.BadRequestException({
                message: 'Either email or phone is required',
                code: auth_constants_1.ERROR_CODES.EMAIL_OR_PHONE_REQUIRED,
            });
        }
        const tenantId = (await this.tenantService.resolveTenantId(input.tenantId ?? null)) ?? undefined;
        let user = email
            ? await this.userService.getUserByEmail(email, tenantId)
            : null;
        if (!user && phone) {
            user = await this.userService.getUserByPhone(phone, tenantId);
        }
        let isNewUser = false;
        if (!user) {
            user = await this.userService.createUser({ email, phone, isActive: true }, tenantId, input.metadata);
            isNewUser = true;
        }
        const token = await this.jwtService.generatePasswordResetToken({
            userId: user.id,
            passwordHashPrefix: '',
            type: 'password-reset',
            tenantId,
        });
        await this.eventEmitter.emitAsync(auth_constants_1.NestAuthEvents.USER_INVITED, new user_invited_event_1.UserInvitedEvent({
            user,
            tenantId,
            token,
            isNewUser,
            invitedBy: input.invitedBy,
            metadata: input.metadata,
        }));
        return { user, isNewUser };
    }
};
exports.InviteService = InviteService;
exports.InviteService = InviteService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [user_service_1.UserService,
        tenant_service_1.TenantService,
        jwt_service_1.JwtService,
        event_emitter_1.EventEmitter2])
], InviteService);
//# sourceMappingURL=invite.service.js.map