// TomSelect global import
import TomSelect from 'tom-select';
import 'tom-select/dist/css/tom-select.bootstrap5.css';
import '../css/tomselect-override.css';
window.TomSelect = TomSelect;
import './bootstrap';
import jQuery from 'jquery';
import 'datatables.net-bs5';
import 'datatables.net-bs5/css/dataTables.bootstrap5.min.css';
import Chart from 'chart.js/auto';
import Swal from 'sweetalert2/dist/sweetalert2.js'
import 'sweetalert2/src/sweetalert2.scss'
window.Swal = Swal;

window.$ = jQuery;
window.jQuery = jQuery;
import './paysFunction';

import './userTableAjax';
import './peopleTableAjax';
import './payTableAjax';
import './assistanceTableAjax';
import './assistanceDailyTableAjax';
import './membershipTableAjax';
import './entryTimeTableAjax';
import './producTableAjax';
import './supplierTableAjax';
import './billTableAjax';
import './rateTableAjax';
import './salesTableAjax';
import './saleEditAjax';
import './chartBasic';
import './chartBasicUser';
import './mountFormat';
import './modalUserAjax';
import './expenseTableAjax';
import './pos';
import './billSupplierModal';
import './billProductModal';
import './billCreatePOS';
import './payBillCreate';

function initUserDebtTomSelect() {
	const userDebtFilter = document.querySelector('#userDebtFilter');
	if (!userDebtFilter || !window.TomSelect) {
		return;
	}

	if (userDebtFilter.tomselect) {
		return;
	}

	new window.TomSelect(userDebtFilter, {
		allowEmptyOption: true,
		create: false,
		sortField: {
			field: 'text',
			direction: 'asc',
		},
	});
}

function initBankTomSelect() {
	const bankField = document.querySelector('#bankField');
	if (!bankField || !window.TomSelect) {
		return;
	}

	if (bankField.tomselect) {
		return;
	}

	new window.TomSelect(bankField, {
		allowEmptyOption: true,
		create: false,
		sortField: {
			field: 'text',
			direction: 'asc',
		},
	});
}

if (document.readyState === 'loading') {
	document.addEventListener('DOMContentLoaded', initUserDebtTomSelect, { once: true });
	document.addEventListener('DOMContentLoaded', initBankTomSelect, { once: true });
} else {
	initUserDebtTomSelect();
	initBankTomSelect();
}

window.addEventListener('pageshow', initUserDebtTomSelect);
window.addEventListener('pageshow', initBankTomSelect);


const toggler = document.querySelector('.btn');

if (toggler) {
	toggler.addEventListener('click', function () {
		const sidebar = document.querySelector('#sidebar');

		if (sidebar) {
			sidebar.classList.toggle('collapsed');
		}
	});
}

