// Copyright: © 2026 TWWIM UG. All rights reserved. (www.twwim.com) import { CONVERSATIONS_LIST, CONVERSATIONS_QUEUE_COUNTS, CONVERSATION_GET, CONVERSATION_INTERCEPT, CONVERSATION_RELEASE, CONVERSATION_SEND_MESSAGE, CONVERSATION_CLOSE, CONVERSATION_MARK_SEEN, CONVERSATION_REQUEST_CONTACT, CONVERSATION_CAPTURED_CONTACT, } from '@archer/api-interface/endpoints/customer-api'; import type { ConversationsListResponse, ConversationDetailResponse, QueueCountsResponse, CapturedContactResponse, } from '@archer/api-interface'; import { apiClient } from '../../ApiClient'; export type ListFilter = { tenantId?: string; status?: 'all' | 'active' | 'idle' | 'intercepted' | 'closed'; }; function buildQuery(params: Record): string { const entries = Object.entries(params).filter(([, v]) => v && v !== 'all'); if (entries.length === 0) return ''; const usp = new URLSearchParams(); for (const [k, v] of entries) usp.set(k, v as string); return `?${usp.toString()}`; } function path(template: string, params: Record): string { return Object.entries(params).reduce((acc, [k, v]) => acc.replace(`:${k}`, encodeURIComponent(v)), template); } export const conversationsApi = { async listConversations(filter: ListFilter): Promise { const url = CONVERSATIONS_LIST.path + buildQuery({ tenantId: filter.tenantId, status: filter.status }); return apiClient.get(url); }, async queueCounts(tenantId?: string): Promise { const url = CONVERSATIONS_QUEUE_COUNTS.path + buildQuery({ tenantId }); return apiClient.get(url); }, async getConversation(id: string): Promise { return apiClient.get(path(CONVERSATION_GET.path, { id })); }, async intercept(id: string): Promise { return apiClient.post(path(CONVERSATION_INTERCEPT.path, { id })); }, async release(id: string): Promise { return apiClient.post(path(CONVERSATION_RELEASE.path, { id })); }, async sendMessage(id: string, content: string): Promise { return apiClient.post(path(CONVERSATION_SEND_MESSAGE.path, { id }), { content }); }, async close(id: string): Promise { return apiClient.post(path(CONVERSATION_CLOSE.path, { id })); }, async markSeen(id: string): Promise { await apiClient.post(path(CONVERSATION_MARK_SEEN.path, { id })); }, async requestContact(id: string): Promise { return apiClient.post(path(CONVERSATION_REQUEST_CONTACT.path, { id })); }, async getCapturedContact(id: string): Promise { return apiClient.get(path(CONVERSATION_CAPTURED_CONTACT.path, { id })); }, };