dofile_once( "data/scripts/lib/utilities.lua" )
dofile_once( "data/scripts/gun/procedural/gun_action_utils.lua" )


local entity_id    = GetUpdatedEntityID()
local pos_x, pos_y = EntityGetTransform( entity_id )

local owner = EntityGetClosestWithTag(pos_x, pos_y, "mortal")


if(owner ~= nil)then
    local wand = find_the_wand_held( owner )

    if(wand ~= 0)then
        local children = EntityGetAllChildren( wand );

        for i,v in ipairs( children ) do
            --print(v)
            local action_component = EntityGetComponentIncludingDisabled( v, "ItemActionComponent" )[1];
            if(action_component ~= nil)then
                local action_id = ComponentGetValue2(action_component, "action_id")
              --  print(action_id)
                if(action_id == "SPELLBOUND_LUCKY_KNIFE")then
                    local item_component = EntityGetComponentIncludingDisabled( v, "ItemComponent")[1];
                    
                    if(item_component ~= nil)then
                        local uses_remaining = ComponentGetValue2(item_component, "uses_remaining")
                        if(tonumber(uses_remaining) == 0)then
                            EntityRemoveFromParent(v)
                            EntityKill(v)

                            local inventory2_comp = EntityGetFirstComponent( owner, "Inventory2Component" )
                            if( inventory2_comp ) then
                                ComponentSetValue( inventory2_comp, "mActualActiveItem", "0" )
                            end
                        end
                    end
                end
            end
        end
    end
end