import { Injectable } from "@angular/core";
import { Observable, map } from "rxjs";
import { DatabaseInfoModel } from "../models/database-info";
import { databaseInfo } from "../data/database-info";

@Injectable({
  providedIn: "root",
})
export class DatabaseInfoService {
  constructor() {}

  databaseInfoData = new Observable<DatabaseInfoModel[]>((subscriber) => {
    subscriber.next(databaseInfo);
  });

  // Get all database info
  getAllDatabaseInfo(): Observable<DatabaseInfoModel[]> {
    return this.databaseInfoData;
  }

  // Get a database info based on id
  getDatabaseInfoById(id: number): Observable<DatabaseInfoModel> {
    return this.databaseInfoData.pipe(
      map((databaseInfo) =>
        databaseInfo.find((databaseInfo) => databaseInfo.id === id)
      )
    );
  }
}
