import { asArray, asOptional, asString, asTuple, asValue } from 'cleaners'

import { makeRpcProtocol } from '../../util/json-rpc'

/**
 * A chain and address identifier, like `['bitcoin', '19z88q...']`
 */











const asSubscribeParams = asTuple(
  asString, // pluginId
  asString, // address
  asOptional(asString) // checkpoint
)


const asSubscribeResult = asValue(
  /** Subscribe failed; not supported */
  -1,
  /** Subscribe failed; some thing went wrong */
  0,
  /** Subscribe succeeded, no changes */
  1,
  /** Subscribed succeeded, changes present */
  2
)

export const changeProtocol = makeRpcProtocol({
  serverMethods: {
    subscribe: {
      asParams: asArray(asSubscribeParams),
      asResult: asArray(asSubscribeResult)
    },

    unsubscribe: {
      asParams: asArray(asSubscribeParams),
      asResult: asValue(undefined)
    }
  },

  clientMethods: {
    update: {
      asParams: asSubscribeParams
    },
    subLost: {
      asParams: asSubscribeParams
    }
  }
})
