import { performance } from "perf_hooks";
import { log } from "./../utils/index.js";
import { wpApiClient } from "./../api/client.js";
const isTestEnvironment = process.env.NODE_ENV === "test" || process.env.VITEST;
function safeExit(code) {
    if (!isTestEnvironment) {
        process.exit(code);
    }
    else {
        log("error", `[Test mode] Would have called process.exit(${code})`);
    }
}
async function testConnection() {
    try {
        const isConnected = await wpApiClient.testConnection();
        if (isConnected) {
            log("info", "WordPress API connection successful");
            return true;
        }
        else {
            log("error", "WordPress API connection failed");
            return false;
        }
    }
    catch (error) {
        log("error", "Error testing WordPress API connection:", error);
        return false;
    }
}
async function executeReadOnlyQuery(sql) {
    try {
        const startTime = performance.now();
        const result = await wpApiClient.executeQuery(sql);
        const endTime = performance.now();
        const duration = endTime - startTime;
        return {
            content: [
                {
                    type: "text",
                    text: JSON.stringify(result.data, null, 2),
                },
                {
                    type: "text",
                    text: `Query execution time: ${duration.toFixed(2)} ms\nRows returned: ${result.count}`,
                },
            ],
            isError: false,
        };
    }
    catch (error) {
        log("error", "Error executing query via WordPress API:", error);
        const errorMessage = error instanceof Error ? error.message : String(error);
        return {
            content: [
                {
                    type: "text",
                    text: `Error executing query: ${errorMessage}`,
                },
            ],
            isError: true,
        };
    }
}
async function executeQuery(sql, params = []) {
    if (params.length > 0) {
        log("info", "Query parameters are not supported by WordPress API. Parameters will be ignored.");
    }
    try {
        const result = await wpApiClient.executeQuery(sql);
        return result.data;
    }
    catch (error) {
        log("error", "Error executing query:", error);
        throw error;
    }
}
async function executeWriteQuery(sql) {
    log("error", "Write operations are not supported by WordPress API");
    return {
        content: [
            {
                type: "text",
                text: "Error: Write operations (INSERT, UPDATE, DELETE, DDL) are not supported by the WordPress REST API. The API is read-only.",
            },
        ],
        isError: true,
    };
}
async function getSchema(tableName) {
    try {
        const schema = await wpApiClient.getSchema(tableName);
        return schema;
    }
    catch (error) {
        log("error", "Error getting schema:", error);
        throw error;
    }
}
export { isTestEnvironment, safeExit, executeQuery, executeWriteQuery, executeReadOnlyQuery, testConnection, getSchema, };
