
import { Injectable } from '@angular/core'
import { HttpClient } from '@angular/common/http'

import { HelperService }    from '@k-services/svc.helper'

interface List {
    status: string;
    data: {
        errorMessage?: string;
        list?: [
            {
                value: string;
                label: string;
            }
        ]
    }
}


@Injectable()
export class getStoresService {
    
    constructor(
        private _http: HttpClient,
        private _helper: HelperService
    ) {}

    // Variables ----
	api = `${this._helper.server}feed/get`
    key = this._helper.credentials



    getList() {

        let request = `/curtain-retailers${this.key}&noCache`

        return this._http.get(this.api + request).toPromise().then((response: List) => {

            switch(response.status) {
                case 'error':
                    throw response.data.errorMessage
            }

            return response.data

        }).catch((error) => {

            if(typeof error == 'object') {
                this._helper.logRequestError(error.statusText, error.status, error.message, '/app-core/modules/storeDropdown/services/svc.geStores.ts:35')
            } else {
                this._helper.logRequestError(error, 'GET', request, '/app-core/modules/storeDropdown/services/svc.geStores.ts:35')
            }

            return []
        })
    }
}


