"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.App = void 0;
const plugins_1 = require("../plugins");
const utils_1 = require("../utils");
const crud_1 = require("./crud");
const ajax_1 = require("./ajax");
class App {
    static boot(config) {
        this.config = config;
        if (typeof this.config?.element === 'string' && this.config?.element !== '')
            if (utils_1.Guard.hasElement(this.config?.element)) {
                // Plugin dataTable
                if (utils_1.Guard.isPlugin(this.config?.plugin) && this.config?.plugin?.dataTable) {
                    (0, plugins_1.loadDataTable)(this.config?.element, this.config);
                }
                // ajax request
                if (utils_1.Guard.isPlugin(this.config?.plugin) && this.config?.validation) {
                    ajax_1.Ajax.post(this.config);
                }
                if (!utils_1.Guard.isPlugin(this.config?.plugin)) {
                    ajax_1.Ajax.make(this.config);
                }
            }
    }
    static bootPlugin() {
        if (this.config?.plugin?.select) {
            (0, plugins_1.tomSelect)(this.config?.plugin?.select);
        }
        if (this.config?.plugin?.jodit) {
            (0, plugins_1.jodit)(this.config?.plugin?.jodit);
        }
        if (this.config?.plugin?.datepicker) {
            (0, plugins_1.datepicker)(this.config?.plugin?.datepicker);
        }
        if (this.config?.plugin?.timepicker) {
            (0, plugins_1.timepicker)(this.config?.plugin?.timepicker);
        }
        if (this.config?.plugin?.datetimepicker) {
            (0, plugins_1.datetimepicker)(this.config?.plugin?.datetimepicker);
        }
        if (this.config?.plugin?.swiper) {
            (0, plugins_1.swiper)(this.config?.plugin?.swiper);
        }
    }
    static bootTable() {
        const el = this.config?.element;
        if (typeof el !== 'string' || !el)
            return;
        window[el] = (_table, api, op) => {
            // PDF
            if (this.pdfConfig?.btn) {
                crud_1.Crud.downloadPdf({ ...op, ...this.pdfConfig });
            }
            // Excel
            if (this.excelConfig?.btn) {
                crud_1.Crud.downloadExcel({ ...op, ...this.excelConfig });
            }
            // BULK Update
            if (this.updateConfig?.element) {
                crud_1.Crud.bulkUpdate({ ...this.updateConfig, api });
            }
            // BULK Delete
            if (this.deleteConfig?.element) {
                crud_1.Crud.bulkDelete({ ...this.deleteConfig, api });
            }
        };
    }
    static table(config) {
        this.boot(config);
        this.bootTable();
        return this;
    }
    static request(config) {
        this.config = config;
        return this;
    }
    static submit(config) {
        this.config = config;
        ajax_1.Ajax.post(this.config);
        return this;
    }
    static create(config) {
        this.boot(config);
        this.bootPlugin();
        return this;
    }
    static update(config) {
        this.config = config;
        return this;
    }
    static destroy(config) {
        this.config = config;
        return this;
    }
    static bulkUpdate(config) {
        this.updateConfig = config;
        return this;
    }
    static bulkDelete(config) {
        this.deleteConfig = config;
        return this;
    }
    static downloadPdf(config) {
        this.pdfConfig = config;
        return this;
    }
    static downloadExcel(config) {
        this.excelConfig = config;
        return this;
    }
    static beforeSuccess(callback) {
        this.beforeSuccessHandler = callback;
        return this;
    }
    static success(callback) {
        this.successHandler = callback;
        return this;
    }
    static onSuccess(callback) {
        this.successHandler = callback;
        return this;
    }
    static afterSuccess(callback) {
        this.afterSuccessHandler = callback;
        return this;
    }
    static plugin(config) {
        this.config = config;
        this.bootPlugin();
        return this;
    }
    static legacy(config, callback) {
        const {} = config;
        callback();
        return this;
    }
}
exports.App = App;
App.config = {};
App.deleteConfig = {};
App.updateConfig = {};
App.pdfConfig = {};
App.excelConfig = {};
//# sourceMappingURL=app.js.map