var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { Util } from "../util/util";
export class Http {
    constructor() {
        this.optionsDefault = {
            cache: 'no-cache',
            credentials: 'same-origin',
            headers: {},
            mode: 'same-origin',
            method: 'GET',
        };
        this._interceptors = [];
    }
    get(url, options = {}) {
        return __awaiter(this, void 0, void 0, function* () {
            let preflight = this.preflightGet(url, options);
            return yield fetch(preflight.url, preflight.options)
                .then(response => response.json()) // or text() or blob() etc.
                .then(data => {
                return data;
            })
                .catch(err => {
                console.error(err);
            });
        });
    }
    put(url, object, options) {
    }
    post(url, object, options) {
    }
    delete(url, object, options) {
    }
    preflightGet(url, options = {}) {
        let defaults = Util.copy(this.optionsDefault);
        options = Object.assign({}, defaults, options);
        if (options.params) {
            url += (url.indexOf('?') === -1 ? '?' : '&') + this.queryParams(options.params);
            delete options.params;
        }
        let parsed = this.applyInterceptors({ type: 'request', url: url, options: options });
        url = parsed[0];
        options = parsed[1];
        return { url: url, options: options };
    }
    queryParams(params) {
        return Object.keys(params)
            .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
            .join('&');
    }
    applyInterceptors(options) {
        let parsed;
        if (options.type === 'request') {
            parsed = [options.url, options.options];
            this.interceptors.forEach((interceptor) => {
                if (interceptor.request) {
                    parsed = interceptor.request(parsed[0], parsed[1]);
                    // console.log(parsed.url);
                    // if(parsed.url !== options.url) {}
                }
            });
            return parsed;
        }
    }
    flushInterceptors() {
        this._interceptors = [];
    }
    registerInterceptor(obj) {
        this._interceptors.push(obj);
    }
    unregisterInterceptor(obj) {
        let i = 0;
        this._interceptors.forEach((interceptor) => {
            if (interceptor === obj) {
                this._interceptors.splice(i, 1);
            }
            i++;
        });
    }
    get interceptors() {
        return this._interceptors;
    }
}
//# sourceMappingURL=http.js.map