"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Http = void 0;
const app_1 = require("../app");
const errors_1 = require("../services/errors");
const success_1 = require("../services/success");
const utils_1 = require("../utils");
class Http {
}
exports.Http = Http;
Http.ajax = {
    make(op, callback) {
        const { confirm, method, payload } = op;
        if (confirm && method === 'PUT') {
            utils_1.Sweet.confirm().then(() => {
                this.send(op, callback);
            });
            return;
        }
        if (confirm && method === 'DELETE' && payload && typeof payload === 'object' && 'ids' in payload) {
            utils_1.Sweet.deleteConfirm({ item: payload.ids }).then(() => {
                this.send(op, callback);
            });
            return;
        }
    },
    send(op, callBack) {
        this.core(op, (op, res) => {
            if (typeof callBack === 'function') {
                callBack(op, res);
                return;
            }
            if (res.success) {
                success_1.Success.handle(op, res);
            }
            else {
                errors_1.Errors.handle(op, res);
            }
        });
    },
    core(op, callBack) {
        const { url = '/', payload = {}, method = 'POST', dataType = 'form', loader = true, token = null } = op;
        const app_url = app_1.Config.app_url + url;
        const csrf_token = app_1.Config.csrf_token;
        const app_env = app_1.Config.app_env;
        const config = {
            method: method,
            url: app_url,
            data: payload,
            contentType: false,
            cache: true,
            processData: false,
        };
        if (dataType === 'json') {
            config.dataType = 'json';
            config.headers = {
                'X-Requested-With': 'XMLHttpRequest',
                Accept: 'application/json',
                Authorization: `Bearer ${csrf_token ?? token}`,
                'X-CSRF-Token': csrf_token ?? token,
            };
            config.data = JSON.stringify(payload);
            config.contentType = 'application/json; charset=utf-8';
        }
        if (loader) {
            utils_1.Loader.show();
        }
        $.ajax(config)
            .done((res) => {
            callBack(op, res);
            utils_1.Loader.hide();
        })
            .fail((xhr, status, error) => {
            if (app_env) {
                console.log("xhr:", xhr);
                console.log("status:", status);
                console.log("error:", error);
            }
            if (xhr.status === 500) {
                utils_1.Sweet.error({ title: error + '\n' + 'Status Code: ' + xhr.status.toString(), html: xhr.responseText, width: '800px', animation: true });
            }
            utils_1.Loader.hide();
        });
    },
};
Http.axios = {
    core() { },
    make() { },
    send() { },
};
//# sourceMappingURL=http.js.map