
import {
  asArray,
  asDate,
  asNumber,
  asObject,
  asOptional,
  asString,
  asValue,
  uncleaner
} from 'cleaners'

import {
  asBase32,
  asBase64,
  asEdgeBox,
  asEdgeKeyBox,
  asEdgeSnrp,
  asRecovery2Auth
} from './server-cleaners'


















































































export const asEdgeRepoDump = asObject(asEdgeBox)

export const asEdgeVoucherDump = asObject({
  // Identity:
  loginId: asBase64,
  voucherAuth: asBase64,
  voucherId: asString,

  // Login capability:
  created: asDate,
  activates: asDate, // Automatically becomes approved on this date
  status: asValue('pending', 'approved', 'rejected'),

  // Information about the login:
  ip: asString,
  ipDescription: asString,
  deviceDescription: asOptional(asString)
})

export const asEdgeLoginDump = asObject({
  // Identity:
  appId: asString,
  created: asOptional(asDate, () => new Date()),
  loginId: asBase64,

  // Nested logins:
  children: asOptional(
    asArray(raw => asEdgeLoginDump(raw)),
    () => []
  ),
  parentBox: asOptional(asEdgeBox),
  parentId: () => undefined,

  // 2-factor login:
  otpKey: asOptional(asBase32),
  otpResetAuth: asOptional(asString),
  otpResetDate: asOptional(asDate),
  otpTimeout: asOptional(asNumber),

  // Password login:
  passwordAuth: asOptional(asBase64),
  passwordAuthBox: asOptional(asEdgeBox),
  passwordAuthSnrp: asOptional(asEdgeSnrp),
  passwordBox: asOptional(asEdgeBox),
  passwordKeySnrp: asOptional(asEdgeSnrp),

  // PIN v2 login:
  pin2Id: asOptional(asBase64),
  pin2Auth: asOptional(asBase64),
  pin2Box: asOptional(asEdgeBox),
  pin2KeyBox: asOptional(asEdgeBox),
  pin2TextBox: asOptional(asEdgeBox),

  // Recovery v2 login:
  recovery2Id: asOptional(asBase64),
  recovery2Auth: asOptional(asRecovery2Auth),
  question2Box: asOptional(asEdgeBox),
  recovery2Box: asOptional(asEdgeBox),
  recovery2KeyBox: asOptional(asEdgeBox),

  // Secret-key login:
  loginAuth: asOptional(asBase64),
  loginAuthBox: asOptional(asEdgeBox),

  // Username:
  userId: asOptional(asBase64),
  userTextBox: asOptional(asEdgeBox),

  // Keys and assorted goodies:
  keyBoxes: asOptional(asArray(asEdgeKeyBox), () => []),
  mnemonicBox: asOptional(asEdgeBox),
  rootKeyBox: asOptional(asEdgeBox),
  syncKeyBox: asOptional(asEdgeBox),
  vouchers: asOptional(asArray(asEdgeVoucherDump), () => []),

  // Obsolete:
  pinBox: asOptional(asEdgeBox),
  pinId: asOptional(asString),
  pinKeyBox: asOptional(asEdgeBox)
})

export const wasEdgeLoginDump = uncleaner(asEdgeLoginDump)
export const wasEdgeRepoDump = uncleaner(asEdgeRepoDump)
