"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DebuggersContributionPoint = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_core_browser_1 = require("@opensumi/ide-core-browser");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const ide_debug_1 = require("@opensumi/ide-debug");
const debug_configuration_manager_1 = require("@opensumi/ide-debug/lib/browser/debug-configuration-manager");
const debug_schema_manager_1 = require("@opensumi/ide-debug/lib/browser/debug-schema-manager");
const common_1 = require("../../../common");
const types_1 = require("../../types");
const { deepClone } = ide_core_common_1.objects;
const INTERNAL_CONSOLE_OPTIONS_SCHEMA = {
    enum: ['neverOpen', 'openOnSessionStart', 'openOnFirstSessionStart'],
    default: 'openOnFirstSessionStart',
    description: (0, ide_core_common_1.localize)('preference.debug.internalConsoleOptions'),
};
let DebuggersContributionPoint = class DebuggersContributionPoint extends common_1.VSCodeContributePoint {
    contribute() {
        for (const contrib of this.contributesMap) {
            const { extensionId, contributes } = contrib;
            const extension = this.extensionManageService.getExtensionInstanceByExtId(extensionId);
            if (!extension) {
                continue;
            }
            this.debugService.registerDebugContributionPoints(extension.path, this.resolveDebuggers(contributes, extension));
        }
        this.debugSchemaManager.update();
    }
    resolveDebuggers(debuggersContributionPoints, extension) {
        return debuggersContributionPoints.map((debuggersContributionPoint) => {
            const debuggers = this.doResolveDebuggers(debuggersContributionPoint, extension);
            this.debugConfigurationManager.registerDebugger(debuggers);
            return debuggers;
        });
    }
    doResolveDebuggers(debuggersContributionPoint, extension) {
        const result = {
            type: debuggersContributionPoint.type,
            label: debuggersContributionPoint.label
                ? (0, ide_core_browser_1.replaceLocalizePlaceholder)(debuggersContributionPoint.label, extension.id)
                : '',
            languages: debuggersContributionPoint.languages,
            enableBreakpointsFor: debuggersContributionPoint.enableBreakpointsFor,
            variables: debuggersContributionPoint.variables,
            adapterExecutableCommand: debuggersContributionPoint.adapterExecutableCommand,
            configurationSnippets: (debuggersContributionPoint.configurationSnippets || []).map((config) => {
                if (config.label) {
                    config.label = (0, ide_core_browser_1.replaceLocalizePlaceholder)(config.label, extension.id);
                }
                if (config.description) {
                    config.description = (0, ide_core_browser_1.replaceLocalizePlaceholder)(config.description, extension.id);
                }
                if (config.markdownDescription) {
                    config.markdownDescription = (0, ide_core_browser_1.replaceLocalizePlaceholder)(config.markdownDescription, extension.id);
                }
                return config;
            }),
            win: debuggersContributionPoint.win,
            winx86: debuggersContributionPoint.winx86,
            windows: debuggersContributionPoint.windows,
            osx: debuggersContributionPoint.osx,
            linux: debuggersContributionPoint.linux,
            program: debuggersContributionPoint.program,
            args: debuggersContributionPoint.args,
            runtime: debuggersContributionPoint.runtime,
            runtimeArgs: debuggersContributionPoint.runtimeArgs,
        };
        result.configurationAttributes =
            debuggersContributionPoint.configurationAttributes &&
                this.resolveSchemaAttributes(debuggersContributionPoint.type, debuggersContributionPoint.configurationAttributes, extension);
        return result;
    }
    resolveSchemaAttributes(type, configurationAttributes, extension) {
        const taskSchema = {};
        const recursionPropertiesDescription = (prop) => {
            Object.keys(prop).forEach((name) => {
                if (prop[name].properties) {
                    recursionPropertiesDescription(prop[name].properties);
                }
                // 避免某些不规范的 json 配置打挂整个 debug 功能，做个防御
                if (typeof prop[name] !== 'object') {
                    return;
                }
                prop[name].description = (0, ide_core_browser_1.replaceLocalizePlaceholder)(prop[name].description, extension.id);
                prop[name].markdownDescription = (0, ide_core_browser_1.replaceLocalizePlaceholder)(prop[name].markdownDescription, extension.id);
            });
        };
        return Object.keys(configurationAttributes).map((request) => {
            const attributes = deepClone(configurationAttributes[request]);
            const defaultRequired = ['name', 'type', 'request'];
            attributes.required =
                attributes.required && attributes.required.length
                    ? defaultRequired.concat(attributes.required)
                    : defaultRequired;
            attributes.additionalProperties = false;
            attributes.type = 'object';
            if (!attributes.properties) {
                attributes.properties = {};
            }
            const properties = attributes.properties;
            properties.type = {
                enum: [type],
                description: (0, ide_core_common_1.localize)('debug.launch.configurations.debugType'),
                pattern: '^(?!node2)',
                errorMessage: (0, ide_core_common_1.localize)('debug.launch.configurations.debugTypeNotRecognised'),
                patternErrorMessage: (0, ide_core_common_1.localize)('debug.launch.configurations.node2NotSupported'),
            };
            properties.name = {
                type: 'string',
                description: (0, ide_core_common_1.localize)('debug.launch.configurations.debugName'),
                default: 'Launch',
            };
            properties.request = {
                enum: [request],
                description: (0, ide_core_common_1.localize)('debug.launch.configurations.debugRequest'),
            };
            properties.debugServer = {
                type: 'number',
                description: (0, ide_core_common_1.localize)('debug.launch.configurations.debugServer'),
                default: 4711,
            };
            properties.preLaunchTask = {
                anyOf: [
                    taskSchema,
                    {
                        type: ['string', 'null'],
                    },
                ],
                default: '',
                description: (0, ide_core_common_1.localize)('debug.launch.configurations.debugPrelaunchTask'),
            };
            properties.postDebugTask = {
                anyOf: [
                    taskSchema,
                    {
                        type: ['string', 'null'],
                    },
                ],
                default: '',
                description: (0, ide_core_common_1.localize)('debug.launch.configurations.debugPostDebugTask'),
            };
            properties.internalConsoleOptions = INTERNAL_CONSOLE_OPTIONS_SCHEMA;
            const osProperties = Object.assign({}, properties);
            properties.windows = {
                type: 'object',
                description: (0, ide_core_common_1.localize)('debug.launch.configurations.debugWindowsConfiguration'),
                properties: osProperties,
            };
            properties.osx = {
                type: 'object',
                description: (0, ide_core_common_1.localize)('debug.launch.configurations.debugOSXConfiguration'),
                properties: osProperties,
            };
            properties.linux = {
                type: 'object',
                description: (0, ide_core_common_1.localize)('debug.launch.configurations.debugLinuxConfiguration'),
                properties: osProperties,
            };
            Object.keys(attributes.properties).forEach((name) => {
                // 为每一个属性创建独立的错误信息
                attributes.properties[name].pattern =
                    attributes.properties[name].pattern || '^(?!.*\\$\\{(env|config|command)\\.)';
                attributes.properties[name].patternErrorMessage =
                    attributes.properties[name].patternErrorMessage ||
                        (0, ide_core_common_1.localize)('deprecatedVariables', "'env.', 'config.' and 'command.' are deprecated, use 'env:', 'config:' and 'command:' instead.");
            });
            recursionPropertiesDescription(attributes.properties);
            return attributes;
        });
    }
    dispose() {
        for (const contrib of this.contributesMap) {
            const { extensionId } = contrib;
            const extension = this.extensionManageService.getExtensionInstanceByExtId(extensionId);
            if (!extension) {
                continue;
            }
            this.debugService.unregisterDebugContributionPoints(extension.path);
        }
    }
};
exports.DebuggersContributionPoint = DebuggersContributionPoint;
DebuggersContributionPoint.schema = {
    description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers', 'Contributes debug adapters.'),
    type: 'array',
    defaultSnippets: [{ body: [{ type: '' }] }],
    items: {
        additionalProperties: false,
        type: 'object',
        defaultSnippets: [{ body: { type: '', program: '', runtime: '' } }],
        properties: {
            type: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.type', 'Unique identifier for this debug adapter.'),
                type: 'string',
            },
            label: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.label', 'Display name for this debug adapter.'),
                type: 'string',
            },
            program: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.program', 'Path to the debug adapter program. Path is either absolute or relative to the extension folder.'),
                type: 'string',
            },
            args: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.args', 'Optional arguments to pass to the adapter.'),
                type: 'array',
            },
            runtime: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.runtime', 'Optional runtime in case the program attribute is not an executable but requires a runtime.'),
                type: 'string',
            },
            runtimeArgs: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.runtimeArgs', 'Optional runtime arguments.'),
                type: 'array',
            },
            variables: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.variables', 'Mapping from interactive variables (e.g. ${action.pickProcess}) in `launch.json` to a command.'),
                type: 'object',
            },
            initialConfigurations: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.initialConfigurations', "Configurations for generating the initial 'launch.json'."),
                type: ['array', 'string'],
            },
            languages: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.languages', 'List of languages for which the debug extension could be considered the "default debugger".'),
                type: 'array',
            },
            configurationSnippets: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.configurationSnippets', "Snippets for adding new configurations in 'launch.json'."),
                type: 'array',
            },
            configurationAttributes: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.configurationAttributes', "JSON schema configurations for validating 'launch.json'."),
                type: 'object',
            },
            when: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.when', "Condition which must be true to enable this type of debugger. Consider using 'shellExecutionSupported', 'virtualWorkspace', 'resourceScheme' or an extension-defined context key as appropriate for this."),
                type: 'string',
                default: '',
            },
            windows: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.windows', 'Windows specific settings.'),
                type: 'object',
                properties: {
                    runtime: {
                        description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.windows.runtime', 'Runtime used for Windows.'),
                        type: 'string',
                    },
                },
            },
            osx: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.osx', 'macOS specific settings.'),
                type: 'object',
                properties: {
                    runtime: {
                        description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.osx.runtime', 'Runtime used for macOS.'),
                        type: 'string',
                    },
                },
            },
            linux: {
                description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.linux', 'Linux specific settings.'),
                type: 'object',
                properties: {
                    runtime: {
                        description: (0, ide_core_common_1.localize)('vscode.extension.contributes.debuggers.linux.runtime', 'Runtime used for Linux.'),
                        type: 'string',
                    },
                },
            },
        },
    },
};
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_debug_1.IDebugService),
    tslib_1.__metadata("design:type", Object)
], DebuggersContributionPoint.prototype, "debugService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(debug_configuration_manager_1.DebugConfigurationManager),
    tslib_1.__metadata("design:type", debug_configuration_manager_1.DebugConfigurationManager)
], DebuggersContributionPoint.prototype, "debugConfigurationManager", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(debug_schema_manager_1.DebugSchemaManager),
    tslib_1.__metadata("design:type", debug_schema_manager_1.DebugSchemaManager)
], DebuggersContributionPoint.prototype, "debugSchemaManager", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(types_1.AbstractExtInstanceManagementService),
    tslib_1.__metadata("design:type", types_1.AbstractExtInstanceManagementService)
], DebuggersContributionPoint.prototype, "extensionManageService", void 0);
exports.DebuggersContributionPoint = DebuggersContributionPoint = tslib_1.__decorate([
    (0, di_1.Injectable)(),
    (0, common_1.Contributes)('debuggers'),
    (0, common_1.LifeCycle)(4 /* LifeCyclePhase.Ready */)
], DebuggersContributionPoint);
//# sourceMappingURL=debuggers.js.map