let census;
let db;

let f = 0;
let industries;

function countField(db, field) {
	let dictionary = [];
	let c = 0;

	for (var i = 0; i < db.length; i++) {
		industries = db[i][field];
		categories = industries.split("|");
		for (var j = 0; j < categories.length; j++) {
			if (!(categories[j] in dictionary) ) {
				dictionary[categories[j]] = 0;
				c++;
			} else {
				dictionary[categories[j]] += 1;
			}
		}
	}
	dictionary.length = c;
	return dictionary;

}

let s;

function salaryToNumber(str) {

	let final = 0;
	str = str.replaceAll(",", "");
	s = str;
	if (str.indexOf(" to ") != -1) {
		intervals = str.split(" to ");
		intervals[0] = parseInt(intervals[0].substr(1, intervals[0].length));
		intervals[1] = parseInt(intervals[1].substr(1, intervals[1].length));

		final = (intervals[0] + intervals[1]) /2;

	} else if (str.substr(str.length-1, str.length) == "+") {
		final = 200000;
	} else if (str.substr(0, 4) == "Less") {
		final = 25000;
	}

	return final;
}

let dictionary = [];
	let count_salary = [];
	let count;
let avg;

function randomInterval(min, max) {
	return Math.floor(Math.random() * (max - min + 1) + min); 
}

function averageSalary(db) {
	dictionary = [];
	count_salary = [];
	let c = 0;

	for (var i = 0; i < db.length; i++) {
		hours = db[i]["work_hours"];
		if (!(hours in dictionary) ) {
			dictionary[hours] = 0;
			count_salary[hours] = 0;
			c++;
		} else {
			dictionary[hours] += salaryToNumber(db[i]["salary"]);
			count_salary[hours] += 1;
		}
		
	}
	dictionary.length = c;


	console.log(dictionary);

	for (var i = 0; i < dictionary.length; i++) {
		console.log(dictionary[Object.keys(dictionary)[i]]);
		dictionary[Object.keys(dictionary)[i]] = dictionary[Object.keys(dictionary)[i]] / count_salary[Object.keys(dictionary)[i]];
	}

	console.log(dictionary);

	return dictionary;

}

function groupToDictionary(field) {
	group = d3.group(db, d => d[field]);
	n = 0;
	dic = [];

	group.forEach(function(d, i) {
		dic[n] = i; 
		n++;
	})

	return dic;
}

function groupToDictionaryKeys(field) {
	group = d3.group(db, d => d[field]);
	n = 0;
	dic = {};

	group.forEach(function(d, i) {
		dic[i] = []; 
		n++;
	})

	return dic;
}

function initializeWith(arr, with_arr) {
	console.log(arr.length);
	cleanArr = {};
	for (var i = arr.length - 1; i >= 0; i--) {
		cleanArr[arr[i]] = with_arr;
	}
	return cleanArr;
}

let line;

d3.csv("design_census.csv").then(function(data) {

	db = data;

	canvas_01();

	canvas_02();

	canvas_03();



})