import { Injectable } from "@angular/core";
import { Observable, map } from "rxjs";
import { CompanyModel } from "../models/company";
import { company } from "../data/company";

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

  companyData = new Observable<CompanyModel[]>((subscriber) => {
    subscriber.next(company);
  });

  // Get all companies
  getAllCompanies(): Observable<CompanyModel[]> {
    return this.companyData;
  }

  // Get a company based on id
  getCompanyById(id: number): Observable<CompanyModel> {
    return this.companyData.pipe(
      map((companies) => companies.find((company) => company.id === id))
    );
  }
}
