const EnchantmentHelper = java("net.minecraft.world.item.enchantment.EnchantmentHelper");

function backPackRecipe(grid, result) {
	let item = grid.find('#curios:back')
	let itemCopy = item.copy()

    let nbt1 = item.nbt
	if (result.nbt == null) {
        result.nbt = {}
	}
	if (nbt1 == null) {
        nbt1 = {}
	}
	let contents = nbt1.contentsUuid
	let borderColor = nbt1.borderColor
	let clothColor = nbt1.clothColor

	let nbt2 = result.nbt
	if (nbt2 == null) {
        nbt2 = {}
    }
	if (contents != null) {
		nbt2.contentsUuid = contents
	}
	if (borderColor != null) {
		nbt2.borderColor = borderColor
	}
	if (clothColor != null) {
		nbt2.clothColor = clothColor
	}

	let outputItem = result.withNBT(nbt2)
		
	//if (itemCopy.enchantments == null) {
		return outputItem
	//} else {
		//return outputItem.enchant(itemCopy.enchantments)
	//}
}

function colorBackpack(grid, result, color) {
	let item = grid.find('#curios:back')
	let itemCopy = item.copy()

	let nbt1 = item.nbt
	if (nbt1 == null) {
        nbt1 = {}
	}
	if (color != null) {
		nbt1.borderColor = '"'+color+'"'
	}
	if (color != null) {
		nbt1.clothColor = '"'+color+'"'
	}

	let outputItem = result.withNBT(nbt1)
	if (itemCopy.enchantments == null) {
		return outputItem
	} else {
		return outputItem.enchant(itemCopy.enchantments)
	}
}

onEvent('recipes', event => {
	// Улучшение Внедрение
	event.remove({output:'sophisticatedbackpacks:inception_upgrade'});
	event.shaped('1x sophisticatedbackpacks:inception_upgrade', [
		'PSW',
		'TBT',
		'ETH'
	  ], {
		P: 'upgradednetherite:water_upgraded_netherite_ingot',
		S: 'minecraft:nether_star',
		W: 'upgradednetherite:wither_upgraded_netherite_ingot',
		T: 'kubejs:netherite_string',
		B: 'sophisticatedbackpacks:upgrade_base',
		E: 'upgradednetherite:ender_upgraded_netherite_ingot',
		H: 'upgradednetherite:phantom_upgraded_netherite_ingot'
	  })
	// Рецепты нитей
	event.shaped('1x kubejs:iron_string', [
		'NSN',
		'SNS',
		'NSN'
	  ], {
		S: '#forge:string',
		N: '#forge:nuggets/iron'
	  })
	event.shaped('1x kubejs:gold_string', [
		'NSN',
		'SNS',
		'NSN'
	  ], {
		S: 'kubejs:iron_string',
		N: '#forge:nuggets/gold'
	  })
	event.shaped('1x kubejs:diamond_string', [
		'NSN',
		'SNS',
		'NSN'
	  ], {
		S: 'kubejs:gold_string',
		N: '#forge:gems/diamond'
	  })
	event.shaped('1x kubejs:netherite_string', [
		'NSN',
		'SNS',
		'NSN'
	  ], {
		S: 'kubejs:diamond_string',
		N: '#forge:nuggets/netherite'
	  })
	  
	//Рюкзаки
	event.remove({id: 'sophisticatedbackpacks:iron_backpack'})
	event.remove({id: 'sophisticatedbackpacks:gold_backpack'})
	event.remove({id: 'sophisticatedbackpacks:diamond_backpack'})
	event.remove({id: 'sophisticatedbackpacks:netherite_backpack'})
	
	event.shaped('1x sophisticatedbackpacks:iron_backpack', [
		'SSS',
		'SBS',
		'SSS'
	  ], {
		S: 'kubejs:iron_string',
		B: 'sophisticatedbackpacks:backpack'
	  }).modifyResult((grid, result) => {
		return backPackRecipe(grid, result)
    })
	event.shaped('1x sophisticatedbackpacks:gold_backpack', [
		'SSS',
		'SBS',
		'SSS'
	  ], {
		S: 'kubejs:gold_string',
		B: 'sophisticatedbackpacks:iron_backpack'
	  }).modifyResult((grid, result) => {
        return backPackRecipe(grid, result)
    })
	event.shaped('1x sophisticatedbackpacks:diamond_backpack', [
		'SSS',
		'SBS',
		'SSS'
	  ], {
		S: 'kubejs:diamond_string',
		B: 'sophisticatedbackpacks:gold_backpack'
	  }).modifyResult((grid, result) => {
		return backPackRecipe(grid, result)
    })
	event.shaped('1x sophisticatedbackpacks:netherite_backpack', [
		'SSS',
		'SBS',
		'SSS'
	  ], {
		S: 'kubejs:netherite_string',
		B: 'sophisticatedbackpacks:diamond_backpack'
	  }).modifyResult((grid, result) => {
		return backPackRecipe(grid, result)
    })

	//покраска рюкзаков
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/white']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "16383998")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/orange']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "16351261")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/magenta']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "13061821")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/light_blue']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "3847130")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/yellow']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "16701501")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/lime']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "8439583")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/pink']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "15961002")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/gray']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "4673362")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/light_gray']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "10329495")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/cyan']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "1481884")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/purple']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "8991416")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/blue']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "3949738")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/brown']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "8606770")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/green']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "6192150")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/red']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "11546150")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack','#forge:dyes/black']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "1908001")
    });
	event.shapeless('sophisticatedbackpacks:iron_backpack', ['sophisticatedbackpacks:iron_backpack', '#forge:dyes/yellow', '#forge:dyes/green', '#forge:dyes/blue', '#forge:dyes/black']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "2895973")
    });

	//gold

	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/white']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "16383998")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/orange']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "16351261")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/magenta']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "13061821")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/light_blue']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "3847130")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/yellow']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "16701501")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/lime']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "8439583")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/pink']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "15961002")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/gray']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "4673362")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/light_gray']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "10329495")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/cyan']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "1481884")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/purple']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "8991416")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/blue']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "3949738")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/brown']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "8606770")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/green']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "6192150")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/red']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "11546150")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack','#forge:dyes/black']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "1908001")
    });
	event.shapeless('sophisticatedbackpacks:gold_backpack', ['sophisticatedbackpacks:gold_backpack', '#forge:dyes/yellow', '#forge:dyes/green', '#forge:dyes/blue', '#forge:dyes/black']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "2895973")
    });

	// diamond

	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/white']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "16383998")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/orange']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "16351261")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/magenta']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "13061821")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/light_blue']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "3847130")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/yellow']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "16701501")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/lime']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "8439583")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/pink']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "15961002")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/gray']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "4673362")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/light_gray']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "10329495")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/cyan']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "1481884")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/purple']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "8991416")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/blue']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "3949738")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/brown']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "8606770")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/green']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "6192150")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/red']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "11546150")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack','#forge:dyes/black']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "1908001")
    });
	event.shapeless('sophisticatedbackpacks:diamond_backpack', ['sophisticatedbackpacks:diamond_backpack', '#forge:dyes/yellow', '#forge:dyes/green', '#forge:dyes/blue', '#forge:dyes/black']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "2895973")
    });

	//netherite

	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/white']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "16383998")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/orange']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "16351261")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/magenta']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "13061821")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/light_blue']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "3847130")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/yellow']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "16701501")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/lime']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "8439583")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/pink']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "15961002")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/gray']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "4673362")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/light_gray']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "10329495")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/cyan']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "1481884")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/purple']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "8991416")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/blue']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "3949738")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/brown']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "8606770")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/green']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "6192150")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/red']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "11546150")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack','#forge:dyes/black']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "1908001")
    });
	event.shapeless('sophisticatedbackpacks:netherite_backpack', ['sophisticatedbackpacks:netherite_backpack', '#forge:dyes/yellow', '#forge:dyes/green', '#forge:dyes/blue', '#forge:dyes/black']).modifyResult((grid, result) => {
		return colorBackpack(grid, result, "2895973")
    });

})