import { defineStore } from 'pinia' import { ref } from 'vue' import { useLabels } from '@/composables/useLabels' import IvyMessage from '@/views/_components/message/ivyMessage.ts' import { useApiClient } from '@/composables/useApiClient.ts' interface FieldData { id: string type: string label: string placeholder?: string required: boolean validation?: Record settings?: Record } interface FormSettings { success_message?: string [key: string]: unknown } interface IntegrationSettings { wpdatatables: { enabled: boolean } [key: string]: unknown } interface FormData { name: string description: string status: string published?: boolean showTitle?: boolean showDescription?: boolean storeEntries?: boolean integrationSettings?: IntegrationSettings fields: FieldData[] settings: FormSettings } interface Template { id: string name: string description: string category: string is_pro: boolean screenshot: string form_data: FormData } interface TemplateCategory { name: string description: string } export const useTemplateStore = defineStore('ivyformsTemplate', () => { // State const templates = ref([]) const categories = ref>({}) const isLoading = ref(false) const error = ref(null) // Composables const { getLabel } = useLabels() const { request } = useApiClient() // Actions const fetchTemplates = async (): Promise => { isLoading.value = true error.value = null try { const { data, error: apiError } = await request('templates', { method: 'GET', }) if (apiError) { throw new Error(apiError) } if (data.data.success) { // Use the associative array from backend, preserve IDs templates.value = Object.values(data.data.data.templates) categories.value = data.data.data.categories } else { throw new Error(getLabel('failed_to_fetch_template')) } } catch (err) { error.value = err instanceof Error ? err.message : getLabel('failed_to_fetch_template') console.error(getLabel('template_fetch_error'), err) IvyMessage({ message: `${getLabel('failed_to_fetch_template')} ${error.value}`, type: 'error', }) throw err } finally { isLoading.value = false } } const getTemplate = async (templateId: string): Promise