/** @babel */

import {Disposable, CompositeDisposable} from 'atom'
import VIEW_URI from './view-uri'

let disposables = null

export function activate () {
  disposables = new CompositeDisposable()

  disposables.add(atom.workspace.addOpener((uri) => {
    if (uri === VIEW_URI) {
      return deserializeIncompatiblePackagesComponent()
    }
  }))

  disposables.add(atom.commands.add('atom-workspace', {
    'incompatible-packages:view': () => {
      atom.workspace.open(VIEW_URI)
    }
  }))
}

export function deactivate () {
  disposables.dispose()
}

export function consumeStatusBar (statusBar) {
  let incompatibleCount = 0
  for (let pack of atom.packages.getLoadedPackages()) {
    if (!pack.isCompatible()) incompatibleCount++
  }

  if (incompatibleCount > 0) {
    let icon = createIcon(incompatibleCount)
    let tile = statusBar.addRightTile({item: icon, priority: 200})
    icon.element.addEventListener('click', () => {
      atom.commands.dispatch(icon.element, 'incompatible-packages:view')
    })
    disposables.add(new Disposable(() => tile.destroy()))
  }
}

export function deserializeIncompatiblePackagesComponent () {
  const IncompatiblePackagesComponent = require('./incompatible-packages-component')
  return new IncompatiblePackagesComponent(atom.packages)
}

function createIcon (count) {
  const StatusIconComponent = require('./status-icon-component')
  return new StatusIconComponent({count})
}

@@ -1420,12 +1420,191 @@
 (%7Bcount%7D)%0A%7D%0A
+%0Aif (parseFloat(atom.getVersion()) %3C 1.7) %7B%0A  atom.deserializers.add(%7B%0A    name: 'IncompatiblePackagesComponent',%0A    deserialize: deserializeIncompatiblePackagesComponent%0A  %7D)%0A%7D%0A

Add deserializers.add call for backwards compatibility