import dayjs from 'dayjs';

function getNullableText(value?: string | null) {
  return value?.trim() || null;
}

export function formatProfileDateTime(value?: string | number | null) {
  if (!value) {
    return '--';
  }

  return dayjs(value).format('YYYY-MM-DD HH:mm:ss');
}

export function resolveProfileRoleLabels(roles: Api.SystemManage.RoleSimple[]) {
  return roles.map(role => role.name?.trim() || role.code || role.id);
}

export function buildProfileUpdatePayload(form: Api.Auth.UpdateMyProfileParams): Api.Auth.UpdateMyProfileParams {
  return {
    nickname: getNullableText(form.nickname),
    email: getNullableText(form.email),
    mobile: getNullableText(form.mobile),
    sex: form.sex ?? null,
    avatar: getNullableText(form.avatar)
  };
}
