export function useDatabase() {

    const buildDebug = (query: string, params: any[]) => {
        console.info("-----------------");
        //build final query
        let finalQuery = query;
        params.forEach((param) => {
            let value = param;
            if(typeof param === 'string'){
                value = `'${param}'`;
            }
            finalQuery = finalQuery.replace('?', value);
        });
        console.info("Final Query: ", finalQuery);
        console.info("-----------------"); 
    }

    const runQuery = async (query: string, params: any[] = []) => {
        return await window.electron.ipcRenderer.invoke('db:run', query, params);
    };
    
    const getQuery = async (query: string, params: any[] = []) => {
        return await window.electron.ipcRenderer.invoke('db:get', query, params);
    };
    
    const allQuery = async (query: string, params: any[] = [], debug: boolean = false) => {
        if(debug){
            buildDebug(query, params);
        }
        return await window.electron.ipcRenderer.invoke('db:all', query, params);
    };
    
    return {
        runQuery,
        getQuery,
        allQuery,
    };
}