const SQL_WRITE_KEYWORDS = [
    "INSERT",
    "UPDATE",
    "DELETE",
    "CREATE",
    "DROP",
    "TRUNCATE",
    "ALTER",
    "REPLACE",
    "MERGE",
];
const HTTP_MUTATION_METHODS = new Set(["DELETE", "PUT", "PATCH", "POST"]);
// `SELECT ... INTO new_table` creates a table — a write — so SELECT alone
// isn't a safe early-return signal.
const SELECT_INTO_RE = /\bSELECT\b[\s\S]*?\bINTO\b/i;
function isSqlWrite(query) {
    // Strip surrounding backticks — v2 YAML bodies may be template-literal
    // wrapped (e.g. `` `INSERT INTO ...` ``).
    const trimmed = query.trim().replace(/^`|`$/g, "").trim();
    if (/^\s*SELECT\b/i.test(trimmed) &&
        !trimmed.includes(";") &&
        !SELECT_INTO_RE.test(trimmed)) {
        return false;
    }
    if (SELECT_INTO_RE.test(trimmed))
        return true;
    return SQL_WRITE_KEYWORDS.some((kw) => new RegExp(`(^|\\s|;)${kw}\\s`, "i").test(trimmed));
}
function isDynamic(value) {
    return value.includes("{{") || value.includes("${");
}
export function classifyYamlStepMutation(step) {
    const pluginConfig = Object.entries(step).find(([key, val]) => key !== "integration" &&
        key !== "name" &&
        val !== null &&
        typeof val === "object" &&
        !Array.isArray(val))?.[1];
    if (!pluginConfig)
        return "unknown";
    const httpMethod = typeof pluginConfig.httpMethod === "string"
        ? pluginConfig.httpMethod
        : typeof pluginConfig.method === "string"
            ? pluginConfig.method
            : null;
    if (httpMethod !== null) {
        return HTTP_MUTATION_METHODS.has(httpMethod.toUpperCase())
            ? "write"
            : "read";
    }
    const sqlBody = typeof pluginConfig.body === "string"
        ? pluginConfig.body
        : typeof pluginConfig.sqlBody === "string"
            ? pluginConfig.sqlBody
            : typeof pluginConfig.query === "string"
                ? pluginConfig.query
                : null;
    if (sqlBody !== null) {
        if (isDynamic(sqlBody))
            return "unknown";
        return isSqlWrite(sqlBody) ? "write" : "read";
    }
    return "unknown";
}
export function classifyIntegrationMutations(steps) {
    let sawUnknown = false;
    for (const step of steps) {
        const result = classifyYamlStepMutation(step);
        if (result === "write")
            return "write";
        if (result === "unknown")
            sawUnknown = true;
    }
    if (steps.length === 0)
        return "unknown";
    return sawUnknown ? "unknown" : "read";
}
//# sourceMappingURL=migration-classifier.js.map