// #region AssistantChatStore [C:3] [TYPE Store] [SEMANTICS assistant, chat, store, session, panel]
// @BRIEF Control assistant chat panel visibility, conversation binding, session context, and seeded prompts.
// @LAYER UI
// @RELATION BINDS_TO -> [AssistantChatPanel]
// @UX_STATE Closed -> Chat panel hidden
// @UX_STATE Open -> Chat panel visible with active conversation
// @RATIONALE Migrated from Svelte 4 writable to Svelte 5 $state runes.
//            Backward-compatible .subscribe() retained for $assistantChatStore syntax.

interface AssistantChatState {
	isOpen: boolean;
	conversationId: string | null;
	datasetReviewSessionId: string | null;
	seedMessage: string;
	focusTarget: string | null;
}

interface ChatContextPayload {
	datasetReviewSessionId?: string | null;
	seedMessage?: string;
	focusTarget?: string | null;
}

const initialState: AssistantChatState = {
	isOpen: false,
	conversationId: null,
	datasetReviewSessionId: null,
	seedMessage: '',
	focusTarget: null,
};

// Reactive state (Svelte 5 rune)
let _state: AssistantChatState = $state(initialState);

// Subscriber list for backward compat with $assistantChatStore syntax
let _subscribers: Array<(value: AssistantChatState) => void> = [];

function _notify(): void {
	const current = _state;
	_subscribers.forEach((fn) => fn(current));
}

export const assistantChatStore = {
	subscribe(run: (value: AssistantChatState) => void): () => void {
		_subscribers.push(run);
		run(_state);
		return () => {
			_subscribers = _subscribers.filter((fn) => fn !== run);
		};
	},
	get value(): AssistantChatState {
		return _state;
	},
};

export function toggleAssistantChat(): void {
	_state = { ..._state, isOpen: !_state.isOpen };
	_notify();
}

export function openAssistantChat(): void {
	if (_state.isOpen) return;
	_state = { ..._state, isOpen: true };
	_notify();
}

export function openAssistantChatWithContext(context: ChatContextPayload = {}): void {
	_state = {
		..._state,
		isOpen: true,
		datasetReviewSessionId: context.datasetReviewSessionId ?? _state.datasetReviewSessionId ?? null,
		seedMessage: context.seedMessage ?? '',
		focusTarget: context.focusTarget ?? null,
	};
	_notify();
}

export function closeAssistantChat(): void {
	if (!_state.isOpen) return;
	_state = { ..._state, isOpen: false };
	_notify();
}

export function setAssistantConversationId(conversationId: string): void {
	if (_state.conversationId === conversationId) return;
	_state = { ..._state, conversationId };
	_notify();
}

export function setAssistantDatasetReviewSessionId(datasetReviewSessionId: string | null): void {
	if (_state.datasetReviewSessionId === datasetReviewSessionId) return;
	_state = { ..._state, datasetReviewSessionId };
	_notify();
}

export function setAssistantSeedMessage(seedMessage: string): void {
	if (_state.seedMessage === (seedMessage || '')) return;
	_state = { ..._state, seedMessage: seedMessage || '' };
	_notify();
}

export function setAssistantFocusTarget(focusTarget: string | null): void {
	if (_state.focusTarget === (focusTarget || null)) return;
	_state = { ..._state, focusTarget: focusTarget || null };
	_notify();
}
// #endregion AssistantChatStore
