'use babel'
import {focusEditorPane} from './pane-item'
import {existsSync} from 'fs'

let remoteFileOpener = undefined
let allowremote = false

export function open (pathOrId, line, {pending = false, existingOnly = true, remote = false} = {}) {
  focusEditorPane()
  let id = getUntitledId(pathOrId)

  if (id) {
    return openEditorById(id, line)
  } else if (allowremote && remoteFileOpener) {
    return new Promise((resolve, reject) => {
      let disposable = atom.workspace.observeActiveTextEditor(ed => {
        if (ed) {
          let ep = ed.getPath()
          if (ep) {
            ep = ep.replace(/\\/g, '/')
            if (ep.indexOf(pathOrId) > -1) {
              ed.setCursorBufferPosition([line, 0])
              if (disposable) disposable.dispose()
            }
          }
        }
      })
      if (remoteFileOpener(pathOrId)) {
        resolve(true)
      } else {
        resolve(false)
        if (disposable) disposable.dispose()
      }
    })
  } else if (!existingOnly || existsSync(pathOrId)) {
    return atom.workspace.open(pathOrId, {initialLine: line, searchAllPanes: true, pending})
  } else {
    return Promise.resolve(false)
  }
}

export function isUntitled(pathOrId) {
  return !!getUntitledId(pathOrId)
}

export function getUntitledId (file) {
  const id = file.match(/untitled-([\d\w]+)$/)
  return id != null ? id[1] : false
}

export function consumeRemoteFileOpener (o) {
  remoteFileOpener = o
}

export function allowRemoteFiles (val) {
  allowremote = val
}

function openEditorById (id, line) {
  return new Promise((resolve) => {
    for (const pane of atom.workspace.getPanes()) {
      // handle docks properly:
      let items
      if (pane.getItems == null) {
        if (pane.getPaneItems == null) {
          continue
        } else {
          items = pane.getPaneItems()
        }
      } else {
        items = pane.getItems()
      }
      for (const item of items) {
        if (item.constructor.name === "TextEditor" && item.getBuffer().id === id) {
          pane.setActiveItem(item)
          item.setCursorBufferPosition([line, 0])
          item.scrollToCursorPosition()
          resolve(item)
        }
      }
    }
    resolve(false)
  })
}

@@ -78,16 +78,47 @@
 rom 'fs'
+%0Aimport %7Bnormalize%7D from 'path'
 %0A%0Alet re
@@ -675,16 +675,26 @@
 indexOf(
+normalize(
 pathOrId
@@ -694,16 +694,17 @@
 athOrId)
+)
  %3E -1) %7B

normalize path when opening remote files