local _,ns = ...
--local L=ns.L

itemCachedPrice = {}
itemMinbuyoutCachedPrice = {}

local function crearTabla (valorMercado)
    return {TSMprice = valorMercado}
end

function ns.getItemCachedPrice(itemID)
    if not itemID then
        return
    end
    if itemCachedPrice[itemID] then
        --print ("devuelvo el precio existente",itemID,itemCachedPrice[itemID]["TSMprice"])
        --return itemCachedPrice[itemID]["TSMprice"]
    else
        if itemID == 82800 or itemID == 180653 then -- pets and item de hacer míticas da problemas con el itemlink y no se venden xk no da itemlink o es una jaula y no la pet.
            itemCachedPrice[itemID] = crearTabla(nil)
        else
            if TSM_API and TSM_API.GetCustomPriceValue then
                local valorMercadoComparacion
                local skip = false
                if not AutoScrap_CharSettings.usar2Mercados then
                    valorMercadoComparacion = TSM_API.GetCustomPriceValue(AutoScrap_CharSettings.TSM_mercado, "i:" .. itemID)
                    if not valorMercadoComparacion then
                        skip = true
                    end
                else
                    local valordbmarket = TSM_API.GetCustomPriceValue("dbmarket", "i:" .. itemID)
                    local valordbregionmarketavg = TSM_API.GetCustomPriceValue("dbregionmarketavg", "i:" .. itemID)
                    
                    if not valordbmarket and not valordbregionmarketavg then
                        skip = true
                    elseif not valordbmarket then
                        valorMercadoComparacion = valordbregionmarketavg
                    elseif not valordbregionmarketavg then
                        valorMercadoComparacion = valordbmarket
                    else
                        if  valordbmarket > valordbregionmarketavg  then
                            valorMercadoComparacion = valordbmarket
                        else
                            valorMercadoComparacion = valordbregionmarketavg
                        end
                    end
                end
                if skip then
                    itemCachedPrice[itemID] = crearTabla(nil)
                    --print ("añado",itemID,itemCachedPrice[itemID]["TSMprice"])
                else
                    itemCachedPrice[itemID] = crearTabla(valorMercadoComparacion)
                    --print ("añado",itemID,itemCachedPrice[itemID]["TSMprice"])
                end
            else
                itemCachedPrice[itemID] = crearTabla(nil)
            end
        end
    end
    return itemCachedPrice[itemID]["TSMprice"]
end

function ns.clearItemCachedPrice()
    --print("borro todo")
    wipe(itemCachedPrice)
    wipe(itemMinbuyoutCachedPrice)
end

--print(TSM_API.GetCustomPriceValue("dbminbuyout", "i:"..itemID))
function ns.getTSMMinbuyoutCache(itemID)
    if not itemMinbuyoutCachedPrice[itemID] then
        if TSM_API and TSM_API.GetCustomPriceValue then
            local valorTSM = TSM_API.GetCustomPriceValue("dbminbuyout", "i:" .. itemID)
            local valorAlternativo = ns.getItemCachedPrice(itemID)
            -- print("itemid:", itemID, "Preciominb:", valorTSM, "preciotsm:", valorAlternativo)
            if valorTSM ~= nil and valorTSM ~= 0 then
                itemMinbuyoutCachedPrice[itemID] = valorTSM
            else
                itemMinbuyoutCachedPrice[itemID] = valorAlternativo
            end
        else
            itemMinbuyoutCachedPrice[itemID] = nil
        end
    end
    return itemMinbuyoutCachedPrice[itemID]
end