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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import z from 'zod';
import { makeRequest } from './http.js';
import { createLogger } from '../logging/logger.js';
import { Cache } from './cache.js';

const logger = createLogger('sync');

/**
 * Configuration for a SyncManager instance.
 */
export interface SyncManagerConfig {
  /** Unique cache key prefix for this manager */
  cacheKey: string;
  /** Max items in the cache */
  maxCacheSize: number;
  /** Refresh interval in seconds (0 = no refresh) */
  refreshInterval: number;
  /** Statically configured URLs from env vars */
  configuredUrls: string[];
  /** Zod schema to validate items fetched from URLs */
  itemSchema: z.ZodType<any>;
  /** Extract the unique key string from a raw fetched item */
  itemKey: (item: any) => string;
  /** Convert a plain string from a `values` array into a typed item */
  convertValue: (value: string) => any;
}

/**
 * A raw item as fetched from a sync URL.
 * All sync URLs must return either:
 *   - An array of objects matching the itemSchema
 *   - An object with a `values` array of strings
 */
export type RawSyncItem =
  | { name: string; pattern: string; score?: number }
  | {
      expression: string;
      name?: string;
      score?: number;
    };

/**
 * Result of fetching items from a single URL, including any error.
 */
export interface FetchResult<T> {
  url: string;
  items: T[];
  error?: string;
}

/**
 * Base class for managing synced items from remote URLs.
 * Handles fetching, caching, periodic refresh, accumulation, and override logic.
 */
export class SyncManager<T extends Record<string, any>> {
  private _initialisationPromise: Promise<void> | null = null;
  private _refreshInterval: ReturnType<typeof setInterval> | null = null;
  private _dynamicUrls = new Set<string>();
  private _accumulatedKeys = new Set<string>();
  private _allowedUrls = new Set<string>();

  public readonly cache: Cache<string, { items: T[] }>;
  protected readonly config: SyncManagerConfig;

  /**
   * Cache TTL is set to max(3× refresh interval, 24 hours) so that if a
   * scheduled refresh fails, the previously-cached data survives and
   * continues to be served during normal usage. Minimum 24 hours ensures
   * sufficient buffer even for short refresh intervals.
   */
  private readonly _cacheTTL: number;

  constructor(config: SyncManagerConfig) {
    this.config = config;
    this._cacheTTL =
      config.refreshInterval > 0
        ? Math.max(config.refreshInterval * 3, 86400)
        : 86400;
    this.cache = Cache.getInstance<string, { items: T[] }>(
      config.cacheKey,
      config.maxCacheSize
    );
    this._allowedUrls = new Set(config.configuredUrls);
  }

  /**
   * Initialise the sync manager: perform initial fetch and start periodic refresh.
   */
  public initialise(): Promise<void> {
    if (!this._initialisationPromise) {
      this._initialisationPromise = this._refresh().then(() => {
        logger.info(
          {
            items: this._accumulatedKeys.size,
            type: this.config.cacheKey,
            refreshInterval: this.config.refreshInterval,
          },
          `initialised sync manager`
        );
        if (this.config.refreshInterval > 0) {
          this._refreshInterval = setInterval(
            () => this._refresh(),
            this.config.refreshInterval * 1000
          );
        }
      });
    }
    return this._initialisationPromise;
  }

  /**
   * Clean up resources (clear refresh interval).
   */
  public cleanup(): void {
    if (this._refreshInterval) {
      clearInterval(this._refreshInterval);
      this._refreshInterval = null;
    }
  }

  /**
   * Fetch items from a single URL with caching and retry.
   */
  public async fetchFromUrl(url: string, forceRefresh = false): Promise<T[]> {
    if (!forceRefresh) {
      const cached = await this.cache.get(url);
      if (cached) {
        return cached.items;
      }
    }

    const items = await this._fetchWithRetry(url);
    if (items.length > 0) {
      await this.cache.set(url, { items }, this._cacheTTL);
    }
    return items;
  }

  /**
   * Fetch items from a URL, returning both items and any error.
   * Used by the API route to forward errors to the client.
   */
  public async fetchFromUrlWithError(url: string): Promise<FetchResult<T>> {
    try {
      const cached = await this.cache.get(url);
      if (cached) {
        return { url, items: cached.items };
      }
      const items = await this._fetchWithRetry(url);
      if (items.length > 0) {
        await this.cache.set(url, { items }, this._cacheTTL);
      }
      return { url, items };
    } catch (error: any) {
      return { url, items: [], error: error.message };
    }
  }

  /**
   * Validate URLs against the whitelist and user trust level.
   * Subclasses override this to implement their own access logic.
   */
  public validateUrls(
    urls: string[],
    _userData?: { trusted?: boolean }
  ): string[] {
    return urls.filter((url) => this.config.configuredUrls.includes(url));
  }

  /**
   * Get all accumulated item keys (used for whitelist checks).
   */
  public get accumulatedKeys(): Set<string> {
    return this._accumulatedKeys;
  }

  /**
   * Add items to the accumulated set.
   */
  public addItems(items: T[]): void {
    const initialCount = this._accumulatedKeys.size;
    for (const item of items) {
      this._accumulatedKeys.add(this.config.itemKey(item));
    }
    const newCount = this._accumulatedKeys.size - initialCount;
    if (newCount > 0) {
      logger.info(
        {
          newCount,
          total: this._accumulatedKeys.size,
          type: this.config.cacheKey,
        },
        `accumulated new items`
      );
    }
  }

  /**
   * Add URLs to the allowed list.
   * URLs added this way are considered trusted and can be used for syncing.
   */
  public addAllowedUrls(urls: string[]): void {
    const initialCount = this._allowedUrls.size;
    for (const url of urls) {
      if (this.isValidUrl(url)) {
        this._allowedUrls.add(url);
      } else {
        logger.warn(
          { url, type: this.config.cacheKey },
          'skipping invalid URL'
        );
      }
    }
    const newCount = this._allowedUrls.size - initialCount;
    if (newCount > 0) {
      logger.info(
        { newCount, total: this._allowedUrls.size, type: this.config.cacheKey },
        `added ${newCount} new allowed URLs`
      );
    }
  }

  /**
   * Get all allowed URLs (configured + dynamically added).
   */
  public get allowedUrls(): string[] {
    return Array.from(this._allowedUrls);
  }

  /**
   * Check if a URL is valid.
   */
  public isValidUrl(url: string): boolean {
    try {
      const parsed = new URL(url);
      return parsed.protocol === 'http:' || parsed.protocol === 'https:';
    } catch {
      return false;
    }
  }

  /**
   * Check if a URL is in the allowed list.
   */
  public isAllowedUrl(url: string): boolean {
    return this._allowedUrls.has(url);
  }

  /**
   * Refresh items from all configured + dynamic URLs.
   */
  private async _refresh(): Promise<void> {
    const allUrls = [
      ...new Set([...this.config.configuredUrls, ...this._dynamicUrls]),
    ];

    if (allUrls.length === 0) return;

    logger.debug(
      { count: allUrls.length, type: this.config.cacheKey },
      'refresh started for all URLs'
    );

    const results = await Promise.allSettled(
      allUrls.map((url) =>
        this._fetchWithRetry(url)
          .then((items) => {
            if (items.length > 0) {
              this.cache.set(url, { items }, this._cacheTTL);
            }
            return items;
          })
          .catch((err) => {
            logger.error(
              { url, type: this.config.cacheKey, error: err.message },
              'background refresh failed'
            );
            return [] as T[];
          })
      )
    );

    const items = results
      .filter((r): r is PromiseFulfilledResult<T[]> => r.status === 'fulfilled')
      .flatMap((r) => r.value);

    if (items.length > 0) {
      this.addItems(items);
    }
  }

  /**
   * Fetch items from a URL.
   */
  private async _fetchWithRetry(url: string): Promise<T[]> {
    logger.debug(
      {
        type: this.config.cacheKey,
        url,
      },
      'fetching from URL'
    );

    try {
      const response = await makeRequest(url, {
        method: 'GET',
        headers: { 'Content-Type': 'application/json' },
        timeout: 5000,
      });

      if (!response.ok) {
        throw new Error(
          `HTTP ${response.status} ${response.statusText} during sync of ${url}`
        );
      }

      const data = await response.json();

      // Try parsing as array of items first
      const arrayResult = z.array(this.config.itemSchema).safeParse(data);
      if (arrayResult.success) {
        return arrayResult.data as T[];
      }

      // Try parsing as { values: string[] }
      const valuesResult = z
        .object({ values: z.array(z.string()) })
        .safeParse(data);
      if (valuesResult.success) {
        return this._convertValuesArray(valuesResult.data.values);
      }

      // Format mismatch detection: give helpful error messages
      if (Array.isArray(data) && data.length > 0) {
        const first = data[0];
        if (typeof first === 'object' && first !== null) {
          const keys = Object.keys(first);
          // Detect ranked-format data in a non-ranked slot
          if (
            keys.includes('expression') &&
            keys.includes('score') &&
            !this.config.itemSchema.safeParse(first).success
          ) {
            throw new Error(
              `Format mismatch: URL returns ranked data ({expression, score}) but this section expects {values: string[]}. ` +
                `Did you put this URL in the wrong section? Try the Ranked section instead.`
            );
          }
          if (
            keys.includes('pattern') &&
            keys.includes('score') &&
            !this.config.itemSchema.safeParse(first).success
          ) {
            throw new Error(
              `Format mismatch: URL returns ranked data ({pattern, score}) but this section expects {values: string[]}. ` +
                `Did you put this URL in the wrong section? Try the Ranked section instead.`
            );
          }
          // Detect values-format data in a ranked slot
          if (keys.includes('values')) {
            throw new Error(
              `Format mismatch: URL returns simple data ({values: string[]}) but this section expects [{expression, score}]. ` +
                `Did you put this URL in the wrong section? Try the Required/Excluded/Included/Preferred section instead.`
            );
          }
        }
      }
      if (
        typeof data === 'object' &&
        data !== null &&
        'values' in data &&
        !Array.isArray(data.values)
      ) {
        throw new Error(
          `Invalid format: 'values' field must be an array of strings.`
        );
      }

      throw new Error(
        `Unexpected format from URL. Expected either an array of items or {values: string[]}. Got: ${JSON.stringify(data).slice(0, 200)}`
      );
    } catch (error: any) {
      logger.error(
        { url, type: this.config.cacheKey, error: error.message },
        'failed to fetch from URL'
      );
      throw error;
    }
  }

  /**
   * Convert a `values` string array into typed items using the config converter.
   */
  protected _convertValuesArray(values: string[]): T[] {
    return values.map((v) => this.config.convertValue(v) as T);
  }
}

export interface SyncOverride {
  /** For regex overrides */
  pattern?: string;
  /** For SEL overrides */
  expression?: string;
  name?: string;
  score?: number;
  originalName?: string;
  /** Extracted names from SEL expression comments, used for matching */
  exprNames?: string[];
  disabled?: boolean;
}

/** Parse a `<SYNCED: url>` placeholder, returning the URL or null. */
export function parseSyncedUrl(value: string): string | null {
  if (!value.startsWith('<SYNCED: ') || !value.endsWith('>')) return null;
  const url = value.slice(9, -1).trim();
  return url.length > 0 ? url : null;
}