import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { IPricingPlanModel } from '../model/pricing-plan.model';
import { FleioApiService } from '../../fleio-api.service';
import { ConfigService } from '@shared/config/config.service';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class PricingPlansApiService extends FleioApiService<IPricingPlanModel> {
  // noinspection JSUnusedGlobalSymbols
  constructor(protected httpClient: HttpClient, protected config: ConfigService) {
    super();
    this.setEndpoint(config.getPanelApiUrl('openstack/billing/plan'));
  }

  getAlternativePlans(currentPlan: IPricingPlanModel, isStaff: boolean):
    Observable<IPricingPlanModel[]> {
    let filtering: string;
    if (isStaff) {
      filtering = `id__ne:${currentPlan.id}`;
    }
    return this.list({
      filtering
    }).pipe(map(plans => plans.objects));
  }
}
