import { ref, watch, readonly } from 'vue' export interface CoopInfo { cooperativeName: string dateEstablished: string purpose: string coreValues: string legalStructure: string registeredLocation: string isLegallyRegistered: boolean } const STORAGE_KEY = 'coop-info' // Global reactive state const coopInfo = ref({ cooperativeName: '', dateEstablished: '', purpose: '', coreValues: '', legalStructure: '', registeredLocation: '', isLegallyRegistered: false }) // Flag to prevent loading during initial save let isInitialized = false export const useCoopInfo = () => { // Load data from localStorage on first use if (!isInitialized && process.client) { const saved = localStorage.getItem(STORAGE_KEY) if (saved) { try { const parsedData = JSON.parse(saved) coopInfo.value = { ...coopInfo.value, ...parsedData } } catch (error) { console.error('Error loading coop info:', error) } } isInitialized = true // Set up watcher to save changes watch( coopInfo, (newData) => { localStorage.setItem(STORAGE_KEY, JSON.stringify(newData)) }, { deep: true } ) } // Helper function to update specific fields const updateCoopInfo = (updates: Partial) => { coopInfo.value = { ...coopInfo.value, ...updates } } // Helper function to get display name (with fallback) const getDisplayName = () => { return coopInfo.value.cooperativeName || 'Worker Cooperative' } // Helper function to get organization name for different contexts const getOrgName = () => { return coopInfo.value.cooperativeName || 'Organization' } // Helper function to check if basic info is complete const isBasicInfoComplete = () => { return !!(coopInfo.value.cooperativeName && coopInfo.value.cooperativeName.trim()) } return { coopInfo: readonly(coopInfo), updateCoopInfo, getDisplayName, getOrgName, isBasicInfoComplete } }