// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log } from './Log';
import { Global } from './Global';

export class JsonService {
    constructor(additionalContentTypes = null, XMLHttpRequestCtor = Global.XMLHttpRequest) {
        if (additionalContentTypes && Array.isArray(additionalContentTypes))
        {
            this._contentTypes = additionalContentTypes.slice();
        }
        else
        {
            this._contentTypes = [];
        }
        this._contentTypes.push('application/json');

        this._XMLHttpRequest = XMLHttpRequestCtor;
    }

    getJson(url, token) {
        if (!url){
            Log.error("JsonService.getJson: No url passed");
            throw new Error("url");
        }

        Log.debug("JsonService.getJson, url: ", url);

        return new Promise((resolve, reject) => {

            var req = new this._XMLHttpRequest();
            req.open('GET', url);

            var allowedContentTypes = this._contentTypes;

            req.onload = function() {
                Log.debug("JsonService.getJson: HTTP response received, status", req.status);

                if (req.status === 200) {

                    var contentType = req.getResponseHeader("Content-Type");
                    if (contentType) {

                        var found = allowedContentTypes.find(item=>{
                            if (contentType.startsWith(item)) {
                                return true;
                            }
                        });

                        if (found) {
                            try {
                                resolve(JSON.parse(req.responseText));
                                return;
                            }
                            catch (e) {
                                Log.error("JsonService.getJson: Error parsing JSON response", e.message);
                                reject(e);
                                return;
                            }
                        }
                    }

                    reject(Error("Invalid response Content-Type: " + contentType + ", from URL: " + url));
                }
                else {
                    reject(Error(req.statusText + " (" + req.status + ")"));
                }
            };

            req.onerror = function() {
                Log.error("JsonService.getJson: network error");
                reject(Error("Network Error"));
            };

            if (token) {
                Log.debug("JsonService.getJson: token passed, setting Authorization header");
                req.setRequestHeader("Authorization", "Bearer " + token);
            }

            req.send();
        });
    }
}

@@ -2828,11 +2828,2340 @@
 );%0A    %7D
+%0A%0A    postForm(url, payload) %7B%0A        if (!url)%7B%0A            Log.error(%22JsonService.postForm: No url passed%22);%0A            throw new Error(%22url%22);%0A        %7D%0A%0A        Log.debug(%22JsonService.postForm, url: %22, url);%0A%0A        return new Promise((resolve, reject) =%3E %7B%0A%0A            var req = new this._XMLHttpRequest();%0A            req.open('POST', url);%0A%0A            var allowedContentTypes = this._contentTypes;%0A%0A            req.onload = function() %7B%0A                Log.debug(%22JsonService.postForm: HTTP response received, status%22, req.status);%0A%0A                if (req.status === 200) %7B%0A%0A                    var contentType = req.getResponseHeader(%22Content-Type%22);%0A                    if (contentType) %7B%0A%0A                        var found = allowedContentTypes.find(item=%3E%7B%0A                            if (contentType.startsWith(item)) %7B%0A                                return true;%0A                            %7D%0A                        %7D);%0A%0A                        if (found) %7B%0A                            try %7B%0A                                resolve(JSON.parse(req.responseText));%0A                                return;%0A                            %7D%0A                            catch (e) %7B%0A                                Log.error(%22JsonService.postForm: Error parsing JSON response%22, e.message);%0A                                reject(e);%0A                                return;%0A                            %7D%0A                        %7D%0A                    %7D%0A%0A                    reject(Error(%22Invalid response Content-Type: %22 + contentType + %22, from URL: %22 + url));%0A                %7D%0A                else %7B%0A                    reject(Error(req.statusText + %22 (%22 + req.status + %22)%22));%0A                %7D%0A            %7D;%0A%0A            req.onerror = function() %7B%0A                Log.error(%22JsonService.postForm: network error%22);%0A                reject(Error(%22Network Error%22));%0A            %7D;%0A%0A            let body = %22%22;%0A            for(let key in payload) %7B%0A                if (body.length %3E 0) %7B%0A                    body += %22&%22;%0A                %7D%0A                body += encodeURIComponent(key);%0A                body += %22=%22;%0A                body += encodeURIComponent(payload%5Bkey%5D);%0A            %7D%0A%0A            req.setRequestHeader(%22Content-Type%22, %22application/x-www-form-urlencoded%22);%0A            req.send(body);%0A        %7D);%0A    %7D
 %0A%7D%0A

add a form post API