export const PERMISSIONS_UPDATED_EVENT = 'permissions-updated';

export function readPermissions(): string[] {
  try {
    const value = JSON.parse(localStorage.getItem('permissions') || '[]');
    return Array.isArray(value)
      ? value.filter((item): item is string => typeof item === 'string')
      : [];
  } catch {
    return [];
  }
}

export function writePermissions(permissions: string[]): void {
  localStorage.setItem('permissions', JSON.stringify([...new Set(permissions)]));
  window.dispatchEvent(new Event(PERMISSIONS_UPDATED_EVENT));
}
