 # slog = () -> null
 
 
-
-spy.resolveCb = (data) ->
-    window.d = data
-    console.log 'Resolved'
-    console.log data
-
-spy.rejectCb = (error) ->
-    window.e = error
-    console.log 'Rejected'
-    console.log error
-
-import { textExtensions }            from './text-extensions.coffee'
 import { welcomeTaskpaper }          from './welcome-taskpaper.coffee'
-
 import { DockPanel }                 from 'phosphor-dockpanel'
 import { CodeMirrorWidget }          from 'phosphor-codemirror'
 

        expose.amplify = amplify

        spy.parseQueryString = parseQueryString
        spy.ensureDropboxToken = ensureDropboxToken

        spy.hashKeys = hashKeys
        spy.path = path
        spy.dbx = dbx
        spy.dropboxAccessDenied = dropboxAccessDenied

    loadShebang = (string) ->
        hashView = new HashView(syncMaster)
        syncMaster.update(decodeURIComponent(string))
        amplify.publish 'outline-ready'
        spy.hashView = hashView
        exposeVariables()
        return

    initPanel()
    if path[0] is '!'
        loadShebang(path[1...])
        return


    switch path
        when 'WELCOME'
            loadShebang(welcomeTaskpaper)
        when 'BLANK', 'NEW', 'DEMO'
            loadShebang('')
        when 'INCEPTION'
            loadShebang(inceptionTour)
        when 'CHOOSE'
            try
                dropboxChooser()
            catch e
                log """
                    ATTENTION: Dropbox chooser was blocked by the browser.
                    Click this link to launch manually:
                    #{openDropboxChooserLink}

                    """
        else  # Dropbox
            afterDropbox = (path) ->
                promise = loadDropboxPath(path)
                promise.then (data) ->
                    doc.setValue(data.text)
                    dropboxView = new DropboxView(syncMaster, path)
                    dropboxView.data = data.text
                    dropboxView.rev = data.rev

                    spy.dropboxView = dropboxView

                promise.catch (error) ->
                    slog 'ERROR'

            # Shared link
            if ///^https?://www.dropbox.com/s///.test(path)
                p1 = dbx.sharingGetSharedLinkFile options =
                    url: path
                p1.then (fileData) ->
                    window.supressReload = true
                    window.location.hash = fileData.path_lower
                    afterDropbox(fileData.path_lower)
                p1.catch (error) ->
                    slog 'Error @sharingGetSharedLinkFile'
                    slog error
                    ensureDropboxToken(path).then () ->
                        log "ERROR: #{error.error} (Status: #{error.status})"
                        history.pushState(null, null, "#BLANK")
            else
                afterDropbox(path)


    exposeVariables()







window.onload = main

window.onhashchange = (e) ->
    slog "onhashchange: #{location.hash}"
    slog "newURL: #{e.newURL}"
    slog "oldURL: #{e.oldURL}"

    reload = false

    reloadWhiteList = ///
        ^WELCOME$
       |^NEW$
       |^DEMO$
       |^BLANK$
       |^INCEPTION$
       |^CHOOSE$
       |^/
       |^http
    ///i



    hash = location.hash[1...]
    reload ||= reloadWhiteList.test(hash)

    if reload and not window.supressReload
        # slog "loading: #{e.newURL}"
        slog "supressReload", window.supressReload
        window.location.reload()
    else
        # slog "skipping: #{e.newURL}"
        window.supressReload = false




        # Initialize outline
        outline = new birch.Outline.createTaskPaperOutline("")
        outline.onDidEndChanges () =>
            @update(@outline.serialize())

        expose.outline = outline


        panel = new DockPanel()
        panel.id = 'main'
