
import { Codicon } from '../../../../base/common/codicons.js';
import '../../../../base/common/observableInternal/index.js';
import { joinPath } from '../../../../base/common/resources.js';
import { ThemeIcon } from '../../../../base/common/themables.js';
import { localize } from '../../../../nls.js';
import { AICustomizationManagementSection } from './aiCustomizationWorkspaceService.js';
import { PromptsType } from './promptSyntax/promptTypes.js';
import { PromptsStorage } from './promptSyntax/service/promptsService.js';
import { AGENT_MD_FILENAME } from './promptSyntax/config/promptFileLocations.js';
import { observableValue } from '../../../../base/common/observableInternal/observables/observableValue.js';
import { constObservable } from '../../../../base/common/observableInternal/observables/constObservable.js';

var CustomizationHarness;
(function(CustomizationHarness) {
    CustomizationHarness["VSCode"] = "vscode";
    CustomizationHarness["CLI"] = "cli";
    CustomizationHarness["Claude"] = "claude";
})(CustomizationHarness || (CustomizationHarness = {}));
const HOOKS_FILTER = {
    sources: [PromptsStorage.local, PromptsStorage.plugin]
};
function getCliUserRoots(userHome) {
    return [
        joinPath(userHome, ".copilot"),
        joinPath(userHome, ".claude"),
        joinPath(userHome, ".agents")
    ];
}
function getClaudeUserRoots(userHome) {
    return [joinPath(userHome, ".claude")];
}
function buildAllSources(extras) {
    return [
        PromptsStorage.local,
        PromptsStorage.user,
        PromptsStorage.plugin,
        ...extras
    ];
}
function createVSCodeHarnessDescriptor(extras) {
    const filter = {
        sources: buildAllSources(extras)
    };
    return {
        id: CustomizationHarness.VSCode,
        label: ( localize(7203, "Local")),
        icon: ThemeIcon.fromId(Codicon.vm.id),
        sectionOverrides: ( new Map([[AICustomizationManagementSection.Instructions, {
            rootFileShortcuts: [AGENT_MD_FILENAME]
        }]])),
        getStorageSourceFilter: () => filter
    };
}
function createRestrictedHarnessDescriptor(id, label, icon, restrictedUserRoots, extras, options) {
    const allSources = buildAllSources(extras);
    const allRootsFilter = {
        sources: allSources
    };
    const restrictedFilter = {
        sources: allSources,
        includedUserFileRoots: restrictedUserRoots
    };
    return {
        id,
        label,
        icon,
        hiddenSections: options?.hiddenSections,
        workspaceSubpaths: options?.workspaceSubpaths,
        hideGenerateButton: options?.hideGenerateButton,
        sectionOverrides: options?.sectionOverrides,
        requiredAgentId: options?.requiredAgentId,
        instructionFileFilter: options?.instructionFileFilter,
        getStorageSourceFilter(type) {
            if (type === PromptsType.hook) {
                return HOOKS_FILTER;
            }
            if (type === PromptsType.prompt) {
                return allRootsFilter;
            }
            return restrictedFilter;
        }
    };
}
function createCliHarnessDescriptor(cliUserRoots, extras) {
    return createRestrictedHarnessDescriptor(CustomizationHarness.CLI, ( localize(7204, "Copilot CLI")), ThemeIcon.fromId(Codicon.worktree.id), cliUserRoots, extras, {
        hideGenerateButton: true,
        requiredAgentId: "copilotcli",
        workspaceSubpaths: [".github", ".copilot", ".agents", ".claude"],
        sectionOverrides: ( new Map([[AICustomizationManagementSection.Instructions, {
            rootFileShortcuts: [AGENT_MD_FILENAME]
        }]]))
    });
}
function createClaudeHarnessDescriptor(claudeRoots, extras) {
    return createRestrictedHarnessDescriptor(CustomizationHarness.Claude, ( localize(7205, "Claude")), ThemeIcon.fromId(Codicon.claude.id), claudeRoots, extras, {
        hiddenSections: [
            AICustomizationManagementSection.Prompts,
            AICustomizationManagementSection.Plugins
        ],
        workspaceSubpaths: [".claude"],
        hideGenerateButton: true,
        requiredAgentId: "claude-code",
        sectionOverrides: ( new Map([[AICustomizationManagementSection.Hooks, {
            label: ( localize(7206, "Configure Claude Hooks")),
            commandId: "copilot.claude.hooks"
        }], [AICustomizationManagementSection.Instructions, {
            label: ( localize(7207, "Add CLAUDE.md")),
            rootFile: "CLAUDE.md",
            typeLabel: ( localize(7208, "Rule")),
            fileExtension: ".md"
        }]])),
        instructionFileFilter: [
            "CLAUDE.md",
            "CLAUDE.local.md",
            ".claude/rules/",
            "copilot-instructions.md"
        ]
    });
}
function matchesWorkspaceSubpath(filePath, subpaths) {
    return ( subpaths.some(sp => filePath.includes(`/${sp}/`) || filePath.endsWith(`/${sp}`)));
}
function matchesInstructionFileFilter(filePath, filters) {
    const name = filePath.substring(filePath.lastIndexOf("/") + 1);
    return ( filters.some(f => {
        if (f.endsWith("/")) {
            return filePath.includes(`/${f}`) || filePath.startsWith(f);
        }
        return name === f;
    }));
}
class CustomizationHarnessServiceBase {
    constructor(harnesses, defaultHarness, availableHarnesses) {
        this._allHarnesses = harnesses;
        this._activeHarness = observableValue(this, defaultHarness);
        this.activeHarness = this._activeHarness;
        this.availableHarnesses = availableHarnesses ?? constObservable(harnesses);
    }
    setActiveHarness(id) {
        const available = this.availableHarnesses.get();
        if (( available.some(h => h.id === id))) {
            this._activeHarness.set(id, undefined);
        }
    }
    getStorageSourceFilter(type) {
        const descriptor = this.getActiveDescriptor();
        return descriptor.getStorageSourceFilter(type);
    }
    getActiveDescriptor() {
        const activeId = this._activeHarness.get();
        const available = this.availableHarnesses.get();
        return available.find(h => h.id === activeId) ?? available[0] ?? this._allHarnesses[0];
    }
}

export { CustomizationHarness, CustomizationHarnessServiceBase, createClaudeHarnessDescriptor, createCliHarnessDescriptor, createVSCodeHarnessDescriptor, getClaudeUserRoots, getCliUserRoots, matchesInstructionFileFilter, matchesWorkspaceSubpath };
