import { app } from 'electron' import { readFile, writeFile, mkdir } from 'node:fs/promises' import { join } from 'node:path' import type { Settings } from '@shared/types' import { SETTINGS_FILE, QUALITY_THRESHOLDS } from '@shared/constants' import { DEFAULT_LANG } from '@shared/i18n' const DEFAULTS: Settings = { language: DEFAULT_LANG, // 기본 한국어 theme: 'dark', // 기본 다크모드 onboarded: false, qualityThresholds: { ...QUALITY_THRESHOLDS }, easyMode: false } /** 앱 설정(언어/테마/온보딩) 영속화. userData/settings.json */ class SettingsStore { private settings: Settings = { ...DEFAULTS } private loaded = false private filePath(): string { return join(app.getPath('userData'), SETTINGS_FILE) } async load(): Promise { if (this.loaded) return this.settings try { const raw = await readFile(this.filePath(), 'utf-8') const parsed = JSON.parse(raw) as Partial this.settings = { ...DEFAULTS, ...parsed } } catch { this.settings = { ...DEFAULTS } } this.loaded = true return this.settings } /** 동기 접근(load 이후). 메뉴 빌드 등에서 사용 */ current(): Settings { return this.settings } async set(patch: Partial): Promise { await this.load() this.settings = { ...this.settings, ...patch } await mkdir(app.getPath('userData'), { recursive: true }) await writeFile(this.filePath(), JSON.stringify(this.settings, null, 2), 'utf-8') return this.settings } } export const settingsStore = new SettingsStore()