local function makeModuleWithAutoLoad(prefix)

    local mod = {}

    local function loadSubmodule(_, name)

        -- For observables, try with Observable suffix

        if prefix == 'Rx.observables.' then
            local success, submodule = pcall(require, prefix .. name .. 'Observable')
            if success then
                rawset(mod, name, submodule)
                return submodule
            end
        end

        -- Standard case / Fallback for Rx.observables

        local submodule = require(prefix .. name)
        rawset(mod, name, submodule)
        return submodule

    end

    setmetatable(mod, {
        __index = loadSubmodule,
        __newindex = error,
    })

    return mod

end

--------------------------------------------------------------------------------

local Rx = makeModuleWithAutoLoad('Rx.')

rawset(Rx, 'internals', makeModuleWithAutoLoad('Rx.internals.'))
rawset(Rx, 'observables', makeModuleWithAutoLoad('Rx.observables.'))
rawset(Rx, 'operators', makeModuleWithAutoLoad('Rx.operators.'))
rawset(Rx, 'testing', makeModuleWithAutoLoad('Rx.testing.'))

return Rx
