"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiClient = void 0;
const vscode = __importStar(require("vscode"));
const http = __importStar(require("http"));
const https = __importStar(require("https"));
class ApiClient {
    constructor(baseUrl = 'http://localhost', port = 8080) {
        this.baseUrl = baseUrl;
        this.port = port;
    }
    getFullUrl(endpoint) {
        return `${this.baseUrl}:${this.port}${endpoint}`;
    }
    // 通用的 HTTP 请求方法
    async request(url, method = 'GET', body, headers = {}) {
        return new Promise((resolve, reject) => {
            const urlObj = new URL(url);
            const isHttps = urlObj.protocol === 'https:';
            const client = isHttps ? https : http;
            const options = {
                hostname: urlObj.hostname,
                port: urlObj.port || (isHttps ? 443 : 80),
                path: urlObj.pathname + urlObj.search,
                method: method,
                headers: {
                    'User-Agent': 'VSCode-Squirrel-NUT-Explorer/1.0',
                    ...headers
                }
            };
            console.log(`HTTP请求: ${method} ${url}`);
            console.log(`请求选项:`, options);
            const req = client.request(options, (res) => {
                let data = '';
                console.log(`响应状态码: ${res.statusCode}`);
                console.log(`响应头:`, res.headers);
                res.on('data', (chunk) => {
                    data += chunk;
                });
                res.on('end', () => {
                    console.log(`原始响应数据长度: ${data.length}`);
                    console.log(`原始响应数据: ${data}`);
                    if (res.statusCode !== 200) {
                        reject(new Error(`HTTP ${res.statusCode}: ${res.statusMessage}`));
                        return;
                    }
                    try {
                        if (!data || data.trim() === '') {
                            reject(new Error('响应为空'));
                            return;
                        }
                        const result = JSON.parse(data);
                        resolve(result);
                    }
                    catch (error) {
                        reject(new Error(`解析JSON响应失败: ${error}。原始数据: ${data}`));
                    }
                });
            });
            req.on('error', (error) => {
                console.error(`请求错误: ${error.message}`);
                reject(new Error(`请求失败: ${error.message}`));
            });
            req.setTimeout(10000, () => {
                console.error('请求超时');
                req.destroy();
                reject(new Error('请求超时'));
            });
            if (body) {
                console.log(`请求体长度: ${body.length}`);
                req.write(body);
            }
            req.end();
        });
    }
    // 获取 nut 文件列表
    async getFileList(dirName = 'sqr', fileType = 'nut') {
        try {
            const url = this.getFullUrl('/Api/PvfUtiltiy/GetFileList');
            const params = new URLSearchParams({
                dirName,
                returnType: '1',
                fileType
            });
            const fullUrl = `${url}?${params}`;
            console.log(`请求URL: ${fullUrl}`);
            const result = await this.request(fullUrl);
            if (result.IsError) {
                throw new Error(result.Msg || '获取文件列表失败');
            }
            return result.Data;
        }
        catch (error) {
            vscode.window.showErrorMessage(`获取文件列表失败: ${error}`);
            throw error;
        }
    }
    // 批量获取文件内容
    async getFileContents(filePaths, encodingType = null) {
        try {
            const url = this.getFullUrl('/Api/PvfUtiltiy/GetFileContents');
            const requestBody = {
                FileList: filePaths,
                UseCompatibleDecompiler: false,
                EncodingType: encodingType
            };
            const result = await this.request(url, 'POST', JSON.stringify(requestBody), {
                'Content-Type': 'application/json'
            });
            if (result.IsError) {
                throw new Error(result.Msg || '获取文件内容失败');
            }
            return result.Data.FileContentData;
        }
        catch (error) {
            vscode.window.showErrorMessage(`获取文件内容失败: ${error}`);
            throw error;
        }
    }
    // 上传文件内容
    async uploadFileContent(filePath, content) {
        try {
            const url = this.getFullUrl(`/Api/PvfUtiltiy/ImportFile?filePath=${encodeURIComponent(filePath)}`);
            console.log(`上传文件URL: ${url}`);
            const result = await this.request(url, 'POST', content, {
                'Content-Type': 'text/plain; charset=utf-8',
            });
            if (result.IsError) {
                throw new Error(result.Msg || '上传文件失败');
            }
            return true;
        }
        catch (error) {
            vscode.window.showErrorMessage(`上传文件失败: ${error}`);
            throw error;
        }
    }
    // 获取 pvfUtility 版本号
    async getVersion() {
        try {
            const url = this.getFullUrl('/Api/PvfUtiltiy/getVersion');
            console.log(`版本检查URL: ${url}`);
            const result = await this.request(url);
            if (result.IsError) {
                throw new Error(result.Msg || '获取版本号失败');
            }
            return result.Data;
        }
        catch (error) {
            console.error('获取版本号失败:', error);
            vscode.window.showErrorMessage(`获取 pvfUtility 版本号失败: ${error}`);
            throw error;
        }
    }
    // 更新配置
    updateConfig(baseUrl, port) {
        this.baseUrl = baseUrl;
        this.port = port;
    }
}
exports.ApiClient = ApiClient;
//# sourceMappingURL=apiClient.js.map