"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Auth0Client = void 0;
const auth0_1 = require("auth0");
const generate_password_1 = __importDefault(require("generate-password"));
const envs_1 = require("../envs");
class Auth0Client {
    constructor() {
        this.authConnectionName =
            process.env.AUTH0_CONNECTION_NAME || 'Username-Password-Authentication';
        this.authDomain = (0, envs_1.maybeGetEnvParam)(envs_1.EnvVar.AUTH0_DOMAIN);
        this.authOrgsUserClientId = (0, envs_1.maybeGetEnvParam)(envs_1.EnvVar.AUTH0_ORGS_USERS_CLIENT_ID);
        this.authOrgsUsersClientSecret = (0, envs_1.maybeGetEnvParam)(envs_1.EnvVar.AUTH0_ORGS_USERS_CLIENT_SECRET);
        this.managementClient = new auth0_1.ManagementClient({
            domain: this.authDomain,
            clientId: this.authOrgsUserClientId,
            clientSecret: this.authOrgsUsersClientSecret,
            scope: 'read:users update:users create:user_tickets',
        });
        this.authClient = new auth0_1.AuthenticationClient({
            domain: this.authDomain,
            clientId: this.authOrgsUserClientId,
            clientSecret: this.authOrgsUsersClientSecret,
        });
    }
    async createUser(args, options = {
        sendEmailVerification: false,
    }) {
        const { email, attributes } = args;
        const { sendEmailVerification } = options;
        const password = args.password ||
            generate_password_1.default.generate({
                length: 10,
                numbers: true,
                symbols: true,
            });
        const result = await this.managementClient.createUser({
            connection: this.authConnectionName,
            email,
            app_metadata: attributes,
            email_verified: false,
            verify_email: !!sendEmailVerification,
            password,
        });
        return result.user_id;
    }
    async createPasswordChangeTicket(userId) {
        const result = await this.managementClient.createPasswordChangeTicket({
            user_id: userId,
            mark_email_as_verified: true,
            ttl_sec: 3600,
        });
        return result.ticket;
    }
    async sendPasswordResetEmail(email) {
        const result = await this.authClient.requestChangePasswordEmail({
            email,
            connection: this.authConnectionName,
        });
        return result;
    }
}
exports.Auth0Client = Auth0Client;
//# sourceMappingURL=Auth0Client.js.map