import { __awaiter } from "tslib";
import qs from 'query-string';
import BaseListGetterApiConsumer from '../base-list-getter-api-consumer/base-list-getter-api-consumer';
export default class EndpointListGetterApiConsumer extends BaseListGetterApiConsumer {
    // instance -- axios instance.js
    constructor({ baseUrl, instance }, params = {}) {
        super(params);
        this.instance = instance;
        this.baseUrl = baseUrl;
        this.instance = instance;
        if (params.nestedUrl)
            this.nestedUrl = params.nestedUrl;
    }
    getList(params) {
        return this._getList(params);
    }
    getNestedList(params) {
        const baseUrl = `${this.baseUrl}/${params.parentId}/${this.nestedUrl}`;
        return this._getList(params, baseUrl);
    }
    _getList({ page = 1, size = 10, search, sort, fields, id, searchQuery = 'q', rest = {}, }, baseUrl = this.baseUrl) {
        return __awaiter(this, void 0, void 0, function* () {
            const stringifyOptions = {
                skipEmptyString: true,
                skipNull: true,
            };
            // tslint:disable-next-line: no-parameter-reassignment
            if (search && search.slice(-1) !== '*')
                search += '*';
            let url = `${baseUrl}?size=${size}&page=${page}`;
            if (search)
                url += `&${searchQuery}=${search}`;
            url += `&${qs.stringify({ sort, fields, id }, stringifyOptions)}`;
            if (Object.keys(rest).length) {
                url += `&${qs.stringify(rest, stringifyOptions)}`;
            }
            try {
                let response = yield this.instance.get(url);
                response = this.responseHandler(response);
                if (this.userResponseHandler) {
                    response = this.userResponseHandler(response);
                }
                return response;
            }
            catch (err) {
                throw err;
            }
        });
    }
}
//# sourceMappingURL=endpoint-list-getter-api-consumer.js.map