1+ import type {
2+ PreferenceRegistration ,
3+ PreferenceChange ,
4+ PreferenceValidation ,
5+ ValidationResult ,
6+ PreferencesHook
7+ } from './types'
8+
9+ /**
10+ * In-memory registry for plugin preferences
11+ */
12+ export class PreferenceRegistry {
13+ private static instance : PreferenceRegistry
14+ private preferences : Map < string , PreferenceRegistration > = new Map ( )
15+ private hooks : Map < string , PreferencesHook > = new Map ( )
16+
17+ private constructor ( ) { }
18+
19+ /**
20+ * Get singleton instance
21+ */
22+ static getInstance ( ) : PreferenceRegistry {
23+ if ( ! PreferenceRegistry . instance ) {
24+ PreferenceRegistry . instance = new PreferenceRegistry ( )
25+ }
26+ return PreferenceRegistry . instance
27+ }
28+
29+ /**
30+ * Register preferences from a plugin
31+ */
32+ async register ( pluginId : string , hook : PreferencesHook , registration : PreferenceRegistration ) : Promise < void > {
33+ // Validate registration
34+ const { PreferenceRegistrationSchema } = await import ( './types' )
35+ const { z } = await import ( 'zod' )
36+
37+ try {
38+ PreferenceRegistrationSchema . parse ( registration )
39+ } catch ( error ) {
40+ if ( error instanceof z . ZodError ) {
41+ throw new Error ( `Invalid preference registration for plugin ${ pluginId } : ${ error . message } ` )
42+ }
43+ throw error
44+ }
45+
46+ // Store preference registration
47+ this . preferences . set ( pluginId , registration )
48+ this . hooks . set ( pluginId , hook )
49+
50+ console . log ( `Registered preferences for plugin: ${ pluginId } ` )
51+ }
52+
53+ /**
54+ * Get all registered preference registrations
55+ */
56+ getAllRegistrations ( ) : Map < string , PreferenceRegistration > {
57+ return new Map ( this . preferences )
58+ }
59+
60+ /**
61+ * Get specific plugin registration
62+ */
63+ getRegistration ( pluginId : string ) : PreferenceRegistration | undefined {
64+ return this . preferences . get ( pluginId )
65+ }
66+
67+ /**
68+ * Handle preference value change
69+ */
70+ async handleChange ( pluginId : string , key : string , value : any , oldValue ?: any ) : Promise < void > {
71+ const hook = this . hooks . get ( pluginId )
72+ const registration = this . preferences . get ( pluginId )
73+
74+ if ( ! hook || ! registration ) {
75+ throw new Error ( `No preferences registered for plugin: ${ pluginId } ` )
76+ }
77+
78+ // Call validation if provided
79+ if ( hook . validate ) {
80+ const validationResult = await hook . validate ( {
81+ key,
82+ value,
83+ registration
84+ } )
85+
86+ if ( ! validationResult . valid ) {
87+ throw new Error ( `Validation failed for ${ key } : ${ validationResult . error } ` )
88+ }
89+
90+ if ( validationResult . warning ) {
91+ console . warn ( `Preference warning for ${ key } : ${ validationResult . warning } ` )
92+ }
93+ }
94+
95+ // Call change handler if provided
96+ if ( hook . change ) {
97+ await hook . change ( {
98+ key,
99+ value,
100+ oldValue,
101+ registration
102+ } )
103+ }
104+
105+ console . log ( `Preference changed for plugin ${ pluginId } : ${ key } = ${ value } ` )
106+ }
107+
108+ /**
109+ * Get current preference values for a plugin
110+ */
111+ async getValues ( pluginId : string ) : Promise < Record < string , any > > {
112+ const hook = this . hooks . get ( pluginId )
113+ const registration = this . preferences . get ( pluginId )
114+
115+ if ( ! registration ) {
116+ return { }
117+ }
118+
119+ // If plugin provides custom value getter, use it
120+ if ( hook ?. getValues ) {
121+ return await hook . getValues ( )
122+ }
123+
124+ // Otherwise, try to load from configuration (this will be implemented with Desktop integration)
125+ return registration . defaults
126+ }
127+
128+ /**
129+ * Validate a preference value without changing it
130+ */
131+ async validateValue ( pluginId : string , key : string , value : any ) : Promise < ValidationResult > {
132+ const hook = this . hooks . get ( pluginId )
133+ const registration = this . preferences . get ( pluginId )
134+
135+ if ( ! hook || ! registration ) {
136+ return { valid : false , error : `No preferences registered for plugin: ${ pluginId } ` }
137+ }
138+
139+ if ( hook . validate ) {
140+ return await hook . validate ( {
141+ key,
142+ value,
143+ registration
144+ } )
145+ }
146+
147+ // Default validation against schema
148+ return this . validateAgainstSchema ( registration . schema [ key ] , value )
149+ }
150+
151+ /**
152+ * Basic schema validation
153+ */
154+ private validateAgainstSchema ( fieldDef : any , value : any ) : ValidationResult {
155+ if ( ! fieldDef ) {
156+ return { valid : false , error : 'Unknown preference key' }
157+ }
158+
159+ const { type, validation = { } } = fieldDef
160+
161+ // Type validation
162+ switch ( type ) {
163+ case 'string' :
164+ if ( typeof value !== 'string' ) {
165+ return { valid : false , error : 'Value must be a string' }
166+ }
167+ break
168+ case 'number' :
169+ if ( typeof value !== 'number' ) {
170+ return { valid : false , error : 'Value must be a number' }
171+ }
172+ break
173+ case 'boolean' :
174+ if ( typeof value !== 'boolean' ) {
175+ return { valid : false , error : 'Value must be a boolean' }
176+ }
177+ break
178+ case 'select' :
179+ case 'multiselect' :
180+ // Array values will be validated at Desktop level
181+ break
182+ case 'file' :
183+ case 'directory' :
184+ if ( typeof value !== 'string' ) {
185+ return { valid : false , error : 'Value must be a file path string' }
186+ }
187+ break
188+ }
189+
190+ // Apply validation rules
191+ if ( validation . required && ( value === undefined || value === null || value === '' ) ) {
192+ return { valid : false , error : 'This field is required' }
193+ }
194+
195+ if ( typeof value === 'string' ) {
196+ if ( validation . pattern && ! new RegExp ( validation . pattern ) . test ( value ) ) {
197+ return { valid : false , error : 'Value does not match required pattern' }
198+ }
199+ if ( validation . minLength && value . length < validation . minLength ) {
200+ return { valid : false , error : `Value must be at least ${ validation . minLength } characters` }
201+ }
202+ if ( validation . maxLength && value . length > validation . maxLength ) {
203+ return { valid : false , error : `Value must be no more than ${ validation . maxLength } characters` }
204+ }
205+ }
206+
207+ if ( typeof value === 'number' ) {
208+ if ( validation . min !== undefined && value < validation . min ) {
209+ return { valid : false , error : `Value must be at least ${ validation . min } ` }
210+ }
211+ if ( validation . max !== undefined && value > validation . max ) {
212+ return { valid : false , error : `Value must be no more than ${ validation . max } ` }
213+ }
214+ }
215+
216+ return { valid : true }
217+ }
218+ }
219+
220+ /**
221+ * Global preference registry instance
222+ */
223+ export const preferenceRegistry = PreferenceRegistry . getInstance ( )
0 commit comments