import { defineStore } from 'pinia' import { type CreateCommentDto, type CommentDto, ResponseError } from '~~/.api-client' import { useCommentApi } from '~/composables/comment/useCommentApi' export const useCommentStore = defineStore('Comment', () => { type FormElementId = string const commentApi = useCommentApi() const comments = ref>({}) const loadedForms = ref(new Set()) async function load(applicationFormId: string) { if (loadedForms.value.has(applicationFormId)) return const { data, error } = await useAsyncData(`comments:${applicationFormId}`, () => commentApi.getCommentsByApplicationFormId(applicationFormId) ) if (error.value) { console.error('Failed loading comments:', error.value) return } comments.value = data.value?.content.reduce((acc: Record, comment: CommentDto) => { const formElementId = comment.formElementId if (!acc[formElementId]) { acc[formElementId] = [] } acc[formElementId].push(comment) return acc }, {}) || {} loadedForms.value.add(applicationFormId) } async function createComment( applicationFormId: string, formElementId: string, createCommentDto: CreateCommentDto ): Promise { try { const newComment = await commentApi.createComment(applicationFormId, formElementId, createCommentDto) if (!comments.value[formElementId]) { comments.value[formElementId] = [] } comments.value[formElementId].push(newComment) return newComment } catch (e: unknown) { if (e instanceof ResponseError) { console.error('Failed creating comment:', e.response) } else { console.error('Failed creating comment:', e) } return Promise.reject(e) } } async function updateComment(id?: string, commentDto?: CommentDto): Promise { if (!id || !commentDto) { return Promise.reject(new Error('ID or comment DTO missing')) } try { const updatedComment = await commentApi.updateComment(id, commentDto) const formElementId = updatedComment.formElementId const formElementComments = comments.value?.[formElementId] if (formElementComments) { const index = formElementComments.findIndex((comment) => comment.id === id) if (index !== -1 && formElementComments[index]) { formElementComments[index] = updatedComment } } return updatedComment } catch (e: unknown) { if (e instanceof ResponseError) { console.error(`Failed updating comment with ID ${id}:`, e.response) } else { console.error(`Failed updating comment with ID ${id}:`, e) } return Promise.reject(e) } } async function deleteCommentById(id: string): Promise { try { await commentApi.deleteCommentById(id) for (const formElementId in comments.value) { const formElementComments = comments.value[formElementId] if (formElementComments) { const index = formElementComments.findIndex((comment) => comment.id === id) if (index !== -1) { formElementComments.splice(index, 1) break } } } } catch (e: unknown) { if (e instanceof ResponseError) { console.error(`Failed deleting comment with ID ${id}:`, e.response) } else { console.error(`Failed deleting comment with ID ${id}:`, e) } return Promise.reject(e) } } return { load, createComment, updateComment, deleteCommentById, comments } })