if not _G.NOT_ONLY_TITANIUM_CONFIG then return end

local Config = _G.NOT_ONLY_TITANIUM_CONFIG

local baseCache = {}

local function is_valid(obj)
    if not obj then return false end
    local ok, v = pcall(function() return obj:IsValid() end)
    return ok and v == true
end

local function apply(sd)
    if not is_valid(sd) then return end

    local ok_name, name = pcall(function() return sd:GetFullName() end)
    if not ok_name then return end

    local ok, current = pcall(function() return sd.ScanDuration end)
    if not ok or type(current) ~= "number" or current <= 0 then return end

    if not baseCache[name] then
        baseCache[name] = current
    end

    local base = baseCache[name]
    local target = base

    if Config.SpeedEnabled and Config.SpeedMultiplier > 1.0 then
        target = math.max(0.001, base / Config.SpeedMultiplier)
    end

    pcall(function() sd.ScanDuration = target end)
end

local function apply_all() end
_G.NOT_ONLY_TITANIUM_SPEED_APPLY_ALL = apply_all

NotifyOnNewObject("/Script/Subnautica2.SN2AbilityTask_ScanHoverActor", function(task)
    ExecuteInGameThread(function()
        if not is_valid(task) then return end
        local ok, sd = pcall(function() return task.ScanData end)
        if ok and sd then apply(sd) end
    end)
end)