import { Injectable, Signal, inject, signal } from '@angular/core';
import { LocalStorageService } from '../../../../core/storage/local-storage.service';

export type AdminUiDensity = 'comfortable' | 'compact';

const DENSITY_KEY = 'adminPreferences.density.v1';

@Injectable({ providedIn: 'root' })
export class AdminPreferencesService {
  private readonly localStorage = inject(LocalStorageService);

  private readonly densitySignal = signal<AdminUiDensity>(this.readStoredDensity());

  readonly density: Signal<AdminUiDensity> = this.densitySignal.asReadonly();

  setDensity(value: AdminUiDensity): void {
    this.densitySignal.set(value);
    this.localStorage.setItem(DENSITY_KEY, value);
  }

  private readStoredDensity(): AdminUiDensity {
    return this.localStorage.getItem(DENSITY_KEY) === 'compact' ? 'compact' : 'comfortable';
  }
}
