import { Config } from '../app';
import { Errors } from '../services/errors';
import { Success } from '../services/success';
import { Loader, Sweet } from '../utils';
export class Http {
}
Http.ajax = {
    make(op, callback) {
        const { confirm, method, payload } = op;
        if (confirm && method === 'PUT') {
            Sweet.confirm().then(() => {
                this.send(op, callback);
            });
            return;
        }
        if (confirm && method === 'DELETE' && payload && typeof payload === 'object' && 'ids' in payload) {
            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.handle(op, res);
            }
            else {
                Errors.handle(op, res);
            }
        });
    },
    core(op, callBack) {
        const { url = '/', payload = {}, method = 'POST', dataType = 'form', loader = true, token = null } = op;
        const app_url = Config.app_url + url;
        const csrf_token = Config.csrf_token;
        const app_env = 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) {
            Loader.show();
        }
        $.ajax(config)
            .done((res) => {
            callBack(op, res);
            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) {
                Sweet.error({ title: error + '\n' + 'Status Code: ' + xhr.status.toString(), html: xhr.responseText, width: '800px', animation: true });
            }
            Loader.hide();
        });
    },
};
Http.axios = {
    core() { },
    make() { },
    send() { },
};
//# sourceMappingURL=http.js.map