/**
 * Mock for @vreko/local-service-client
 *
 * Provides a mock VrekoLocalClient for testing DaemonBridge
 */

import { vi } from "vitest";

// Connection state tracking
let _isConnected = false;

// Create mock functions that can be accessed in tests
export const mockConnect = vi.fn().mockImplementation(async () => {
	_isConnected = true;
});

export const mockInitialize = vi.fn().mockResolvedValue(undefined);

export const mockCall = vi.fn().mockResolvedValue({ pong: true, uptime: 1000, version: "1.0.0" });

export const mockOn = vi.fn();

export const mockIsConnected = vi.fn().mockImplementation(() => _isConnected);

export const mockClose = vi.fn().mockImplementation(() => {
	_isConnected = false;
});

export const getDefaultSocketPath = vi.fn().mockReturnValue("/tmp/test.sock");

// Mock VrekoLocalClient class
export class VrekoLocalClient {
	constructor(_options) {
		this.connect = mockConnect;
		this.initialize = mockInitialize;
		this.call = mockCall;
		this.on = mockOn;
		this.isConnected = mockIsConnected;
		this.close = mockClose;
	}
}

// Backward-compatible alias for older tests/docs
export const SnapBackLocalClient = VrekoLocalClient;

// Reset all mock functions
export function resetMockClient() {
	_isConnected = false;
	mockConnect.mockClear();
	mockConnect.mockImplementation(async () => {
		_isConnected = true;
	});
	mockInitialize.mockClear();
	mockInitialize.mockResolvedValue(undefined);
	mockCall.mockClear();
	mockCall.mockResolvedValue({ pong: true, uptime: 1000, version: "1.0.0" });
	mockOn.mockClear();
	mockIsConnected.mockClear();
	mockIsConnected.mockImplementation(() => _isConnected);
	mockClose.mockClear();
	mockClose.mockImplementation(() => {
		_isConnected = false;
	});
}

export default VrekoLocalClient;
