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 { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { IOrderOptionsModel } from '@fleio-api/billing/cart/model/order-options.model';
import { Observable } from 'rxjs/internal/Observable';

@Injectable({
  providedIn: 'root'
})
export class CartItemsApiService extends FleioApiService<any> {
  constructor(protected httpClient: HttpClient, protected config: ConfigService) {
    super();
    this.setEndpoint(config.getPanelApiUrl('billing/cart/items'));
  }

  getOrderOptions(code: FleioId): Observable<IOrderOptionsModel> {
    return this.getAction(
      'create_options', {product_code: code}
    );
  }

  getEditOptions(id: FleioId): Observable<IOrderOptionsModel> {
    return this.objectGetAction(id, 'edit_options');
  }

  startOrder(clientId: FleioId, currency: string) {
    return this.postAction('start_order', {
      client_id: clientId,
      currency: currency,
    });
  }

  dropCart(): Observable<any> {
    return this.postAction('drop_cart');
  }

  orderProduct(
    productId: FleioId,
    cycleId: FleioId,
    configurableOptions: any[],
    domainAction = null,
    domainName = null,
    pluginData: Record<any, any>,
    clientId = null,
  ): Observable<any> {
    const params = {
      product: productId,
      cycle: cycleId,
      configurable_options: configurableOptions,
      domain_action: domainAction,
      domain_name: domainName,
      plugin_data: pluginData,
    } as any;
    if (clientId) {
      params.client_id = clientId;
    }
    return this.create(params);
  }
}
