require("./chunk-Ble4zEEl.js");
let _camstack_types = require("@camstack/types");
//#region src/moleculer/worker-device-restore.ts
/**
* Worker-side device restore helper.
*
* Shared by `process-runner` (single-addon subprocess) and
* `group-runner` (group subprocess). Calls the worker addon's
* `restoreDevices(SavedDevice[])` with devices read from the hub's
* `device-manager` capability. Blocks on the `system.ready-state`
* readiness of the hub's `device-manager` via
* `ReadinessRegistry.awaitReady`, then issues the calls ONCE.
*
* Lives in its own module so it can be imported without triggering
* the runner entry-point side effects (each runner is also an
* executable that calls `main()` at import time).
*/
async function runWorkerDeviceRestoreWithRetry(addon, context, addonId, sourceNodeId) {
	console.error(`[worker-restore] entry: addon="${addonId}" sourceNodeId="${sourceNodeId}"`);
	const restoreFn = addon.restoreDevices;
	if (typeof restoreFn !== "function") {
		console.error(`[worker-restore] "${addonId}": no restoreDevices function — skipping`);
		return;
	}
	const api = context.api;
	if (!api) {
		console.error(`[worker-restore] "${addonId}": context.api missing — skipping`);
		return;
	}
	const bus = context.eventBus;
	if (!bus) {
		console.error(`[worker-restore] "${addonId}": context.eventBus missing — skipping`);
		return;
	}
	const shared = context.kernel?.readinessRegistry ?? null;
	const registry = shared ?? new _camstack_types.ReadinessRegistry({
		eventBus: bus,
		sourceNodeId,
		logger: context.logger
	});
	console.error(`[worker-restore] "${addonId}": awaiting device-manager readiness on hub...`);
	try {
		await registry.awaitReady("device-manager", {
			type: "node",
			nodeId: "hub"
		}, { timeoutMs: 6e4 });
		console.error(`[worker-restore] "${addonId}": device-manager READY`);
	} catch (err) {
		if (err instanceof _camstack_types.ReadinessTimeoutError) {
			context.logger?.warn?.(`[worker-restore] device-manager not ready within ${err.waitedMs}ms — skipping for "${addonId}"`);
			return;
		}
		throw err;
	} finally {
		if (!shared && registry instanceof _camstack_types.ReadinessRegistry) registry.close();
	}
	try {
		const deviceManager = Reflect.get(api, "deviceManager");
		console.error(`[worker-restore] "${addonId}": calling listPersistedByAddon...`);
		const rowsResult = await deviceManager.listPersistedByAddon.query({ addonId });
		console.error(`[worker-restore] "${addonId}": got ${Array.isArray(rowsResult) ? rowsResult.length : 0} row(s)`);
		const rows = Array.isArray(rowsResult) ? rowsResult : [];
		if (rows.length === 0) {
			context.logger?.info?.(`[worker-restore] no persisted devices to restore for addon "${addonId}"`);
			return;
		}
		const savedDevices = await Promise.all(rows.map(async (row) => {
			const config = await deviceManager.loadConfig.query({ deviceId: row.id });
			return {
				id: row.id,
				stableId: row.stableId,
				type: row.type,
				name: row.name,
				parentDeviceId: row.parentDeviceId,
				config: config ?? {}
			};
		}));
		await restoreFn.call(addon, savedDevices);
		console.error(`[worker-restore] "${addonId}": restored ${savedDevices.length} device(s)`);
	} catch (err) {
		console.error(`[worker-restore] "${addonId}": restoreDevices threw: ${(0, _camstack_types.errMsg)(err)}`);
	}
}
//#endregion
Object.defineProperty(exports, "runWorkerDeviceRestoreWithRetry", {
	enumerable: true,
	get: function() {
		return runWorkerDeviceRestoreWithRetry;
	}
});

//# sourceMappingURL=worker-device-restore-B_Di-zuW.js.map