import slug from 'slug';

AccountsTemplates.configure({
	defaultLayoutType: 'blaze-to-react',
	defaultLayout: MainLayout,
	defaultContentRegion: 'content',
	confirmPassword: true,
	enablePasswordChange: true,
	forbidClientAccountCreation: false,
	overrideLoginErrors: true,
	sendVerificationEmail: true,
	lowercaseUsername: false,
	showAddRemoveServices: true,
	showForgotPasswordLink: true,
	showLabels: true,
	showPlaceholders: true,
	showResendVerificationEmailLink: true,
	continuousValidation: false,
	negativeFeedback: false,
	negativeValidation: true,
	positiveValidation: false,
	positiveFeedback: true,
	showValidating: true,
	privacyUrl: Config.privacyUrl || null,
	termsUrl: Config.termsUrl || null,
});

/*
 * Account routes
 */

AccountsTemplates.configureRoute('changePwd');
AccountsTemplates.configureRoute('forgotPwd');
AccountsTemplates.configureRoute('resetPwd');
AccountsTemplates.configureRoute('signIn');
AccountsTemplates.configureRoute('signUp');
AccountsTemplates.configureRoute('verifyEmail');
AccountsTemplates.configureRoute('resendVerificationEmail');


if (Meteor.isServer) {
	Accounts.onCreateUser((options, user) => {
		let username = '';
		if (!('emails' in user)) {
			user.emails = [];
		}

		if ('facebook' in user.services) {
			username = slug(user.services.facebook.name);
			user.emails.push({
				address: user.services.facebook.email,
				verified: true,
			});
		}
		if ('google' in user.services) {
			username = slug(user.services.google.name);
			user.emails.push({
				address: user.services.google.email,
				verified: true,
			}); }
		if ('twitter' in user.services) {
			username = slug(user.services.twitter.screenName);
		}

		user.username = username;

		check(user, Schemas.User);

		return user;
	});
}

@@ -1218,16 +1218,107 @@
 %5D;%0A%09%09%7D%0A%0A
+%09%09if (user.emails.length) %7B%0A%09%09%09username = slug(user.emails%5B0%5D.address.split('@')%5B0%5D);%0A%09%09%7D%0A%0A
 %09%09if ('f

set default username from first half of email address