import { ValidationError } from '../../../errors/index.js';
import { generatePasswordSaltHash } from './generatePasswordSaltHash.js';
export const registerLocalStrategy = async ({ collection, doc, password, payload, req })=>{
    const existingUser = await payload.find({
        collection: collection.slug,
        depth: 0,
        where: {
            email: {
                equals: doc.email
            }
        }
    });
    if (existingUser.docs.length > 0) {
        throw new ValidationError([
            {
                field: 'email',
                message: 'A user with the given email is already registered'
            }
        ]);
    }
    const { hash, salt } = await generatePasswordSaltHash({
        password
    });
    const sanitizedDoc = {
        ...doc
    };
    if (sanitizedDoc.password) delete sanitizedDoc.password;
    return payload.db.create({
        collection: collection.slug,
        data: {
            ...sanitizedDoc,
            hash,
            salt
        },
        req
    });
};

//# sourceMappingURL=register.js.map