import { getNameFromPath } from '../utils/get-name-from-path.js';
import { basicSearch } from '../utils/basic-search.js';
/**
 * Steering file context provider.
 * Syntax: #[[steering:scope:path]]
 *
 * Every getItems() call fetches the full list fresh from the extension side,
 * replacing the cached snapshot used by search().
 */
export class SteeringContextProvider {
    resolver;
    listItems;
    id = 'steering';
    name = 'Steering';
    description = 'Reference a steering file';
    cache = [];
    constructor(resolver, listItems) {
        this.resolver = resolver;
        this.listItems = listItems;
    }
    /** Resolves the steering file content for the given query path. */
    async resolveContent(query) {
        const [scope, path] = query.split(':');
        if (!scope || !path) {
            return `Malformed query while processing steering context: ${query}`;
        }
        const result = await this.resolver(path);
        // If result is an error, forward the error message
        if (result instanceof Error) {
            return result.message;
        }
        const scopeLabel = scope ? ` [${scope === 'global' ? 'Global' : 'Workspace'}]` : '';
        const precedenceNote = scope === 'workspace'
            ? '\n  Workspace-level rules take precedence over global-level rules when conflicts exist.'
            : '';
        return `## Included Rules (${query})${scopeLabel}

  I am providing you some additional guidance that you should follow for your entire execution. These are intended to steer you in the right direction.
  They have been automatically suggested by the system and may be unrelated to my specific request which follows after them. Consider them, but your number one priority is my request.${precedenceNote}

<user-rule id=${query}>
\`\`\`
${result}
\`\`\`
</user-rule>
`;
    }
    /** Fetches the current steering files and replaces the cached snapshot. */
    async getItems() {
        const items = await this.listItems();
        this.cache = items.map((item) => this.transformItem(item));
        return this.cache;
    }
    /** Substring search across the last cached result. */
    search(query) {
        return basicSearch(this.cache, query);
    }
    transformItem({ path, scope, workspaceFolderName }) {
        return {
            name: getNameFromPath(path),
            description: workspaceFolderName,
            query: `${scope}:${path}`,
        };
    }
}
//# sourceMappingURL=steering.js.map