import type { components } from '@crates-io/api-client';
import type { User } from '../models/index.js';

type ApiUser = components['schemas']['User'];
type ApiAuthenticatedUser = components['schemas']['AuthenticatedUser'];

export function serializeUser(user: User): ApiUser;
export function serializeUser(user: User, options: { removePrivateData?: true }): ApiUser;
export function serializeUser(user: User, options: { removePrivateData: false }): ApiAuthenticatedUser;
export function serializeUser(
  user: User,
  { removePrivateData = true }: { removePrivateData?: boolean } = {},
): ApiUser | ApiAuthenticatedUser {
  let serialized: ApiUser = {
    id: user.id,
    login: user.login,
    name: user.name,
    url: user.url,
    avatar: user.avatar,
  };

  if (!removePrivateData) {
    return {
      ...serialized,
      email: user.email,
      email_verified: user.emailVerified,
      email_verification_sent: user.emailVerified || Boolean(user.emailVerificationToken),
      is_admin: user.isAdmin,
      publish_notifications: user.publishNotifications,
    };
  }

  return serialized;
}
