import { RootStore, Substore } from './rootStore';
import { makeAutoObservableSubstore } from './utils';

export class NavigationStore implements Substore {
  navigationHistory: { path: string; timestamp: string }[] = [];

  readonly root: RootStore;
  get apiClient() {
    return this.root.apiClient;
  }
  // DO NOT ADD INIT LOGIC IN constructor. this will run on every page! considering adding any it in the component init logic instead
  constructor(root: RootStore) {
    this.root = root;
    makeAutoObservableSubstore(this);
  }

  addToHistory(path: string) {
    this.navigationHistory.push({
      path,
      timestamp: new Date().toISOString(),
    });

    // Keep only the last 5 entries
    if (this.navigationHistory.length > 5) {
      this.navigationHistory = this.navigationHistory.slice(-5);
    }
  }
}
