"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var axios_1 = __importDefault(require("axios"));
var rxjs_1 = require("rxjs");
var Reset = '\x1b[0m';
var Bright = '\x1b[1m';
var Dim = '\x1b[2m';
var Underscore = '\x1b[4m';
var Blink = '\x1b[5m';
var Reverse = '\x1b[7m';
var Hidden = '\x1b[8m';
var FgBlack = '\x1b[30m';
var FgRed = '\x1b[31m';
var FgGreen = '\x1b[32m';
var FgYellow = '\x1b[33m';
var FgBlue = '\x1b[34m';
var FgMagenta = '\x1b[35m';
var FgCyan = '\x1b[36m';
var FgWhite = '\x1b[37m';
var BgBlack = '\x1b[40m';
var BgRed = '\x1b[41m';
var BgGreen = '\x1b[42m';
var BgYellow = '\x1b[43m';
var BgBlue = '\x1b[44m';
var BgMagenta = '\x1b[45m';
var BgCyan = '\x1b[46m';
var BgWhite = '\x1b[47m';
var AxiosSubscriber = (function (_super) {
    __extends(AxiosSubscriber, _super);
    function AxiosSubscriber(observer, axiosRequestConfig, httpService) {
        var _this = _super.call(this, observer) || this;
        _this.source = null;
        _this.completed = false;
        _this.source = axios_1.default.CancelToken.source();
        if (httpService.mockError) {
            var errorMessage = {
                success: false,
                detail: {
                    description: 'Erro ao conectar com servidor, verifique sua internet',
                },
            };
            observer.error(errorMessage);
            observer.unsubscribe();
            return _this;
        }
        axiosRequestConfig.cancelToken = _this.source.token;
        console.log(BgBlue + " " + axiosRequestConfig.method.toUpperCase() + " " + "\u001B[0m", axiosRequestConfig.url);
        axios_1.default
            .request(axiosRequestConfig)
            .then(function (response) {
            if (typeof response.data === 'string') {
                observer.error({
                    success: false,
                    detail: {
                        description: 'Erro ao realizar ação, tente novamente mais tarde',
                    },
                });
            }
            else {
                console.log(BgGreen + " Success " + "\u001B[0m", response.data);
                observer.next(response.data);
                _this.completed = true;
                observer.complete();
                observer.unsubscribe();
            }
        })
            .catch(function (error) {
            _this.completed = true;
            httpService.listenersErrors.forEach(function (handle) { return handle(error); });
            var errorMessage = null;
            try {
                errorMessage = error.response.data;
            }
            catch (e) {
                errorMessage = {
                    success: false,
                    detail: {
                        description: 'Erro ao conectar com servidor, verifique sua internet',
                    },
                };
            }
            console.log(BgRed + " Error " + "\u001B[0m", errorMessage);
            observer.error(errorMessage);
            observer.unsubscribe();
        });
        return _this;
    }
    AxiosSubscriber.prototype.unsubscribe = function () {
        if (!this.completed && this.source) {
            this.source.cancel();
        }
    };
    return AxiosSubscriber;
}(rxjs_1.Subscriber));
var HttpService = (function () {
    function HttpService() {
        this.defaults = {
            headers: {},
        };
        this.listenersErrors = [];
        this.mockError = false;
    }
    HttpService.prototype.get = function (url, headers) {
        var _this = this;
        return new rxjs_1.Observable(function (subscriber) {
            return new AxiosSubscriber(subscriber, {
                method: 'get',
                url: url,
                headers: __assign(__assign({}, _this.defaults.headers), headers),
            }, _this);
        });
    };
    HttpService.prototype.post = function (url, body, headers) {
        var _this = this;
        if (body === void 0) { body = {}; }
        return new rxjs_1.Observable(function (subscriber) {
            return new AxiosSubscriber(subscriber, {
                method: 'post',
                data: body,
                url: url,
                headers: __assign(__assign({}, _this.defaults.headers), headers),
            }, _this);
        });
    };
    HttpService.prototype.delete = function (url, body, headers) {
        var _this = this;
        if (body === void 0) { body = {}; }
        return new rxjs_1.Observable(function (subscriber) {
            return new AxiosSubscriber(subscriber, {
                method: 'delete',
                data: body,
                url: url,
                headers: __assign(__assign({}, _this.defaults.headers), headers),
            }, _this);
        });
    };
    return HttpService;
}());
exports.default = new HttpService();
//# sourceMappingURL=http.service.js.map