{"size": 1807, "ext": "js", "max_stars_repo_path": "lib/settings.js", "max_stars_repo_name": "twynsicle/atom-titanium-fastbuild", "max_stars_repo_stars_event_min_datetime": "2018-11-20T10:11:37.000Z", "max_stars_repo_stars_event_max_datetime": "2018-11-20T10:11:37.000Z", "max_issues_repo_path": "lib/settings.js", "max_issues_repo_name": "twynsicle/atom-titanium-fastbuild", "max_issues_repo_issues_event_min_datetime": "2018-01-25T19:24:06.000Z", "max_issues_repo_issues_event_max_datetime": "2018-01-25T19:24:06.000Z", "max_forks_repo_path": "lib/settings.js", "max_forks_repo_name": "twynsicle/ti-fastbuild", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 21.2588235294, "max_line_length": 95, "alphanum_fraction": 0.7078029884}
/** @babel */

const { EventEmitter } = require('fbemitter');

const defaultSettings = {
	// build
	buildCommand: 'ti build',
	debugPortNumber: 55283,
	randomisePortNumber: true,
	enableFileWatcher: true,
	// debugger
	openDebugTools: true,
	skipFirstBreakPoint: true,
	// console
	showOnBuild: true,
	linewrap: false,
	clearBuildLogs: true,
	hideWebviewMessages: false,
	hideTimestamps: false
};

export const SETTINGS = {
	BUILD: {
		BUILD_COMMAND: 'buildCommand',
		DEBUG_PORT: 'debugPortNumber',
		RANDOMIZE_PORT_NUMBER: 'randomisePortNumber',
		ENABLE_FILE_WATCHER: 'enableFileWatcher'
	},
	DEBUGGER: {
		OPEN_DEBUG_TOOLS: 'openDebugTools',
		SKIP_FIRST_BREAKPOINT: 'skipFirstBreakPoint'
	},
	CONSOLE: {
		SHOW_ON_BUILD: 'showOnBuild',
		LINE_WRAP: 'linewrap',
		CLEAR_BUILD_LOGS: 'clearBuildLogs',
		HIDE_WEBVIEW_MESSAGES: 'hideWebviewMessages',
		HIDE_TIMESTAMPS: 'hideTimestamps'
	}
};

export default class Settings {

	constructor (state) {
		this.eventEmitter = new EventEmitter();
		this.loadSettings(defaultSettings, state);
	}

	// Builds settings object fresh so we drop old settings and properly incorporate new settings.
	loadSettings(defaultSettings, state) {
		this.settings = Object.assign({}, defaultSettings);
		if (!state) {
			return false;
		}
		for (let [ key, value ] of Object.entries(state)) {
			if (typeof this.settings[key] !== 'undefined') {
				this.settings[key] = value;
			}
		}
	}

	addListener(callback) {
		this.eventEmitter.addListener('changed', callback);
	}

	set(key, value) {
		this.settings[key] = value;
		this.eventEmitter.emit('changed', key);
	}

	get(key) {
		return this.settings[key];
	}

	getSettings() {
		return this.settings;
	}

	restoreDefaults() {
		for (let [ key, value ] of Object.entries(defaultSettings)) {
			this.set(key, value);
		}
	}
}
