import { errors } from '@adonisjs/auth';
import { compose } from '@adonisjs/core/helpers';
import emitter from '@adonisjs/core/services/emitter';
import { AKModelService } from '../../crud/services/index.js';
import { AKAPIException } from '../../exceptions/api_exception.js';
export const AKUserService = (superclass) => {
    return class extends superclass {
        static createUniquenessViolationException = AKAPIException.auth.emailAlreadyExist;
        async signup(email, password, userData) {
            const data = {
                email: email.toLocaleLowerCase(),
                password,
                ...userData,
            };
            const user = await this.create(data);
            if (user)
                await emitter.emit('auth.signup', user);
            return user;
        }
        async login(email, password) {
            try {
                const user = await this.model.verifyCredentials(email.toLowerCase(), password);
                if (user.disabledAt)
                    throw AKAPIException.auth.userDisabled;
                else
                    return user;
            }
            catch (e) {
                if (e instanceof errors.E_INVALID_CREDENTIALS)
                    throw AKAPIException.auth.wrongCredentials;
                else
                    throw e;
            }
        }
        async findByEmail(email) {
            return await this.findBy('email', email);
        }
        async updatePassword(user, password) {
            user.password = password;
            await user.save();
        }
    };
};
export const userService = (m) => compose(AKModelService(m), AKUserService);
//# sourceMappingURL=user_service.js.map