import config from 'config';
import manticore from 'manticoresearch';

const mantiClient = new manticore.ApiClient();

mantiClient.basePath = `http://${config.database.manticore.host}:${config.database.manticore.httpPort}`;

export const searchApi = new manticore.SearchApi(mantiClient);
export const indexApi = new manticore.IndexApi(mantiClient);
export const utilsApi = new manticore.UtilsApi();

// manticoresearch v10's utilsApi.sql() wraps the response in a `SqlResponse` oneOf model instead of
// returning the parsed array/object directly; unwrap it to keep the previous plain data shape.
export async function querySql(query) {
	const response = await utilsApi.sql(query);

	return response.getActualInstance();
}
