
import { PromptsType } from '../promptTypes.js';
import { getPromptFileDefaultLocations, PromptFileSource } from './promptFileLocations.js';
import { PromptsStorage } from '../service/promptsService.js';

var PromptsConfig;
(function(PromptsConfig) {
    PromptsConfig.PROMPT_LOCATIONS_KEY = "chat.promptFilesLocations";
    PromptsConfig.INSTRUCTIONS_LOCATION_KEY = "chat.instructionsFilesLocations";
    PromptsConfig.MODE_LOCATION_KEY = "chat.modeFilesLocations";
    PromptsConfig.AGENTS_LOCATION_KEY = "chat.agentFilesLocations";
    PromptsConfig.SKILLS_LOCATION_KEY = "chat.agentSkillsLocations";
    PromptsConfig.HOOKS_LOCATION_KEY = "chat.hookFilesLocations";
    PromptsConfig.PROMPT_FILES_SUGGEST_KEY = "chat.promptFilesRecommendations";
    PromptsConfig.USE_COPILOT_INSTRUCTION_FILES = "github.copilot.chat.codeGeneration.useInstructionFiles";
    PromptsConfig.USE_AGENT_MD = "chat.useAgentsMdFile";
    PromptsConfig.USE_NESTED_AGENT_MD = "chat.useNestedAgentsMdFiles";
    PromptsConfig.USE_CLAUDE_MD = "chat.useClaudeMdFile";
    PromptsConfig.USE_AGENT_SKILLS = "chat.useAgentSkills";
    PromptsConfig.USE_CHAT_HOOKS = "chat.useHooks";
    PromptsConfig.USE_CLAUDE_HOOKS = "chat.useClaudeHooks";
    PromptsConfig.USE_CUSTOM_AGENT_HOOKS = "chat.useCustomAgentHooks";
    PromptsConfig.USE_SKILL_ADHERENCE_PROMPT = "chat.experimental.useSkillAdherencePrompt";
    PromptsConfig.INCLUDE_APPLYING_INSTRUCTIONS = "chat.includeApplyingInstructions";
    PromptsConfig.INCLUDE_REFERENCED_INSTRUCTIONS = "chat.includeReferencedInstructions";
    PromptsConfig.USE_CUSTOMIZATIONS_IN_PARENT_REPOS = "chat.useCustomizationsInParentRepositories";
    function getLocationsValue(configService, type) {
        const key = getPromptFileLocationsConfigKey(type);
        const configValue = configService.getValue(key);
        if (configValue === undefined || configValue === null || Array.isArray(configValue)) {
            return undefined;
        }
        if (typeof configValue === "object") {
            const paths = {};
            for (const [path, value] of Object.entries(configValue)) {
                const cleanPath = path.trim();
                const booleanValue = asBoolean(value);
                if ((booleanValue !== undefined) && cleanPath) {
                    paths[cleanPath] = booleanValue;
                }
            }
            return paths;
        }
        return undefined;
    }
    PromptsConfig.getLocationsValue = getLocationsValue;
    function promptSourceFolders(configService, type) {
        const value = getLocationsValue(configService, type);
        const defaultSourceFolders = getPromptFileDefaultLocations(type);
        if (value && (typeof value === "object")) {
            const paths = [];
            const defaultFolderPathsSet = ( new Set(( defaultSourceFolders.map(f => f.path))));
            for (const defaultFolder of defaultSourceFolders) {
                if (value[defaultFolder.path] !== false) {
                    paths.push(defaultFolder);
                }
            }
            for (const [path, enabledValue] of Object.entries(value)) {
                if ((enabledValue === false) || ( defaultFolderPathsSet.has(path))) {
                    continue;
                }
                const storage = isTildePath(path) ? PromptsStorage.user : PromptsStorage.local;
                paths.push({
                    path,
                    source: storage === PromptsStorage.local ? PromptFileSource.ConfigPersonal : PromptFileSource.ConfigWorkspace,
                    storage
                });
            }
            return paths;
        }
        return [];
    }
    PromptsConfig.promptSourceFolders = promptSourceFolders;
    function getPromptFilesRecommendationsValue(configService, resource) {
        const configValue = configService.getValue(PromptsConfig.PROMPT_FILES_SUGGEST_KEY, {
            resource
        });
        if (!configValue || typeof configValue !== "object" || Array.isArray(configValue)) {
            return undefined;
        }
        const suggestions = {};
        for (const [promptName, value] of Object.entries(configValue)) {
            const cleanPromptName = promptName.trim();
            if (!cleanPromptName) {
                continue;
            }
            if (typeof value === "boolean") {
                suggestions[cleanPromptName] = value;
                continue;
            }
            if (typeof value === "string") {
                const cleanValue = value.trim();
                if (cleanValue) {
                    suggestions[cleanPromptName] = cleanValue;
                }
                continue;
            }
            const booleanValue = asBoolean(value);
            if (booleanValue !== undefined) {
                suggestions[cleanPromptName] = booleanValue;
            }
        }
        return ( Object.keys(suggestions)).length > 0 ? suggestions : undefined;
    }
    PromptsConfig.getPromptFilesRecommendationsValue = getPromptFilesRecommendationsValue;
})(PromptsConfig || (PromptsConfig = {}));
function getPromptFileLocationsConfigKey(type) {
    switch (type) {
    case PromptsType.instructions:
        return PromptsConfig.INSTRUCTIONS_LOCATION_KEY;
    case PromptsType.prompt:
        return PromptsConfig.PROMPT_LOCATIONS_KEY;
    case PromptsType.agent:
        return PromptsConfig.AGENTS_LOCATION_KEY;
    case PromptsType.skill:
        return PromptsConfig.SKILLS_LOCATION_KEY;
    case PromptsType.hook:
        return PromptsConfig.HOOKS_LOCATION_KEY;
    default:
        throw ( new Error("Unknown prompt type"));
    }
}
function asBoolean(value) {
    if (typeof value === "boolean") {
        return value;
    }
    if (typeof value === "string") {
        const cleanValue = value.trim().toLowerCase();
        if (cleanValue === "true") {
            return true;
        }
        if (cleanValue === "false") {
            return false;
        }
        return undefined;
    }
    return undefined;
}
function isTildePath(path) {
    return path.startsWith("~/");
}

export { PromptsConfig, asBoolean, getPromptFileLocationsConfigKey, isTildePath };
