---@diagnostic disable: param-type-mismatch
-- 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.

--- Minimal startup for Lua states owned by worker threads
-- Workers process explicit job input and never inherit application state
-- They must not load CLI, UI, document, workspace, session, scale, or user startup state
-- @module core.workerboot

local function setup_paths()
	PATHSEP = package.config:sub(1, 1)
	EXE_DIR = EXE_DIR or "."
	CORE_DIR = false

	assert(type(EXE_FILE) == "string" and EXE_FILE ~= "", "EXE_FILE must be set")

	DATA_DIR = system.join_path(EXE_DIR, "data", true)
	HAVE_LOCAL_DATA = system.is_dir(DATA_DIR)

	if HAVE_LOCAL_DATA then
		local d = nil

		d = system.join_path(DATA_DIR, "core", true)
		if system.is_file(system.join_path(d, "init.lua")) then
			CORE_DIR = d
		end
	end
end

local function setup_external_package_path()
	if not HAVE_LOCAL_DATA then
		return
	end
	package.path = DATA_DIR .. "?.lua;" .. package.path
	package.path = DATA_DIR .. "?/init.lua;" .. package.path
end

local workerboot = {}

--- Mark and prepare one isolated worker Lua state
function workerboot.run()
	IS_WORKER = true
	setup_paths()
	setup_external_package_path()
end

return workerboot
