local G={ServiceLocked=false,IssueSignal=Madwork.NewScriptSignal(),CorruptionSignal=Madwork.NewScriptSignal(),CriticalState=false,CriticalStateSignal=Madwork.NewScriptSignal(),ServiceIssueCount=0;_active_profile_stores={},_auto_save_list={};_issue_queue={},_critical_state_start=0,_mock_data_store={};_user_mock_data_store={};_use_mock_data_store=false}
----- Service Table -----

local ProfileService = {

	ServiceLocked = false, -- Set to true once the server is shutting down

	IssueSignal = Madwork.NewScriptSignal(), -- (error_message, profile_store_name, profile_key) -- Fired when a DataStore API call throws an error
	CorruptionSignal = Madwork.NewScriptSignal(), -- (profile_store_name, profile_key) -- Fired when DataStore key returns a value that has
	-- all or some of it's profile components set to invalid data types. E.g., accidentally setting Profile.Data to a noon table value

	CriticalState = false, -- Set to true while DataStore service is throwing too many errors
	CriticalStateSignal = Madwork.NewScriptSignal(), -- (is_critical_state) -- Fired when CriticalState is set to true
	-- (You may alert players with this, or set up analytics)

	ServiceIssueCount = 0,

	_active_profile_stores = {
		--[[
			{
				_profile_store_name = "", -- [string] -- DataStore name
				_profile_template = {} / nil, -- [table / nil]
				_global_data_store = global_data_store, -- [GlobalDataStore] -- Object returned by DataStoreService:GetDataStore(_profile_store_name)
				
				_loaded_profiles = {
					[profile_key] = {
						Data = {}, -- [table] -- Loaded once after ProfileStore:LoadProfileAsync() finishes
						MetaData = {}, -- [table] -- Updated with every auto-save
						GlobalUpdates = {, -- [GlobalUpdates]
							_updates_latest = {}, -- [table] {update_index, {{update_id, version_id, update_locked, update_data}, ...}}
							_pending_update_lock = {update_id, ...} / nil, -- [table / nil]
							_pending_update_clear = {update_id, ...} / nil, -- [table / nil]
							
							_new_active_update_listeners = {listener, ...} / nil, -- [table / nil]
							_new_locked_update_listeners = {listener, ...} / nil, -- [table / nil]
							
							_profile = Profile / nil, -- [Profile / nil]
							
							_update_handler_mode = true / nil, -- [bool / nil]
						}
						
						_profile_store = ProfileStore, -- [ProfileStore]
						_profile_key = "", -- [string]
						
						_release_listeners = {listener, ...} / nil, -- [table / nil]
						
						_view_mode = true / nil, -- [bool / nil]
						
						_load_timestamp = os.clock(),
						
						_is_user_mock = false, -- ProfileStore.Mock
					},
					...
				},
				_profile_load_jobs = {[profile_key] = {load_id, loaded_data}, ...},
				
				_mock_loaded_profiles = {[profile_key] = Profile, ...},
				_mock_profile_load_jobs = {[profile_key] = {load_id, loaded_data}, ...},
				_is_pending = false, -- Waiting for live access check
			},
			...
		--]]
	},

	_auto_save_list = { -- loaded profile table which will be circularly auto-saved
		--[[
			Profile,
			...
		--]]
	},

	_issue_queue = {}, -- [table] {issue_time, ...}
	_critical_state_start = 0, -- [number] 0 = no critical state / os.clock() = critical state start

	-- Debug:
	_mock_data_store = {},
	_user_mock_data_store = {},

	_use_mock_data_store = false,
}
