// @flow
import {AsyncStorage} from 'react-native'

export function clearAsyncStorage() {
  return AsyncStorage.clear()
}

/// MARK: Utilities

function setItem(key: string, value: any) {
  return AsyncStorage.setItem(`aao:${key}`, JSON.stringify(value))
}
function getItem(key: string): Promise<any> {
  return AsyncStorage.getItem(`aao:${key}`).then(stored => JSON.parse(stored))
}
function removeItem(key: string): Promise<any> {
  return AsyncStorage.removeItem(`aao:${key}`)
}

/// MARK: Settings

const analyticsOptOutKey = 'settings:opt-out'
export function setAnalyticsOptOut(status: boolean) {
  return setItem(analyticsOptOutKey, status)
}
export function getAnalyticsOptOut(): Promise<boolean> {
  return getItem(analyticsOptOutKey)
}

const homescreenOrderKey = 'homescreen:view-order'
export function setHomescreenOrder(order: string[]) {
  return setItem(homescreenOrderKey, order)
}
export function getHomescreenOrder(): Promise<string[]> {
  return getItem(homescreenOrderKey)
}

const acknowledgementStatusKey = 'settings:ackd'
export function setAcknowledgementStatus(status: boolean) {
  return setItem(acknowledgementStatusKey, status)
}
export function getAcknowledgementStatus(): Promise<?boolean> {
  return getItem(acknowledgementStatusKey)
}

/// MARK: Credentials

const tokenValidKey = 'credentials:valid'
export function setTokenValid(valid: boolean) {
  return setItem(tokenValidKey, valid)
}
export function getTokenValid(): Promise<boolean> {
  return getItem(tokenValidKey)
}
export function clearTokenValid(): Promise<any> {
  return removeItem(tokenValidKey)
}

const credentialsValidKey = 'olafCredentials:valid'
export function setCredentialsValid(valid: boolean) {
  return setItem(credentialsValidKey, valid)
}
export function getCredentialsValid(): Promise<boolean> {
  return getItem(credentialsValidKey)
}

@@ -174,19 +174,21 @@
  value: 
-any
+mixed
 ) %7B%0A  re
@@ -283,32 +283,33 @@
 tring): Promise%3C
+?
 any%3E %7B%0A  return 
@@ -414,35 +414,36 @@
 tring): Promise%3C
-any
+void
 %3E %7B%0A  return Asy
@@ -476,24 +476,357 @@
 $%7Bkey%7D%60)%0A%7D%0A%0A
+/// MARK: Typed utility functions%0A// These simply cast the return value of getItem; they provide no runtime%0A// guarantees.%0A%0Aasync function getItemAsBoolean(key: string): Promise%3Cboolean%3E %7B%0A  return (await getItem(key)) %7C%7C false%0A%7D%0Aasync function getItemAsArray(key: string): Promise%3CArray%3C*%3E%3E %7B%0A  return (await getItem(key)) %7C%7C %5B%5D%0A%7D%0A%0A
 /// MARK: Se
@@ -1049,24 +1049,33 @@
 turn getItem
+AsBoolean
 (analyticsOp
@@ -1283,24 +1283,29 @@
 Promise%3C
+Array%3C
 string
-%5B%5D
+%3E
 %3E %7B%0A  re
@@ -1312,24 +1312,31 @@
 turn getItem
+AsArray
 (homescreenO
@@ -1561,17 +1561,16 @@
 Promise%3C
-?
 boolean%3E
@@ -1584,24 +1584,33 @@
 turn getItem
+AsBoolean
 (acknowledge
@@ -1845,24 +1845,33 @@
 turn getItem
+AsBoolean
 (tokenValidK
@@ -2187,24 +2187,33 @@
 turn getItem
+AsBoolean
 (credentials

enforce types for storage a bit more