
import { getAllCodicons, Codicon } from '../../../base/common/codicons.js';
import { Platform, PlatformToString, OperatingSystem } from '../../../base/common/platform.js';
import { localize } from '../../../nls.js';
import { ConfigurationScope, Extensions } from '../../configuration/common/configurationRegistry.js';
import { Registry } from '../../registry/common/platform.js';
import { TerminalSettingId } from './terminal.js';
import { createProfileSchemaEnums } from './terminalProfiles.js';

const terminalColorSchema = {
    type: ["string", "null"],
    enum: [
        "terminal.ansiBlack",
        "terminal.ansiRed",
        "terminal.ansiGreen",
        "terminal.ansiYellow",
        "terminal.ansiBlue",
        "terminal.ansiMagenta",
        "terminal.ansiCyan",
        "terminal.ansiWhite"
    ],
    default: null
};
const terminalIconSchema = {
    type: "string",
    enum: Array.from(getAllCodicons(), icon => icon.id),
    markdownEnumDescriptions: Array.from(getAllCodicons(), icon => `$(${icon.id})`)
};
const terminalProfileBaseProperties = {
    args: {
        description: ( localize(2197, "An optional set of arguments to run the shell executable with.")),
        type: "array",
        items: {
            type: "string"
        }
    },
    icon: {
        description: ( localize(2198, "A codicon ID to associate with the terminal icon.")),
        ...terminalIconSchema
    },
    color: {
        description: ( localize(2199, "A theme color ID to associate with the terminal icon.")),
        ...terminalColorSchema
    },
    env: {
        markdownDescription: ( localize(
            2200,
            "An object with environment variables that will be added to the terminal profile process. Set to `null` to delete environment variables from the base environment."
        )),
        type: "object",
        additionalProperties: {
            type: ["string", "null"]
        },
        default: {}
    }
};
const terminalProfileSchema = {
    type: "object",
    required: ["path"],
    properties: {
        path: {
            description: ( localize(
                2201,
                "A single path to a shell executable or an array of paths that will be used as fallbacks when one fails."
            )),
            type: ["string", "array"],
            items: {
                type: "string"
            }
        },
        overrideName: {
            description: ( localize(
                2202,
                "Whether or not to replace the dynamic terminal title that detects what program is running with the static profile name."
            )),
            type: "boolean"
        },
        ...terminalProfileBaseProperties
    }
};
const terminalAutomationProfileSchema = {
    type: "object",
    required: ["path"],
    properties: {
        path: {
            description: ( localize(2203, "A path to a shell executable.")),
            type: ["string"],
            items: {
                type: "string"
            }
        },
        ...terminalProfileBaseProperties
    }
};
function createTerminalProfileMarkdownDescription(platform) {
    const key = platform === Platform.Linux ? "linux" : platform === Platform.Mac ? "osx" : "windows";
    return localize(
        2204,
        "A set of terminal profile customizations for {0} which allows adding, removing or changing how terminals are launched. Profiles are made up of a mandatory path, optional arguments and other presentation options.\n\nTo override an existing profile use its profile name as the key, for example:\n\n{1}\n\n{2}Read more about configuring profiles{3}.",
        PlatformToString(platform),
        "```json\n\"terminal.integrated.profile." + key + "\": {\n  \"bash\": null\n}\n```",
        "[",
        "](https://code.visualstudio.com/docs/terminal/profiles)"
    );
}
const terminalPlatformConfiguration = {
    id: "terminal",
    order: 100,
    title: ( localize(2205, "Integrated Terminal")),
    type: "object",
    properties: {
        [TerminalSettingId.AutomationProfileLinux]: {
            restricted: true,
            markdownDescription: ( localize(
                2206,
                "The terminal profile to use on Linux for automation-related terminal usage like tasks and debug."
            )),
            type: ["object", "null"],
            default: null,
            "anyOf": [{
                type: "null"
            }, terminalAutomationProfileSchema],
            defaultSnippets: [{
                body: {
                    path: "${1}",
                    icon: "${2}"
                }
            }]
        },
        [TerminalSettingId.AutomationProfileMacOs]: {
            restricted: true,
            markdownDescription: ( localize(
                2207,
                "The terminal profile to use on macOS for automation-related terminal usage like tasks and debug."
            )),
            type: ["object", "null"],
            default: null,
            "anyOf": [{
                type: "null"
            }, terminalAutomationProfileSchema],
            defaultSnippets: [{
                body: {
                    path: "${1}",
                    icon: "${2}"
                }
            }]
        },
        [TerminalSettingId.AutomationProfileWindows]: {
            restricted: true,
            markdownDescription: ( localize(
                2208,
                "The terminal profile to use for automation-related terminal usage like tasks and debug. This setting will currently be ignored if {0} (now deprecated) is set.",
                "`terminal.integrated.automationShell.windows`"
            )),
            type: ["object", "null"],
            default: null,
            "anyOf": [{
                type: "null"
            }, terminalAutomationProfileSchema],
            defaultSnippets: [{
                body: {
                    path: "${1}",
                    icon: "${2}"
                }
            }]
        },
        [TerminalSettingId.AgentHostProfileLinux]: {
            restricted: true,
            markdownDescription: ( localize(
                2209,
                "The terminal profile to use on Linux for agent host terminals, including shells launched by AI agent tools. Accepts either a profile name from {0} or an inline profile object. When unset, falls back to {1}. Currently applies to the local agent host. Only the executable `path` is honored today; `args` and `env` from the profile are ignored. Remote agent hosts need remote-side shell configuration because local resolved paths may be invalid on the remote.",
                "`#terminal.integrated.profiles.linux#`",
                "`#terminal.integrated.defaultProfile.linux#`"
            )),
            type: ["string", "object", "null"],
            default: null,
            "anyOf": [{
                type: "null"
            }, {
                type: "string"
            }, terminalAutomationProfileSchema],
            defaultSnippets: [{
                body: {
                    path: "${1}",
                    icon: "${2}"
                }
            }]
        },
        [TerminalSettingId.AgentHostProfileMacOs]: {
            restricted: true,
            markdownDescription: ( localize(
                2210,
                "The terminal profile to use on macOS for agent host terminals, including shells launched by AI agent tools. Accepts either a profile name from {0} or an inline profile object. When unset, falls back to {1}. Currently applies to the local agent host. Only the executable `path` is honored today; `args` and `env` from the profile are ignored. Remote agent hosts need remote-side shell configuration because local resolved paths may be invalid on the remote.",
                "`#terminal.integrated.profiles.osx#`",
                "`#terminal.integrated.defaultProfile.osx#`"
            )),
            type: ["string", "object", "null"],
            default: null,
            "anyOf": [{
                type: "null"
            }, {
                type: "string"
            }, terminalAutomationProfileSchema],
            defaultSnippets: [{
                body: {
                    path: "${1}",
                    icon: "${2}"
                }
            }]
        },
        [TerminalSettingId.AgentHostProfileWindows]: {
            restricted: true,
            markdownDescription: ( localize(
                2211,
                "The terminal profile to use on Windows for agent host terminals, including shells launched by AI agent tools. Accepts either a profile name from {0} or an inline profile object. When unset, falls back to {1}. Currently applies to the local agent host. Only the executable `path` is honored today; `args` and `env` from the profile are ignored. Remote agent hosts need remote-side shell configuration because local resolved paths may be invalid on the remote.",
                "`#terminal.integrated.profiles.windows#`",
                "`#terminal.integrated.defaultProfile.windows#`"
            )),
            type: ["string", "object", "null"],
            default: null,
            "anyOf": [{
                type: "null"
            }, {
                type: "string"
            }, terminalAutomationProfileSchema],
            defaultSnippets: [{
                body: {
                    path: "${1}",
                    icon: "${2}"
                }
            }]
        },
        [TerminalSettingId.ProfilesWindows]: {
            restricted: true,
            markdownDescription: createTerminalProfileMarkdownDescription(Platform.Windows),
            type: "object",
            default: {
                "PowerShell": {
                    source: "PowerShell",
                    icon: Codicon.terminalPowershell.id
                },
                "Command Prompt": {
                    path: ["${env:windir}\\Sysnative\\cmd.exe", "${env:windir}\\System32\\cmd.exe"],
                    args: [],
                    icon: Codicon.terminalCmd.id
                },
                "Git Bash": {
                    source: "Git Bash",
                    icon: Codicon.terminalGitBash.id
                }
            },
            additionalProperties: {
                "anyOf": [{
                    type: "object",
                    required: ["source"],
                    properties: {
                        source: {
                            description: ( localize(
                                2212,
                                "A profile source that will auto detect the paths to the shell. Note that non-standard executable locations are not supported and must be created manually in a new profile."
                            )),
                            enum: ["PowerShell", "Git Bash"]
                        },
                        ...terminalProfileBaseProperties
                    }
                }, {
                    type: "object",
                    required: ["extensionIdentifier", "id", "title"],
                    properties: {
                        extensionIdentifier: {
                            description: ( localize(2213, "The extension that contributed this profile.")),
                            type: "string"
                        },
                        id: {
                            description: ( localize(2214, "The id of the extension terminal")),
                            type: "string"
                        },
                        title: {
                            description: ( localize(2215, "The name of the extension terminal")),
                            type: "string"
                        },
                        ...terminalProfileBaseProperties
                    }
                }, {
                    type: "null"
                }, terminalProfileSchema]
            }
        },
        [TerminalSettingId.ProfilesMacOs]: {
            restricted: true,
            markdownDescription: createTerminalProfileMarkdownDescription(Platform.Mac),
            type: "object",
            default: {
                "bash": {
                    path: "bash",
                    args: ["-l"],
                    icon: Codicon.terminalBash.id
                },
                "zsh": {
                    path: "zsh",
                    args: ["-l"]
                },
                "fish": {
                    path: "fish",
                    args: ["-l"]
                },
                "tmux": {
                    path: "tmux",
                    icon: Codicon.terminalTmux.id
                },
                "pwsh": {
                    path: "pwsh",
                    icon: Codicon.terminalPowershell.id
                }
            },
            additionalProperties: {
                "anyOf": [{
                    type: "object",
                    required: ["extensionIdentifier", "id", "title"],
                    properties: {
                        extensionIdentifier: {
                            description: ( localize(2216, "The extension that contributed this profile.")),
                            type: "string"
                        },
                        id: {
                            description: ( localize(2217, "The id of the extension terminal")),
                            type: "string"
                        },
                        title: {
                            description: ( localize(2218, "The name of the extension terminal")),
                            type: "string"
                        },
                        ...terminalProfileBaseProperties
                    }
                }, {
                    type: "null"
                }, terminalProfileSchema]
            }
        },
        [TerminalSettingId.ProfilesLinux]: {
            restricted: true,
            markdownDescription: createTerminalProfileMarkdownDescription(Platform.Linux),
            type: "object",
            default: {
                "bash": {
                    path: "bash",
                    icon: Codicon.terminalBash.id
                },
                "zsh": {
                    path: "zsh"
                },
                "fish": {
                    path: "fish"
                },
                "tmux": {
                    path: "tmux",
                    icon: Codicon.terminalTmux.id
                },
                "pwsh": {
                    path: "pwsh",
                    icon: Codicon.terminalPowershell.id
                }
            },
            additionalProperties: {
                "anyOf": [{
                    type: "object",
                    required: ["extensionIdentifier", "id", "title"],
                    properties: {
                        extensionIdentifier: {
                            description: ( localize(2219, "The extension that contributed this profile.")),
                            type: "string"
                        },
                        id: {
                            description: ( localize(2220, "The id of the extension terminal")),
                            type: "string"
                        },
                        title: {
                            description: ( localize(2221, "The name of the extension terminal")),
                            type: "string"
                        },
                        ...terminalProfileBaseProperties
                    }
                }, {
                    type: "null"
                }, terminalProfileSchema]
            }
        },
        [TerminalSettingId.UseWslProfiles]: {
            description: ( localize(
                2222,
                "Controls whether or not WSL distros are shown in the terminal dropdown"
            )),
            type: "boolean",
            default: true
        },
        [TerminalSettingId.InheritEnv]: {
            scope: ConfigurationScope.APPLICATION,
            description: ( localize(
                2223,
                "Whether new shells should inherit their environment from VS Code, which may source a login shell to ensure $PATH and other development variables are initialized. This has no effect on Windows."
            )),
            type: "boolean",
            default: true
        },
        [TerminalSettingId.PersistentSessionScrollback]: {
            scope: ConfigurationScope.APPLICATION,
            markdownDescription: ( localize(
                2224,
                "Controls the maximum amount of lines that will be restored when reconnecting to a persistent terminal session. Increasing this will restore more lines of scrollback at the cost of more memory and increase the time it takes to connect to terminals on start up. This setting requires a restart to take effect and should be set to a value less than or equal to `#terminal.integrated.scrollback#`."
            )),
            type: "number",
            default: 100
        },
        [TerminalSettingId.ShowLinkHover]: {
            scope: ConfigurationScope.APPLICATION,
            description: ( localize(2225, "Whether to show hovers for links in the terminal output.")),
            type: "boolean",
            default: true
        },
        [TerminalSettingId.IgnoreProcessNames]: {
            markdownDescription: ( localize(
                2226,
                "A set of process names to ignore when using the {0} setting.",
                "`#terminal.integrated.confirmOnKill#`"
            )),
            type: "array",
            items: {
                type: "string",
                uniqueItems: true
            },
            default: [
            "starship", "oh-my-posh",
            "bash", "zsh"]
        }
    }
};
function registerTerminalPlatformConfiguration() {
    ( Registry.as(Extensions.Configuration)).registerConfiguration(terminalPlatformConfiguration);
    registerTerminalDefaultProfileConfiguration();
}
let defaultProfilesConfiguration;
function registerTerminalDefaultProfileConfiguration(detectedProfiles, extensionContributedProfiles) {
    const registry = ( Registry.as(Extensions.Configuration));
    let profileEnum;
    if (detectedProfiles) {
        profileEnum = createProfileSchemaEnums(detectedProfiles?.profiles, extensionContributedProfiles);
    }
    const oldDefaultProfilesConfiguration = defaultProfilesConfiguration;
    defaultProfilesConfiguration = {
        id: "terminal",
        order: 100,
        title: ( localize(2205, "Integrated Terminal")),
        type: "object",
        properties: {
            [TerminalSettingId.DefaultProfileLinux]: {
                restricted: true,
                markdownDescription: ( localize(2227, "The default terminal profile on Linux.")),
                type: ["string", "null"],
                default: null,
                enum: detectedProfiles?.os === OperatingSystem.Linux ? profileEnum?.values : undefined,
                markdownEnumDescriptions: detectedProfiles?.os === OperatingSystem.Linux ? profileEnum?.markdownDescriptions : undefined
            },
            [TerminalSettingId.DefaultProfileMacOs]: {
                restricted: true,
                markdownDescription: ( localize(2228, "The default terminal profile on macOS.")),
                type: ["string", "null"],
                default: null,
                enum: detectedProfiles?.os === OperatingSystem.Macintosh ? profileEnum?.values : undefined,
                markdownEnumDescriptions: detectedProfiles?.os === OperatingSystem.Macintosh ? profileEnum?.markdownDescriptions : undefined
            },
            [TerminalSettingId.DefaultProfileWindows]: {
                restricted: true,
                markdownDescription: ( localize(2229, "The default terminal profile on Windows.")),
                type: ["string", "null"],
                default: null,
                enum: detectedProfiles?.os === OperatingSystem.Windows ? profileEnum?.values : undefined,
                markdownEnumDescriptions: detectedProfiles?.os === OperatingSystem.Windows ? profileEnum?.markdownDescriptions : undefined
            }
        }
    };
    registry.updateConfigurations({
        add: [defaultProfilesConfiguration],
        remove: oldDefaultProfilesConfiguration ? [oldDefaultProfilesConfiguration] : []
    });
}

export { registerTerminalDefaultProfileConfiguration, registerTerminalPlatformConfiguration, terminalColorSchema, terminalIconSchema, terminalProfileBaseProperties };
