states.push({
	id: "refine",
	button: "Refine Materials",
	action: "Smelting",
	duration: () => selectedItems.length * 500,
	step: () => selectedItems.length,
	requires: () => {
		let selectCount = selectedItems.length;
		let enoughEnergy = selectCount <= userData.energy;
		if (!enoughEnergy) { errorMessage = "insufficient energy to refine all selected ore"; }
		if(selectedItems.length == 0) {
			receiveMessage({name:"[Refinery]", id:-1, style: ""}, "Ther's nathin I can smelt!");
		}
		return enoughEnergy && selectedItems.length > 0;
	},
	start: () => {},
	run: () => {
		selectedItems.forEach(item => {
			let index = userData.inCargoHold.indexOf(item);
			if(index > -1) {
				userData.inCargoHold.splice(index, 1, refineItem(item));
			}
		});
		selectedItems = [];
	},
});

var refineMap = [
	{ input: "ironore", output: "iron", outputName: "Iron Bar", cost: 1, duration: 500 },
	{ input: "silverore", output: "silver", outputName: "Silver Bar", cost: 1, duration: 500 },
	{ input: "goldore", output: "gold", outputName: "Gold Bar", cost: 1, duration: 500 },
	{ input: "platinumore", output: "platinum", outputName: "Platinum Bar", cost: 2, duration: 500 },
	{ input: "rawdiamond", output: "diamond", outputName: "Diamond", cost: 5, duration: 500 },
];

function refineItem(item) {
	let refineRule = refineMap.find(refine => refine.input == item.type);
	if (!refineRule) {
		return item;
	}
	userData.energy -= refineRule.cost;
	return { id: item.id, type: refineRule.output, description: refineRule.outputName };
}