/**
 * host-mode-watcher.ts
 *
 * Shared helper that creates a config-key watcher for host-mode restart logic.
 * Extracted so the restart guard + dirty-flag loop-back pattern can be tested
 * independently of DaemonServer or HttpListener.
 *
 * Both facade.ts and http-listener.ts delegate to this helper, passing their
 * own `onRestart` closure and `getIsRunning` accessor.
 */
/**
 * Create a host-mode restart watcher.
 *
 * Behaviour:
 * - Subscribes to each key in `keys`.
 * - When a key changes and `getIsRunning()` is true, calls `onRestart()`.
 * - The caller's `onRestart` implementation handles re-entrancy / dirty-flag
 *   logic internally (the watcher does NOT duplicate that logic).
 * - `unsubscribe()` removes all subscriptions; subsequent key changes do not
 *   trigger restarts.
 *
 * @param opts - Watcher configuration.
 * @returns A handle with an `unsubscribe` method.
 */
export function createHostModeRestartWatcher(opts) {
    const { configManager, keys, onRestart, getIsRunning } = opts;
    const listener = () => {
        if (!getIsRunning())
            return;
        onRestart();
    };
    const unsubs = keys.map((key) => configManager.subscribe(key, listener));
    return {
        unsubscribe: () => {
            for (const u of unsubs)
                u();
            // Clear the array so subsequent calls are no-ops.
            unsubs.length = 0;
        },
    };
}
