"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const APPLICATION_JSON = 'application/json';
const APPLICATION_OCTET = 'application/octet-stream';
const TEXT = 'text/plain';
const FETCH_MODE = 'cors';
const CACHE_MODE = 'no-cache';
const BEARER = 'Bearer';
const DELETE_METHOD = 'DELETE';
const PUT_METHOD = 'PUT';
const POST_METHOD = 'POST';
const GET_METHOD = 'GET';
const VERSION = '/api/v1/';
class BaseAPIService {
    constructor(baseURL, subUrl, token) {
        this.baseURL = baseURL;
        this.subUrl = subUrl;
        this.token = token;
        this.version = VERSION;
    }
    list() {
        return __awaiter(this, void 0, void 0, function* () {
            const url = `${this.baseURL}${this.version}${this.subUrl}`;
            const response = yield fetch(url, {
                method: GET_METHOD,
                cache: CACHE_MODE,
                headers: {
                    Authorization: `${BEARER} ${this.token}`,
                    Accept: APPLICATION_JSON,
                },
            });
            return response.json();
        });
    }
    listByPath(path) {
        return __awaiter(this, void 0, void 0, function* () {
            const url = `${this.baseURL}${this.version}${this.subUrl}${path}`;
            const response = yield fetch(url, {
                method: GET_METHOD,
                cache: CACHE_MODE,
                headers: {
                    Authorization: `${BEARER} ${this.token}`,
                    Accept: APPLICATION_JSON,
                },
            });
            return response.json();
        });
    }
    get(path) {
        return __awaiter(this, void 0, void 0, function* () {
            const url = `${this.baseURL}${this.version}${this.subUrl}/${path}`;
            const response = yield fetch(url, {
                method: GET_METHOD,
                cache: CACHE_MODE,
                headers: {
                    Authorization: `${BEARER} ${this.token}`,
                    Accept: APPLICATION_JSON,
                },
            });
            return response.json();
        });
    }
    getString(path) {
        return __awaiter(this, void 0, void 0, function* () {
            const url = `${this.baseURL}${this.version}${this.subUrl}/${path}`;
            const response = yield fetch(url, {
                method: GET_METHOD,
                cache: CACHE_MODE,
                headers: {
                    Authorization: `${BEARER} ${this.token}`,
                    Accept: TEXT,
                },
            });
            return response.text();
        });
    }
    getArrayBuffer(path) {
        return __awaiter(this, void 0, void 0, function* () {
            const url = `${this.baseURL}${this.version}${this.subUrl}/${path}`;
            const response = yield fetch(url, {
                method: GET_METHOD,
                cache: CACHE_MODE,
                headers: {
                    Authorization: `${BEARER} ${this.token}`,
                    Accept: APPLICATION_OCTET,
                },
            });
            return response.arrayBuffer();
        });
    }
    putWithContentType(path, payload, contentType) {
        return __awaiter(this, void 0, void 0, function* () {
            const url = `${this.baseURL}${this.version}${this.subUrl}/${path}`;
            const response = yield fetch(url, {
                method: PUT_METHOD,
                mode: FETCH_MODE,
                headers: {
                    'Content-Type': contentType,
                    Authorization: `${BEARER} ${this.token}`,
                    Accept: APPLICATION_JSON,
                },
                body: JSON.stringify(payload),
            });
            const cType = response.headers.get('content-type');
            if (cType && cType.indexOf(contentType) !== -1) {
                return response.json();
            }
            return Promise.resolve();
        });
    }
    postWithContentType(path, payload, contentType) {
        return __awaiter(this, void 0, void 0, function* () {
            const url = `${this.baseURL}${this.version}${this.subUrl}/${path}`;
            const response = yield fetch(url, {
                method: POST_METHOD,
                mode: FETCH_MODE,
                headers: {
                    'Content-Type': contentType,
                    Authorization: `${BEARER} ${this.token}`,
                    Accept: APPLICATION_JSON,
                },
                body: JSON.stringify(payload),
            });
            const cType = response.headers.get('content-type');
            if (cType && cType.indexOf(contentType) !== -1) {
                return response.json();
            }
            return Promise.resolve();
        });
    }
    postFormData(path, data) {
        return __awaiter(this, void 0, void 0, function* () {
            const url = `${this.baseURL}${this.version}${this.subUrl}/${path}`;
            const response = yield fetch(url, {
                method: POST_METHOD,
                mode: FETCH_MODE,
                headers: {
                    Authorization: `${BEARER} ${this.token}`,
                    Accept: APPLICATION_JSON,
                },
                body: data,
            });
            const cType = response.headers.get('content-type');
            if (cType && cType.indexOf(APPLICATION_JSON) !== -1) {
                return response.json();
            }
            return Promise.resolve();
        });
    }
    filterWithContentType(payload, contentType) {
        return __awaiter(this, void 0, void 0, function* () {
            const url = `${this.baseURL}${this.version}${this.subUrl}/filter`;
            const response = yield fetch(url, {
                method: POST_METHOD,
                mode: FETCH_MODE,
                headers: {
                    'Content-Type': contentType,
                    Authorization: `${BEARER} ${this.token}`,
                    Accept: APPLICATION_JSON,
                },
                body: JSON.stringify(payload),
            });
            const cType = response.headers.get('content-type');
            if (cType && cType.indexOf(contentType) !== -1) {
                return response.json();
            }
            return Promise.reject();
        });
    }
    filterString(payload, path) {
        return __awaiter(this, void 0, void 0, function* () {
            const url = `${this.baseURL}${this.version}${this.subUrl}/${path}`;
            const response = yield fetch(url, {
                method: POST_METHOD,
                mode: FETCH_MODE,
                headers: {
                    'Content-Type': APPLICATION_JSON,
                    Authorization: `${BEARER} ${this.token}`,
                    Accept: TEXT,
                },
                body: JSON.stringify(payload),
            });
            return response.text();
        });
    }
    delete(path) {
        return __awaiter(this, void 0, void 0, function* () {
            const url = `${this.baseURL}${this.version}${this.subUrl}/${path}`;
            const response = yield fetch(url, {
                method: DELETE_METHOD,
                headers: {
                    Authorization: `${BEARER} ${this.token}`,
                },
            });
            if (response.ok) {
                return Promise.resolve();
            }
            return Promise.reject();
        });
    }
    deleteWithReturn(path) {
        return __awaiter(this, void 0, void 0, function* () {
            const url = `${this.baseURL}${this.version}${this.subUrl}/${path}`;
            const response = yield fetch(url, {
                method: DELETE_METHOD,
                headers: {
                    Authorization: `${BEARER} ${this.token}`,
                },
            });
            const cType = response.headers.get('content-type');
            if (cType && cType.indexOf(APPLICATION_JSON) !== -1) {
                return response.json();
            }
            return Promise.reject();
        });
    }
    put(path, payload) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.putWithContentType(path, payload, APPLICATION_JSON);
        });
    }
    putNoPayload(path) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.putWithContentType(path, undefined, APPLICATION_JSON);
        });
    }
    post(path, payload) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.postWithContentType(path, payload, APPLICATION_JSON);
        });
    }
    postNoPayload(path) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.postWithContentType(path, undefined, APPLICATION_JSON);
        });
    }
    clone(payload) {
        return __awaiter(this, void 0, void 0, function* () {
            const url = `${this.baseURL}${this.version}${this.subUrl}/clone`;
            const response = yield fetch(url, {
                method: POST_METHOD,
                mode: FETCH_MODE,
                headers: {
                    'Content-Type': APPLICATION_JSON,
                    Authorization: `${BEARER} ${this.token}`,
                    Accept: APPLICATION_JSON,
                },
                body: JSON.stringify(payload),
            });
            const cType = response.headers.get('content-type');
            if (cType && cType.indexOf(APPLICATION_JSON) !== -1) {
                return response.json();
            }
            return Promise.resolve();
        });
    }
    filter(payload) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.filterWithContentType(payload, APPLICATION_JSON);
        });
    }
}
exports.default = BaseAPIService;
