const STORAGE_KEY = 'docs-public-settings';

export function readCachedPublicSettings(): Record<string, string> {
  if (typeof window === 'undefined') {
    return {};
  }
  try {
    const raw = localStorage.getItem(STORAGE_KEY);
    if (!raw) {
      return {};
    }
    const parsed = JSON.parse(raw) as Record<string, string>;
    return parsed && typeof parsed === 'object' ? parsed : {};
  } catch {
    return {};
  }
}

export function writeCachedPublicSettings(settings: Record<string, string>) {
  if (typeof window === 'undefined') {
    return;
  }
  try {
    localStorage.setItem(STORAGE_KEY, JSON.stringify(settings));
  } catch {
    // ignore quota errors
  }
}

export function mergePublicSettings(...sources: Array<Record<string, string>>): Record<string, string> {
  return Object.assign({}, ...sources);
}
