import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {
  BehaviorSubject,
  filter,
  map,
  Observable,
  of,
  switchMap,
  take,
  tap,
  throwError,
} from 'rxjs';

import { BasicSetting } from '../models/basic-setting';

@Injectable({
  providedIn: 'root',
})
export class BasicService {
  // Private
  private __basicSetting = new BehaviorSubject<BasicSetting | undefined>(
    undefined
  );

  /**
   * Constructor
   */
  constructor(private _httpClient: HttpClient) {}

  // -----------------------------------------------------------------------------------------------------
  // @ Accessors
  // -----------------------------------------------------------------------------------------------------

  /**
   * Getter for basicSetting
   */
  get basicSetting$(): Observable<BasicSetting | undefined> {
    return this.__basicSetting.asObservable();
  }

  // -----------------------------------------------------------------------------------------------------
  // @ Public methods
  // -----------------------------------------------------------------------------------------------------

  /**
   * Get basicSetting
   *
   *
   */
  getBasicSetting(): Observable<{
    basicSetting: BasicSetting;
  }> {
    return this._httpClient
      .get<{ basicSetting: BasicSetting }>('api/apps/settings/basic')
      .pipe(
        tap((response) => {
          this.__basicSetting.next(response.basicSetting);
        })
      );
  }
}
