/**
 * 
 * @param {Internal.LivingEntityUseItemEvent$Start} e 
 */
function fantasiaHandleWeaponStanceSwap(e) {
    const { entity, item } = e

    if (!entity.isPlayer()) return

    /** @type {Player} */
    const player = entity

    if (!player.isCrouching()) return

    const offHandItem = player.offHandItem
    const mainHandItem = player.mainHandItem

    if (item == offHandItem && item != mainHandItem) return

    const id = item.id

    const isOne = item.hasTag("fantasia:stance_one_handed")
    const isTwo = item.hasTag("fantasia:stance_two_handed")

    if (!isOne && !isTwo) return

    let targetId = null

    let idParts = id.split(':')
    let path = idParts[1]

    if (isOne) {

        targetId = `kubejs:${path}_two_handed`
    } else if (isTwo) {

        let baseName = path.replace("_two_handed", "")
        targetId = `spartanweaponry:${baseName}`
    }

    if (targetId) {
        swapWeaponStance(player, targetId)
        e.setCanceled(true)
    }
}

/**
 * @param {Player} player
 * @param {Internal.InteractionHand} hand
 * @param {String} targetItemId
 */
const swapWeaponStance = (player, targetItemId) => {

    let currentItem = player.mainHandItem

    player.addItemCooldown(currentItem.id, 10)
    player.addItemCooldown(targetItemId, 10)

    let newItemStack = Item.of(targetItemId).withNBT(currentItem.nbt)
    player.setItemInHand("main_hand", newItemStack)
}