/* @flow */

import { CompositeDisposable } from 'atom'
import type { TextEditor } from 'atom'
import Editor from './editor'
import { $file, getEditorsMap, filterMessages } from './helpers'
import type { LinterMessage, MessagesPatch } from './types'

class Editors {
  editors: Set<Editor>
  messages: Array<LinterMessage>
  firstRender: boolean
  subscriptions: CompositeDisposable

  constructor() {
    this.editors = new Set()
    this.messages = []
    this.firstRender = true
    this.subscriptions = new CompositeDisposable()

    this.subscriptions.add(
      atom.workspace.observeTextEditors(textEditor => {
        this.getEditor(textEditor)
      }),
    )
    this.subscriptions.add(
      atom.workspace.getCenter().observeActivePaneItem(paneItem => {
        this.editors.forEach(editor => {
          if (editor.textEditor !== paneItem) {
            editor.removeTooltip()
          }
        })
      }),
    )
  }
  isFirstRender(): boolean {
    return this.firstRender
  }
  update({ messages, added, removed }: MessagesPatch) {
    this.messages = messages
    this.firstRender = false

    const { editorsMap, filePaths } = getEditorsMap(this)
    removed.forEach(function(message) {
      const filePath = $file(message)
      if (filePath && editorsMap[filePath]) {
        editorsMap[filePath].removed.push(message)
      }
    })
    if (Array.isArray(added)) {
      added.forEach(function(message) {
        const filePath = $file(message)
        if (filePath && editorsMap[filePath]) {
          editorsMap[filePath].added.push(message)
        }
      })
    }

    filePaths.forEach(function(filePath) {
      const value = editorsMap[filePath]
      if (value.added.length || value.removed.length) {
        value.editors.forEach(editor => editor.apply(value.added, value.removed))
      }
    })
  }
  getEditor(textEditor: TextEditor): Editor {
    for (const entry of this.editors) {
      if (entry.textEditor === textEditor) {
        return entry
      }
    }
    const editor = new Editor(textEditor)
    this.editors.add(editor)
    editor.onDidDestroy(() => {
      this.editors.delete(editor)
    })
    editor.subscriptions.add(
      textEditor.onDidChangePath(() => {
        editor.dispose()
        this.getEditor(textEditor)
      }),
    )
    editor.subscriptions.add(
      textEditor.onDidChangeGrammar(() => {
        editor.dispose()
        this.getEditor(textEditor)
      }),
    )
    editor.apply(filterMessages(this.messages, textEditor.getPath()), [])
    return editor
  }
  dispose() {
    for (const entry of this.editors) {
      entry.dispose()
    }
    this.subscriptions.dispose()
  }
}

module.exports = Editors

@@ -1164,21 +1164,19 @@
 is)%0A    
-remov
+add
 ed.forEa
@@ -1311,21 +1311,19 @@
 lePath%5D.
-remov
+add
 ed.push(
@@ -1354,45 +1354,13 @@
     
-if (Array.isArray(added)) %7B%0A      add
+remov
 ed.f
@@ -1382,26 +1382,24 @@
 (message) %7B%0A
-  
       const 
@@ -1426,26 +1426,24 @@
 sage)%0A      
- 
- 
 if (filePath
@@ -1474,26 +1474,24 @@
 ) %7B%0A        
-  
 editorsMap%5Bf
@@ -1491,35 +1491,37 @@
 rsMap%5BfilePath%5D.
-add
+remov
 ed.push(message)
@@ -1531,26 +1531,16 @@
     
-  
 %7D%0A    
- 
- 
 %7D)
-%0A    %7D
 %0A%0A  

Revert "fix: add isArray check for added"