"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.SquirrelFileSystemProvider = void 0;
const vscode = __importStar(require("vscode"));
class SquirrelFileSystemProvider {
    constructor(model) {
        this._onDidChangeFile = new vscode.EventEmitter();
        this.onDidChangeFile = this._onDidChangeFile.event;
        // 存储文件实例内容，支持多标签页
        this.fileContentCache = new Map();
        this.model = model;
    }
    // 读取文件
    async readFile(uri) {
        const filePath = this.getPathFromUri(uri);
        // 检查是否有缓存的内容（用于多标签页支持）
        const cacheKey = uri.toString();
        if (this.fileContentCache.has(cacheKey)) {
            const content = this.fileContentCache.get(cacheKey);
            return new TextEncoder().encode(content);
        }
        // 从模型获取内容
        const content = await this.model.getFileContent(filePath);
        if (content === undefined) {
            throw vscode.FileSystemError.FileNotFound(uri);
        }
        // 缓存内容以支持多标签页
        this.fileContentCache.set(cacheKey, content);
        return new TextEncoder().encode(content);
    }
    // 写入文件
    async writeFile(uri, content, options) {
        const filePath = this.getPathFromUri(uri);
        const textContent = new TextDecoder().decode(content);
        // 更新缓存内容
        const cacheKey = uri.toString();
        this.fileContentCache.set(cacheKey, textContent);
        const success = await this.model.saveFileContent(filePath, textContent);
        if (!success) {
            throw vscode.FileSystemError.Unavailable('保存文件失败');
        }
    }
    // 其他必须实现的方法（简化实现）
    stat(uri) {
        // 获取文件内容以确定大小
        const cacheKey = uri.toString();
        let content = '';
        if (this.fileContentCache.has(cacheKey)) {
            content = this.fileContentCache.get(cacheKey);
        }
        return {
            type: vscode.FileType.File,
            ctime: Date.now(),
            mtime: Date.now(),
            size: Buffer.byteLength(content, 'utf8')
        };
    }
    readDirectory(uri) {
        return [];
    }
    createDirectory(uri) {
        throw vscode.FileSystemError.NoPermissions('不支持创建目录');
    }
    delete(uri) {
        throw vscode.FileSystemError.NoPermissions('不支持删除文件');
    }
    rename(oldUri, newUri, options) {
        throw vscode.FileSystemError.NoPermissions('不支持重命名文件');
    }
    watch(uri, options) {
        return new vscode.Disposable(() => { });
    }
    // 清理文件缓存
    clearFileCache(uri) {
        const cacheKey = uri.toString();
        this.fileContentCache.delete(cacheKey);
    }
    // 从 URI 解析文件路径
    getPathFromUri(uri) {
        if (uri.scheme !== 'squirrel') {
            throw vscode.FileSystemError.FileNotFound(uri);
        }
        // 移除查询参数，只返回路径部分
        return uri.path.substring(1); // 去掉开头的 '/'
    }
    // 获取文件实例ID
    getInstanceId(uri) {
        return uri.query ? new URLSearchParams(uri.query).get('instance') || 'default' : 'default';
    }
}
exports.SquirrelFileSystemProvider = SquirrelFileSystemProvider;
//# sourceMappingURL=fileSystemProvider.js.map