import { IHttpService, IQService } from 'angular';
import { IPromise } from 'q';

export class Magento2ApiService<T> {

  constructor(
    public $http: IHttpService,
    public $q: IQService,
    public API_URL: string,
    public API_ENDPOINT: string
  ) {}

  protected _get(
    slug?: string
  ): IPromise<any> {
    let deferred = this.$q.defer();
    let url = `${this.API_URL}/${this.API_ENDPOINT}`;
    if (slug) {
      url += `/${slug}`;
    }
    this.$http
      .get(url)
      .then((res) => {
        deferred.resolve(res.data);
      }, (reason) => {
        deferred.reject(reason);
      });

    return deferred.promise;
  }

  public get(...args: any[]): IPromise<T> {
    return this._get();
  }
}
