export function getToken() {
    const scriptUrl = new URL(import.meta.url);
    const params = new URLSearchParams(scriptUrl.search);

    const paramsObject = Object.fromEntries(params.entries());
    return paramsObject.token;
}

export function capitalizeFirstLetter(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}


export const initAutonumeric = () => {
    $(document).on('draw.dt', (event) => {
        $("span.autonumeric").each(function () {
            let autoNumericInstance = AutoNumeric.getAutoNumericElement(this)
            if (!autoNumericInstance) {
                new AutoNumeric(this, {
                    digitGroupSeparator: ".",
                    decimalCharacter: ",",
                    allowDecimalPadding: 'floats',
                    decimalPlaces: 2,
                    unformatOnSubmit: true,

                });
            }
        })
        $("span.autonumeric-decimal").each(function () {
            let autoNumericInstance = AutoNumeric.getAutoNumericElement(this)
            if (!autoNumericInstance) {
                new AutoNumeric(this, {
                    digitGroupSeparator: ".",
                    decimalCharacter: ",",
                    allowDecimalPadding: 'floats',
                    decimalPlaces: $(this).data('decimal-places'),
                    unformatOnSubmit: true,

                });
            }
        })
        // $(':input[type="number"]').each(function(){
        //     $(this).attr('type','text')
        //     let autoNumericInstance = AutoNumeric.getAutoNumericElement(this)
        //     if (!autoNumericInstance) {
        //         new AutoNumeric(this, {
        //             digitGroupSeparator: ".",
        //             decimalCharacter: ",",
        //             allowDecimalPadding : 'floats',
        //             decimalPlaces: 2,
        //             unformatOnSubmit: true,

        //         });
        //     }
        // })
    })
    // editor.on('open', (event) => {
    //     $("input.autonumeric").each(function () {
    //         let autoNumericInstance = AutoNumeric.getAutoNumericElement(this)
    //         if (autoNumericInstance) {
    //             autoNumericInstance.remove()

    //         }
    //         new AutoNumeric(this, {
    //             decimalCharacter: ",",
    //             digitGroupSeparator: ".",
    //             allowDecimalPadding : 'floats',
    //             decimalPlaces: 2,
    //             unformatOnSubmit: true,
    //         });
    //     })
    // })
    //     editor.on('preSubmit', (e, d, type) => {
    //         if (d.data) {
    //             Object.keys(d.data).forEach(function (key) {
    //                 // Find all elements with class .autonumeric
    //                 $("input.autonumeric").each(function () {
    //                     let autoNumericInstance = AutoNumeric.getAutoNumericElement(this)
    //                     if (autoNumericInstance) {
    //                         // Get raw value and update the corresponding field
    //                         let rawValue = autoNumericInstance.getNumericString();
    //                         d.data[key][this.name] = rawValue; // Ensure the correct name attribute is used
    //                     }
    //                 });
    //             });
    //         }
    //     })
}

export const getTablerIconClassName = () => {
    const classNames = [];
    for (const sheet of document.styleSheets) {
        try {
            if (sheet.cssRules) {
                for (const rule of sheet.cssRules) {
                    if (rule.selectorText && rule.selectorText.startsWith(".ti-")) {
                        let className = rule.selectorText.split(" ")[0].replace(".", "");
                        className = className.split("::")[0]
                        className = className.split(":")[0]
                        classNames.push(className);
                    }
                }
            }
        } catch (e) {
            console.warn("Cannot access stylesheet:", sheet.href);
        }
    }
    return classNames.reverse();

}