client_id = 'aed0aa63baf747a83ad0b6b246acddca'
redirect_uri = "#{window.location.origin}/connect/soundcloud"

lsAuthKey = 'wdtc-sc-at'
loadSDK = -> $.getScript "//connect.soundcloud.com/sdk-2.0.0.js"

initPromise = null
initSDK = ->
	initPromise or= loadSDK().then( -> SC.initialize {client_id, redirect_uri}).then ->
		callSC = (method, args...) -> SC[method] args...
		for own key, _ of SC when !sdk[key] and typeof SC[key] is 'function'
			sdk[key] = callSC.bind SC, key

		SC.accessToken key if (key = localStorage.getItem lsAuthKey)?

module.exports = sdk = 
	ready: initSDK

	connect: (callback) ->
		SC.connect ->
			localStorage.setItem lsAuthKey, SC.accessToken()
			callback()

	disconnect: ->
		SC.disconnect()
		localStorage.removeItem lsAuthKey

	clientId: -> client_id