import { getCellLocalPath, setCellLocalPath } from './cell/cellLocal'; import { getOfflineMapDir, setOfflineMapDir } from './map/offlineMap'; type PersistedCaseGlobalConfig = { offlineMapDir?: string; cellLocalPath?: string; }; const normalizeText = (value: unknown) => String(value ?? '').trim(); const isPlainObject = (value: unknown): value is Record => Object.prototype.toString.call(value) === '[object Object]'; export async function readPersistedCaseGlobalConfig(): Promise { const ipcRenderer = (window as any)?.electron?.ipcRenderer; if (!ipcRenderer?.invoke) return {}; try { const payload = await ipcRenderer.invoke('controller/os/readCaseGlobalConfig'); if (!isPlainObject(payload)) return {}; return { offlineMapDir: normalizeText(payload.offlineMapDir), cellLocalPath: normalizeText(payload.cellLocalPath), }; } catch { return {}; } } export async function writePersistedCaseGlobalConfig(patch: Partial): Promise { const ipcRenderer = (window as any)?.electron?.ipcRenderer; if (!ipcRenderer?.invoke) return false; try { const payload: Partial = {}; if (Object.prototype.hasOwnProperty.call(patch, 'offlineMapDir')) { payload.offlineMapDir = normalizeText(patch.offlineMapDir); } if (Object.prototype.hasOwnProperty.call(patch, 'cellLocalPath')) { payload.cellLocalPath = normalizeText(patch.cellLocalPath); } return !!(await ipcRenderer.invoke('controller/os/writeCaseGlobalConfig', payload)); } catch { return false; } } export async function hydratePersistedCaseGlobalConfig() { const config = await readPersistedCaseGlobalConfig(); const offlineMapDir = normalizeText(config.offlineMapDir); const cellLocalPath = normalizeText(config.cellLocalPath); if (offlineMapDir && !getOfflineMapDir()) { setOfflineMapDir(offlineMapDir); } if (cellLocalPath && !getCellLocalPath()) { setCellLocalPath(cellLocalPath); } return { offlineMapDir: getOfflineMapDir(), cellLocalPath: getCellLocalPath(), }; }