setup.getStartingCharacter = function(id=undefined){
	id ??= State.variables.startCharacter;
	let currentData = clone(setup.startingCharacters[id]);
	currentData.inheritance = [id];
	if("parent" in currentData){
		let parentData = setup.getStartingCharacter(currentData.parent);
		let newcurrentData = setup.mergeDeep(parentData,currentData,{inheritance:[...parentData.inheritance,...currentData.inheritance]});

		currentData = newcurrentData;
		delete currentData.parent;
	}
	return currentData;
}

setup.getStartingCharactersByFilter = function(filters,shallow = true){
	let result = {};
	charLoop: for (const [charId, charData] of Object.entries(setup.startingCharacters)) {
		for (const [filterKey, filterValue] of Object.entries(filters)) {
			if(charData[filterKey] !== filterValue)
				continue charLoop;
			if(shallow)
				result[charId] = charData;
			else
				result[charId] = setup.getStartingCharacter(charId);
		}
	}
	return result;
}

setup.startingCharacterApply = function(characterId){
	const startCharacterData = setup.getStartingCharacter(characterId);
	const variables = State.variables;

	variables.startCharacter = characterId;

	variables.pc.avatar = startCharacterData.image;

	
	if(startCharacterData.time){
		if(startCharacterData.time.start){
			variables.time.initTime(
				startCharacterData.time.start[0],
				startCharacterData.time.start[1],
				startCharacterData.time.start[2],
				startCharacterData.time.start[3],
				startCharacterData.time.start[4]);
		}
	}

	variables.housing.home = startCharacterData.housing.home;

	for(const [_key,_value] of Object.entries(startCharacterData.pc ?? {})){
		variables.pc[_key] = _value;
	}


	for(const [_locationId,_locationValue] of Object.entries(startCharacterData.location ?? {})){
		for(const [_fieldId,_fieldValue] of Object.entries(_locationValue  ?? {})){
			variables.location.set(_locationId,_fieldId,_fieldValue);
		}
	}

	for(const [_locationTag,_locationTagValue] of Object.entries(startCharacterData.locationTags ?? {})){
		for(const [_fieldId,_fieldValue] of Object.entries(_locationTagValue  ?? {})){
			variables.location.setTagValue(_locationTag,_fieldId,_fieldValue);
		}
	}

	for(const [_key,_value] of Object.entries(startCharacterData.personality ?? {})){
		variables.pc.personalityScale(_key).current = _value;
	}

	for(const [_key,_value] of Object.entries(startCharacterData.quests ?? {})){
		if(_value === true){
			variables.quest(_key).start();
		}
	}

	for(const [_key,_value] of Object.entries(startCharacterData.skills ?? {})){
		variables.pc.skillSetLevel(_key,_value);
	}

	for(const [_key,_value] of Object.entries(startCharacterData.traits ?? {})){
		variables.pc.traitSet(_key,_value);
	}

	for(const [_key,_value] of Object.entries(startCharacterData.finances ?? {})){
		variables.finances[_key] = _value;
	}

	for(const [_key,_value] of Object.entries(startCharacterData.items ?? {})){
		if(typeof _value == "number")
			variables.inventory.set(_key,_value);
		else if(typeof _value === "object"){
			if(_value.generateFunction)
				variables.inventory.set(
					_key,
					1,
					undefined,
					Object.assign(
						setup[_value.generateFunction](
							...(_value.generateParameters ?? [])
						),
						{id:_key}
					)
				);
			else
				variables.inventory.set(_key,1,undefined,_value);
			
		}
	}

	//<!-- NPCs -->
		//<!-- BULK -->
		for(const [_key,_value] of Object.entries(startCharacterData.npcs?.bulk ?? {})){
			let _filters = _value.filters ?? {};
			for(const [_fieldKey,_fieldValue] of Object.entries(_value.values ?? {})){
				variables.npcs.setBulkByFilter(_filters,_fieldKey,_fieldValue);
			}
		}
		//<!-- IDs: Need to come after bulk so they don't get overwritten -->
		for(const [_key,_value] of Object.entries(startCharacterData.npcs?.ids ?? {})){
			for(const [_fieldKey,_fieldValue] of Object.entries(_value)){
				variables.npcs.set(_key,_fieldKey,_fieldValue);
			}
		}

	//<!-- School stuff -->
	if(startCharacterData.school){
		for(const [_key,_value] of Object.entries(startCharacterData.school.grades ?? {})){
			variables.quest('school').func('grade_award',_key,_value);
		}
		variables.quest('school').func('initGroupMembership',startCharacterData.school.group);
	}

	//<!--Wardrobe -->
	for(const [_key,_value] of Object.entries(startCharacterData.wardrobe?.items ?? {})){
		if(_value === 'wear')
			variables.wardrobe.wear(_key);
		else if(_value === 'add')
			variables.wardrobe.add(_key);
	}

	for(const [_key,_value] of Object.entries(startCharacterData.wardrobe?.itemsByFilter ?? {})){
		let _filters = _value.filters;
		let _count = _value.count ?? 1;
		let _action = _value.action ?? 'add';
		let _itemIds = variables.wardrobe.allItemIdsFiltered(_filters,_count,true);
		for(const _itemId of _itemIds){
			switch(_action){
				case 'add':
					variables.wardrobe.add(_itemId);
					break;
				case 'wear':
					variables.wardrobe.wear(_itemId);
					break;
			}
		}
	}

}



setup.startingCharacterTagsIncludes = function(tag){
	const startData = setup.getStartingCharacter(State.variables.startCharacter) ?? {};
	const startDataTags = startData.tags ?? [];
	return startDataTags.includes(tag);
}


setup.startingCharacters ??= {};