import firebase from 'firebase/app';
import 'firebase/firestore';
export const createUser = (authUser) => {
  return new Promise(async (resolve, reject) => {
    if (!authUser || !authUser.uid) {
      reject('Authentication user not defined.');
      return;
    }
    try {
      const firestore = firebase.firestore();
      const snapshot = await firestore.collection('users').doc(authUser.uid).get();
      if (!snapshot.exists) {
        const user = await createUserData(authUser);
        resolve(Object.assign({}, user));
        return;
      }
      const user = snapshot.data();
      const updatedUser = await updateUserWithAuthData(authUser, user);
      resolve({
        id: authUser.uid,
        data: updatedUser
      });
    }
    catch (err) {
      reject(err);
    }
  });
};
export const updateUser = (user) => {
  return new Promise(async (resolve, reject) => {
    const firestore = firebase.firestore();
    const now = firebase.firestore.Timestamp.now();
    user.data.updated_at = now;
    // We delete username and apiUserId as we do not want to save these information in Firestore
    const data = Object.assign({}, user.data);
    delete data.username;
    delete data.apiUserId;
    try {
      await firestore.collection('users').doc(user.id).set(data, { merge: true });
      resolve(Object.assign({}, user));
    }
    catch (err) {
      reject(err);
    }
  });
};
export const deleteUser = (userId) => {
  return new Promise(async (resolve, reject) => {
    try {
      const firestore = firebase.firestore();
      await firestore.collection('users').doc(userId).delete();
      resolve();
    }
    catch (err) {
      reject(err);
    }
  });
};
const createUserData = (authUser) => {
  return new Promise(async (resolve, reject) => {
    try {
      const firestore = firebase.firestore();
      const now = firebase.firestore.Timestamp.now();
      const user = {
        newsletter: true,
        created_at: now,
        updated_at: now
      };
      if (authUser.name) {
        user.name = authUser.name;
      }
      if (authUser.email) {
        user.email = authUser.email;
        user.newsletter = true;
      }
      if (authUser.photo_url) {
        user.photo_url = authUser.photo_url;
      }
      await firestore.collection('users').doc(authUser.uid).set(user, { merge: true });
      resolve({
        id: authUser.uid,
        data: user
      });
    }
    catch (err) {
      reject(err);
    }
  });
};
const updateUserWithAuthData = (authUser, user) => {
  return new Promise(async (resolve, reject) => {
    try {
      if (userNeedUpdate(authUser, user)) {
        const firestore = firebase.firestore();
        if (!user.name && authUser.name) {
          user.name = authUser.name;
        }
        if (!user.email && authUser.email) {
          user.email = authUser.email;
        }
        if (!user.photo_url && authUser.photo_url) {
          user.photo_url = authUser.photo_url;
        }
        user.updated_at = firebase.firestore.Timestamp.now();
        await firestore.collection('users').doc(authUser.uid).set(user, { merge: true });
      }
      resolve(user);
    }
    catch (err) {
      reject(err);
    }
  });
};
const userNeedUpdate = (authUser, user) => {
  if (!user.name && authUser.name) {
    return true;
  }
  else if (!user.email && authUser.email) {
    return true;
  }
  else if (!user.photo_url && authUser.photo_url) {
    return true;
  }
  else {
    return false;
  }
};
