var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
define(["require", "exports", "jquery", "../../Form", "./Datatables.declare"], function (require, exports, jquery_1, Form_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.initDatatables = void 0;
    jquery_1 = __importDefault(jquery_1);
    function IsJsonString(str) {
        try {
            if (typeof str === 'string') {
                JSON.parse(str);
            }
            else {
                return false;
            }
        }
        catch (e) {
            return false;
        }
        return true;
    }
    function initDatatables(table) {
        const dataTable = table ? (0, jquery_1.default)(table) : (0, jquery_1.default)('#dataTable');
        if (dataTable.length > 0) {
            const tableUrl = dataTable.attr('data-url');
            const tablaOrderStr = dataTable.attr('data-order') || '';
            const createUrl = dataTable.attr('data-create');
            const hasLeftAction = dataTable.attr('data-left-action');
            const hasRightAction = dataTable.attr('data-right-action');
            const placeHolder = dataTable.attr('data-placeholder');
            const filterInputWidth = dataTable.attr('data-fwidth');
            const afterSearch = dataTable.attr('data-after-search');
            const columnsDefs = [];
            let tableOrder;
            if (IsJsonString(tablaOrderStr)) {
                tableOrder = JSON.parse(tablaOrderStr);
            }
            columnsDefs.push({
                visible: false,
                targets: 0
            });
            if (hasLeftAction) {
                columnsDefs.push({
                    orderable: false,
                    targets: 1
                });
            }
            if (hasRightAction) {
                columnsDefs.push({
                    orderable: false,
                    className: 'text-right',
                    targets: -1
                });
            }
            let props = {
                order: tableOrder,
                columnDefs: columnsDefs,
                stateSave: true,
                language: {
                    searchPlaceholder: placeHolder ? placeHolder : 'Пошук...'
                }
            };
            if (tableUrl != null) {
                props = Object.assign(Object.assign({}, props), { 
                    // @ts-ignore
                    processing: true, serverSide: true, ajax: {
                        url: tableUrl,
                        data: (d) => `json=${JSON.stringify(d)}&cache=${Math.random()}`,
                        cache: true
                    } });
            }
            // @ts-ignore
            dataTable.DataTable(props);
            dataTable.on('init.dt', () => {
                if (createUrl) {
                    (0, jquery_1.default)('#dataTable_filter').before('<a href="' + createUrl + '" class="btn btn-primary float-left mb-3 DTTT_container" title=""><span class="icon-plus22 position-left"></span> Додати</a>');
                }
                if (afterSearch) {
                    (0, jquery_1.default)('#dataTable_filter').after(afterSearch);
                }
                if (filterInputWidth) {
                    (0, jquery_1.default)('#dataTable_filter input').css('width', filterInputWidth);
                }
                (0, Form_1.Select2)('.dataTables_length select', {
                    minimumResultsForSearch: Infinity,
                    dropdownAutoWidth: true,
                    width: 'auto'
                });
            });
        }
    }
    exports.initDatatables = initDatatables;
    document.addEventListener('DOMContentLoaded', () => {
        initDatatables();
    });
});
