import { getIdentity } from '../auth/auth.ic';
import { initUserActor } from '../../utils/user.utils';
import { toNullable, toTimestamp, toUserSocial } from '../../utils/did.utils';
export const updateUser = async (user) => {
  const identity = getIdentity();
  if (!identity) {
    return;
  }
  const { userActor, ownerId } = await initUserActor({ identity });
  const { data } = user;
  const { name, email, photo_url, newsletter, bio, social, created_at, username } = data;
  const now = new Date();
  const updateUser = {
    userId: ownerId,
    data: {
      name: toNullable(name),
      username: toNullable(username),
      bio: toNullable(bio),
      photo_url: toNullable(photo_url),
      email: toNullable(email),
      newsletter: toNullable(newsletter),
      social: toUserSocial(social),
      created_at: toTimestamp(created_at),
      updated_at: toTimestamp(now)
    }
  };
  console.log('User IC about to SET', updateUser);
  const t0 = performance.now();
  await userActor.set(updateUser);
  const t1 = performance.now();
  console.log('User IC SET done', t1 - t0);
  return {
    id: user.id,
    data: Object.assign(Object.assign({}, data), { updated_at: now })
  };
};
export const deleteUser = async (_userId) => {
  const identity = getIdentity();
  if (!identity) {
    return;
  }
  const { userActor, ownerId } = await initUserActor({ identity });
  console.log('User IC about to DEL');
  const t0 = performance.now();
  await userActor.del(ownerId);
  const t1 = performance.now();
  console.log('User IC DEL done', t1 - t0);
};
