import { Injectable } from '@angular/core';
import { ConfigService } from '@shared/config/config.service';
import { FleioApiService } from '../../fleio-api.service';
import { HttpClient } from '@angular/common/http';
import { IConfigOptionModel } from '../model/config-option.model';
import { Observable } from 'rxjs';

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

  hasMatchingCycle(optionId, cycleParams): Observable<{ has_matching_cycle: boolean }> {
    return this.objectGetAction(optionId, 'has_matching_cycle', {
      cycle_params: cycleParams
    });
  }

  getOptionPrice(optionId, currencyCode, cycleParams, optionValue, previousOptions, serviceId,
                 quantity=null): Observable<any> {
    const params = {
      currency_code: currencyCode,
      cycle_params: cycleParams,
      previous_options: previousOptions,
      service_id: serviceId,
    } as Record<string, any>;
    if (optionValue) {
      params.option_value = optionValue;
    }
    if (quantity) {
      params.quantity = quantity;
    }
    return this.objectGetAction(optionId, 'get_option_price', params);
  }
}
