"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleExecuteCommand = void 0;
const constants_1 = require("./constants");
const protocol_translation_1 = require("./protocol-translation");
function handleExecuteCommand(documents, lspServer, params) {
    if (params.command === constants_1.EXTRACT_SQL_STRUCTURE_COMMAND) {
        const uri = params.arguments?.[0];
        const options = params.arguments?.[1];
        if (!uri) {
            console.error('Language Server: Invalid document URI provided for Extract SQL Structure command.');
            return;
        }
        const document = documents.get(uri);
        if (!document) {
            console.error('Language Server: TextDocument not found.');
            return;
        }
        if (options !== undefined && typeof options === 'object') {
            return lspServer.parseStructure(document, options);
        }
        return lspServer.parseStructure(document);
    }
    else if (params.command === constants_1.REGISTER_SCHEMAS_COMMAND) {
        const registerMode = params.arguments?.[0];
        if (registerMode !== 'set' && registerMode !== 'patch') {
            console.error("Language Server: 1st argument must be either 'patch' or 'set' to indicate register mode.");
            return;
        }
        const schemas = params.arguments?.[1];
        if (!schemas || typeof schemas !== 'object') {
            console.error('Language Server: Schemas must be provided for Register Schemas command.');
            return;
        }
        lspServer.schemaRegistry.register(registerMode, schemas);
    }
    else if (params.command === constants_1.EXTRACT_SCHEMA_CONTEXTS_COMMAND) {
        const uri = params.arguments?.[0];
        if (!uri) {
            console.error('Language Server: Invalid document URI provided for Extract Schema Contexts command.');
            return;
        }
        const document = documents.get(uri);
        if (!document) {
            console.error('Language Server: TextDocument not found.');
            return;
        }
        const range = params.arguments?.[1] || undefined;
        if (range) {
            try {
                if (!(0, protocol_translation_1.instanceOfRange)(range)) {
                    console.error('Language Server: 2nd argument must be Range type.');
                    return;
                }
            }
            catch {
                console.error('Language Server: 2nd argument must be Range type.');
                return;
            }
        }
        return lspServer.extractSchemaContexts(document, range);
    }
    else if (params.command === constants_1.PREVIEW_SCRIPT_COMMAND) {
        const uri = params.arguments?.[0];
        if (!uri) {
            console.error('Language Server: Invalid document URI provided for Preview Script command.');
            return;
        }
        const document = documents.get(uri);
        if (!document) {
            console.error('Language Server: TextDocument not found.');
            return;
        }
        const context = params.arguments?.[1] || { sources: [], sinks: [] };
        if (!context.sinks || !context.sources) {
            console.error('Language Server: Preview contexts not found.');
            return;
        }
        const range = params.arguments?.[2] || undefined;
        if (range) {
            try {
                if (!(0, protocol_translation_1.instanceOfRange)(range)) {
                    console.error('Language Server: 3rd argument must be Range type.');
                    return;
                }
            }
            catch {
                console.error('Language Server: 3rd argument must be Range type.');
                return;
            }
        }
        return lspServer.generatePreviewScript(document, context, range);
    }
    else if (params.command === constants_1.COLLECT_TOKENS_COMMAND) {
        const uri = params.arguments?.[0];
        if (!uri) {
            console.error('Language Server: Invalid document URI provided for Collect Tokens command.');
            return;
        }
        const document = documents.get(uri);
        if (!document) {
            console.error('Language Server: TextDocument not found.');
            return;
        }
        const range = params.arguments?.[1] || undefined;
        if (range) {
            try {
                if (!(0, protocol_translation_1.instanceOfRange)(range)) {
                    console.error('Language Server: 2nd argument must be Range type.');
                    return;
                }
            }
            catch {
                console.error('Language Server: 2nd argument must be Range type.');
                return;
            }
        }
        return lspServer.collectTokens(document, range);
    }
    else if (params.command === constants_1.COLUMN_LEVEL_LINEAGE_COMMAND) {
        const uri = params.arguments?.[0];
        if (!uri) {
            console.error('Language Server: Invalid document URI provided for Column-level lineage command.');
            return;
        }
        const document = documents.get(uri);
        if (!document) {
            console.error('Language Server: TextDocument not found.');
            return;
        }
        const options = params.arguments?.[1] || { mergeLeaves: true };
        return lspServer.getLineage(document, options);
    }
}
exports.handleExecuteCommand = handleExecuteCommand;
