import { world, system, ItemStack } from "@minecraft/server";
import * as utils from "./utils";


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 === "pu_se:book"){
				slotsWithBook.push(i);
			}
		}

		if(slotsWithBook.length > 0){
			for (let i = 0; i < slotsWithBook.length; i++){
				let englishBook = new ItemStack("pu_se:book_english");
				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 === "pu_se:book"){
				slotsWithBook.push(i);
			}
		}

		if(slotsWithBook.length > 0){
			for (let i = 0; i < slotsWithBook.length; i++){
				let frenchBook = new ItemStack("pu_se:book_french");
				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 === "pu_se:book"){
				slotsWithBook.push(i);
			}
		}

		if(slotsWithBook.length > 0){
			for (let i = 0; i < slotsWithBook.length; i++){
				let germanBook = new ItemStack("pu_se:book_german");
				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 === "pu_se:book"){
				slotsWithBook.push(i);
			}
		}

		if(slotsWithBook.length > 0){
			for (let i = 0; i < slotsWithBook.length; i++){
				let spanishBook = new ItemStack("pu_se:book_spanish");
				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 === "pu_se:book"){
				slotsWithBook.push(i);
			}
		}

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