"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const Path = tslib_1.__importStar(require("path"));
const url_1 = require("url");
const FSE = tslib_1.__importStar(require("fs-extra"));
const lodash_1 = tslib_1.__importDefault(require("lodash"));
const ssh_config_1 = require("./ssh-config");
class VSCodeStorage {
    constructor() {
        switch (process.platform) {
            case 'win32': {
                let appDataPath = process.env.APPDATA;
                this.path = appDataPath && Path.join(appDataPath, 'Code/storage.json');
                break;
            }
            case 'darwin': {
                let homePath = process.env.HOME;
                this.path =
                    homePath &&
                        Path.join(homePath, 'Library/Application Support/Code/storage.json');
                break;
            }
            case 'linux': {
                let homePath = process.env.HOME;
                this.path =
                    homePath && Path.join(homePath, '.config/Code/storage.json');
                break;
            }
            default:
                break;
        }
        if (!this.path) {
            console.error('Unable to determine VSCode storage.json path.');
        }
    }
    cleanUp(activeWorkspaces) {
        let path = this.path;
        if (!path || !FSE.existsSync(path)) {
            return;
        }
        let hostSet = new Set(activeWorkspaces.map(workspace => ssh_config_1.SSH_CONFIG_HOST(workspace)));
        let storageJSON = FSE.readFileSync(path, 'utf8');
        let storage = JSON.parse(storageJSON);
        let updatedStorage = lodash_1.default.cloneDeep(storage);
        let { openedPathsList: { workspaces3, files2, workspaceLabels, fileLabels }, } = updatedStorage;
        if (workspaces3) {
            for (let [index, entry] of workspaces3.entries()) {
                if (isOutDatedRemoteWorkspacePath(entry)) {
                    workspaces3.splice(index, 1);
                    if (workspaceLabels) {
                        workspaceLabels.splice(index, 1);
                    }
                }
            }
        }
        if (files2) {
            for (let [index, entry] of files2.entries()) {
                if (isOutDatedRemoteWorkspacePath(entry)) {
                    files2.splice(index, 1);
                    if (fileLabels) {
                        fileLabels.splice(index, 1);
                    }
                }
            }
        }
        if (!lodash_1.default.isEqual(updatedStorage, storage)) {
            FSE.writeFileSync(path, JSON.stringify(updatedStorage, undefined, 4));
            console.info('Cleaned up VSCode recently opened.');
        }
        function isOutDatedRemoteWorkspacePath(path) {
            if (typeof path !== 'string') {
                return false;
            }
            let url = new url_1.URL(path);
            if (url.protocol !== 'vscode-remote:') {
                return false;
            }
            let [type, host] = decodeURIComponent(url.hostname).split('+');
            if (type !== 'ssh-remote') {
                return false;
            }
            if (!host.startsWith('remote-workspace-')) {
                return false;
            }
            return !hostSet.has(host);
        }
    }
}
exports.VSCodeStorage = VSCodeStorage;
//# sourceMappingURL=vscode-storage.js.map