// Extracted from src/db/repository.ts (mechanical split — bodies unchanged).
// shared repository domain. Facade: src/db/repository.ts (Repository class).

/** Escape LIKE wildcards so a caller-supplied prefix matches literally. */
export function escapeLike(s: string): string {
  return s.replace(/[\\%_]/g, (c) => '\\' + c);
}


export function encodeTags(values: string[]): string {
  const unique = Array.from(new Set(values.filter(Boolean)));
  return `,${unique.join(',')},`;
}


export function decodeTags(raw: string | null): string[] {
  if (!raw) return [];
  return raw.split(',').map((value) => value.trim()).filter(Boolean);
}


export function decodeAvailableModels(raw: string | null): string[] {
  if (!raw) return [];
  try {
    const parsed = JSON.parse(raw) as unknown;
    return Array.isArray(parsed) ? parsed.filter((value): value is string => typeof value === 'string') : [];
  } catch {
    return [];
  }
}


/** SQLite datetime('now') は UTC だがタイムゾーン情報なし。'Z' を付加して ISO 8601 UTC として明示する */
export function utc(dt: string | null): string {
  if (!dt) return '';
  // 既に Z や +/- オフセットが付いていれば何もしない
  if (/[Zz]$/.test(dt) || /[+-]\d{2}:\d{2}$/.test(dt)) return dt;
  return dt.replace(' ', 'T') + 'Z';
}


export function localTaskRepoName(taskId: number): string {
  return `local/task-${taskId}`;
}
