-- lx-bundle:true

-- This file is part of Lx, created by Liqube <www.liqube.com>
-- Copyright Liqube Audio. All rights reserved.
-- Lx Interim License. See LICENSE for details.

local SessionState = {}

-- Keep namespaced runtime state for current LX session
local values = {}

-- Get a session value and apply optional fallback
function SessionState.get(key, default_value)
	local value = values[key]
	if value == nil then
		return default_value
	end
	return value
end

-- Set a session value
function SessionState.set(key, value)
	values[key] = value
end

-- Remove one session value
function SessionState.remove(key)
	values[key] = nil
end

-- Remove all keys in one namespace prefix
function SessionState.clear_namespace(prefix)
	if type(prefix) ~= "string" or prefix == "" then
		return
	end
	local namespace_prefix = prefix .. "."
	for key in pairs(values) do
		if key == prefix or key:sub(1, #namespace_prefix) == namespace_prefix then
			values[key] = nil
		end
	end
end

return SessionState
