import { world, system, ItemStack } from "@minecraft/server";


export async function giveEnglishBook()
{
	for (const player of world.getPlayers()) {
		const inventory = player.getComponent("minecraft:inventory").container;
		let slotsWithBook = [];
		for (let i = 0; i < inventory.size; i++) {
			let item = inventory.getItem(i);
			if (item !== undefined && item.typeId === "bf_dc:news_paper6"){
				slotsWithBook.push(i);
			}
		}

		if(slotsWithBook.length > 0){
			for (let i = 0; i < slotsWithBook.length; i++){
				let englishBook = new ItemStack("bf_dc:news_paper");
				inventory.setItem(slotsWithBook[i], englishBook);
			}
		}
	}
}
export async function giveFrenchBook()
{
	for (const player of world.getPlayers()) {
		const inventory = player.getComponent("minecraft:inventory").container;
		let slotsWithBook = [];
		for (let i = 0; i < inventory.size; i++) {
			let item = inventory.getItem(i);
			if (item !== undefined && item.typeId === "bf_dc:news_paper6"){
				slotsWithBook.push(i);
			}
		}

		if(slotsWithBook.length > 0){
			for (let i = 0; i < slotsWithBook.length; i++){
				let frenchBook = new ItemStack("bf_dc:news_paper2");
				inventory.setItem(slotsWithBook[i], frenchBook);
			}
		}
	}
}
export async function giveGermanBook()
{
	for (const player of world.getPlayers()) {
		const inventory = player.getComponent("minecraft:inventory").container;
		let slotsWithBook = [];
		for (let i = 0; i < inventory.size; i++) {
			let item = inventory.getItem(i);
			if (item !== undefined && item.typeId === "bf_dc:news_paper6"){
				slotsWithBook.push(i);
			}
		}

		if(slotsWithBook.length > 0){
			for (let i = 0; i < slotsWithBook.length; i++){
				let germanBook = new ItemStack("bf_dc:news_paper3");
				inventory.setItem(slotsWithBook[i], germanBook);
			}
		}
	}
}
export async function giveSpanishBook()
{
	for (const player of world.getPlayers()) {
		const inventory = player.getComponent("minecraft:inventory").container;
		let slotsWithBook = [];
		for (let i = 0; i < inventory.size; i++) {
			let item = inventory.getItem(i);
			if (item !== undefined && item.typeId === "bf_dc:news_paper6"){
				slotsWithBook.push(i);
			}
		}

		if(slotsWithBook.length > 0){
			for (let i = 0; i < slotsWithBook.length; i++){
				let spanishBook = new ItemStack("bf_dc:news_paper5");
				inventory.setItem(slotsWithBook[i], spanishBook);
			}
		}
	}
}
export async function givePortugeseBook()
{
	for (const player of world.getPlayers()) {
		const inventory = player.getComponent("minecraft:inventory").container;
		let slotsWithBook = [];
		for (let i = 0; i < inventory.size; i++) {
			let item = inventory.getItem(i);
			if (item !== undefined && item.typeId === "bf_dc:news_paper6"){
				slotsWithBook.push(i);
			}
		}

		if(slotsWithBook.length > 0){
			for (let i = 0; i < slotsWithBook.length; i++){
				let portugeseBook = new ItemStack("bf_dc:news_paper4");
				inventory.setItem(slotsWithBook[i], portugeseBook);
			}
		}
	}
}