'use strict';
var fixmyjs = require('fixmyjs');
var jshintCli = require('jshint/src/cli');
var plugin = module.exports;

function run() {
	var editor = atom.workspace.getActiveEditor();

	if (!editor) {
		return;
	}

	var retText = '';
	var file = editor.getUri();
	var config = jshintCli.getConfig(file);
	var selectedText = editor.getSelectedText();
	var text = selectedText || editor.getText();

	try {
		if (atom.config.get('fixmyjs.legacy')) {
			var jshint = require('jshint').JSHINT;
			jshint(text, config);
			retText = fixmyjs(jshint.data(), text).run();
		} else {
			retText = fixmyjs.fix(text, config);
		}
	} catch (err) {
		console.error(err);
		atom.beep();
		return;
	}

	var cursorPosition = editor.getCursorBufferPosition();

	if (selectedText) {
		editor.setTextInBufferRange(editor.getSelectedBufferRange(), retText);
	} else {
		editor.setText(retText);
	}

	editor.setCursorBufferPosition(cursorPosition);
}

plugin.configDefaults = {
	legacy: true
};

plugin.activate = function () {
	return atom.workspaceView.command('FixMyJS', run);
};

@@ -271,16 +271,23 @@
 config =
+ file ?
  jshintC
@@ -304,16 +304,21 @@
 ig(file)
+ : %7B%7D
 ;%0A%09var s

use the default config on new unsaved files - fixes #4