import type { AgentSession, SessionTreeSessionMeta } from '../types/index.d';

type SessionLike = Pick<AgentSession, 'metadata'> | Pick<SessionTreeSessionMeta, 'metadata'> | null | undefined;

function isRecord(value: unknown): value is Record<string, unknown> {
	return Boolean(value && typeof value === 'object' && !Array.isArray(value));
}

export function isAuditOrderSessionMetadata(metadata: unknown): boolean {
	if (!isRecord(metadata)) return false;

	const source = metadata.source || metadata.type || metadata.kind;
	return metadata.isAuditOrder === true
		|| source === 'audit_order'
		|| source === 'auditOrder'
		|| isRecord(metadata.auditOrder);
}

export function isAuditOrderSession(session: SessionLike): boolean {
	return isAuditOrderSessionMetadata(session?.metadata);
}

export function canDeleteAgentSession(session: SessionLike): boolean {
	return !isAuditOrderSession(session);
}
