"use strict";
// *****************************************************************************
// Copyright (C) 2020 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
// based on https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/workbench/api/browser/mainThreadFileSystemEventService.ts
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainFileSystemEventService = void 0;
const plugin_api_rpc_1 = require("../../common/plugin-api-rpc");
const core_1 = require("@theia/core");
const disposable_1 = require("@theia/core/lib/common/disposable");
const file_service_1 = require("@theia/filesystem/lib/browser/file-service");
const filesystem_preferences_1 = require("@theia/filesystem/lib/common/filesystem-preferences");
class MainFileSystemEventService {
    constructor(rpc, container, fileService = container.get(file_service_1.FileService), preferences = container.get(filesystem_preferences_1.FileSystemPreferences)) {
        this.fileService = fileService;
        this.preferences = preferences;
        this.toDispose = new disposable_1.DisposableCollection();
        this.watches = new Map();
        const proxy = rpc.getProxy(plugin_api_rpc_1.MAIN_RPC_CONTEXT.ExtHostFileSystemEventService);
        this.toDispose.push(fileService.onDidFilesChange(event => {
            // file system events - (changes the editor and others make)
            const events = {
                created: [],
                changed: [],
                deleted: []
            };
            for (const change of event.changes) {
                switch (change.type) {
                    case 1 /* FileChangeType.ADDED */:
                        events.created.push(change.resource['codeUri']);
                        break;
                    case 0 /* FileChangeType.UPDATED */:
                        events.changed.push(change.resource['codeUri']);
                        break;
                    case 2 /* FileChangeType.DELETED */:
                        events.deleted.push(change.resource['codeUri']);
                        break;
                }
            }
            proxy.$onFileEvent(events);
        }));
        // BEFORE file operation
        fileService.addFileOperationParticipant({
            participate: (target, source, operation, timeout, token) => proxy.$onWillRunFileOperation(operation, target['codeUri'], source?.['codeUri'], timeout, token)
        });
        // AFTER file operation
        this.toDispose.push(fileService.onDidRunUserOperation(e => proxy.$onDidRunFileOperation(e.operation, e.target['codeUri'], e.source?.['codeUri'])));
    }
    dispose() {
        this.toDispose.dispose();
    }
    $watch(session, resource, options) {
        if (this.watches.has(session)) {
            throw new Error(`There is already a watch request for the key ${session}`);
        }
        const uri = core_1.URI.fromComponents(resource);
        // Plugin-created watchers (`vscode.workspace.createFileSystemWatcher`) arrive here with an
        // empty `excludes` list. Language servers frequently request recursive watches rooted at
        // absolute paths outside the workspace (e.g. JDT-LS's per-project globs), so apply the
        // user's `files.watcherExclude` here to keep the number of OS file watches bounded.
        const watch = this.fileService.watch(uri, { ...options, excludes: this.getExcludes(uri, options.excludes) });
        this.toDispose.push(watch);
        this.watches.set(session, watch);
    }
    getExcludes(uri, requested = []) {
        const configured = this.preferences.get('files.watcherExclude', undefined, uri.toString());
        const excludes = new Set(requested);
        if (configured) {
            for (const pattern of Object.keys(configured)) {
                if (configured[pattern]) {
                    excludes.add(pattern);
                }
            }
        }
        return Array.from(excludes);
    }
    $unwatch(session) {
        const watch = this.watches.get(session);
        if (watch) {
            watch.dispose();
            this.watches.delete(session);
        }
    }
}
exports.MainFileSystemEventService = MainFileSystemEventService;
//# sourceMappingURL=main-file-system-event-service.js.map