import {LibraryAddCommand} from '../cmd/library';
import ParticleApi from '../cmd/api';
import settings from '../../settings';

import chalk from 'chalk';
import log from '../app/log';
import {spin} from '../app/ui';

import libraryInstall from './library_install';
import libraryMigrate from './library_migrate';
import libraryInit from './library_init';

//const ui = require('../cli/ui');

const apiJS = new ParticleApi(settings.apiUrl, {
	accessToken: settings.access_token
}).api;


export class CLILibraryAddCommandSite {
	constructor(argv) {
		[this.name, this.version='latest'] = argv.params.name.split('@');
		this.dir = argv.params.dir || process.cwd();
	}

	run(cmd) {
		return cmd.run(this, {
			name: this.name,
			version: this.version
		});
	}

	projectDir() {
		return this.dir;
	}

	fetchingLibrary(promise, name) {
		return spin(promise, `Adding library ${chalk.green(name)}`);
	}

	addedLibrary(name, version) {
		log.info(`Added library ${chalk.green(name)} ${version} to project`);
		return Promise.resolve();
	}
}

export default (app, cli) => {
	const lib = cli.createCategory(app, 'library', 'Manages firmware libraries', { alias: 'libraries' });

	libraryInit(lib, cli);
	libraryInstall(lib, cli);
	libraryMigrate(lib, cli);

	cli.createCommand(lib, 'add', 'Add a library to the current project.', {
		options: {},
		params: '<name>',

		handler: function libraryAddHandler(argv) {
			const apiClient = apiJS.client({ auth: settings.access_token });
			const site = new CLILibraryAddCommandSite(argv);
			const cmd = new LibraryAddCommand({ apiClient });
			return site.run(cmd);
		}
	});
	return lib;
};

@@ -390,99 +390,17 @@
 );%0A%0A
-const apiJS = new ParticleApi(settings.apiUrl, %7B%0A%09accessToken: settings.access_token%0A%7D).
+let 
 api
+JS
 ;%0A%0A%0A
@@ -979,16 +979,108 @@
 i) =%3E %7B%0A
+%09apiJS =  new ParticleApi(settings.apiUrl, %7B%0A%09%09accessToken: settings.access_token%0A%09%7D).api;%0A%0A
 %09const l

Create the API client later in the command implementation after settings are loaded.