"use strict";
/**
 * Shared prompt injection strategy.
 *
 * Detects which prompt injection mode the user is using and ensures CCS
 * always uses the SAME mode so Claude CLI never receives mixed
 * `--append-system-prompt` and `--append-system-prompt-file` flags.
 *
 * Rules:
 *  - User passes `--append-system-prompt` → all CCS prompts use inline
 *  - User passes `--append-system-prompt-file` → all CCS prompts use file
 *  - Neither present → default to inline (`--append-system-prompt`)
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildSteeringArg = exports.buildFileSteeringArg = exports.buildInlineSteeringArg = exports.detectPromptInjectionMode = exports.hasManagedPromptFileArg = exports.getManagedPromptFilePath = exports.getManagedPromptFileName = exports.PROMPT_FLAG_FILE = exports.PROMPT_FLAG_INLINE = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const config_manager_1 = require("./config-manager");
/** `--append-system-prompt` — inline prompt text */
exports.PROMPT_FLAG_INLINE = '--append-system-prompt';
/** `--append-system-prompt-file` — prompt read from file */
exports.PROMPT_FLAG_FILE = '--append-system-prompt-file';
function getManagedPromptsDir() {
    return path.join((0, config_manager_1.getCcsDir)(), 'prompts');
}
function getManagedPromptFileName(promptName) {
    return `${promptName}.txt`;
}
exports.getManagedPromptFileName = getManagedPromptFileName;
function getManagedPromptFilePath(promptName) {
    return path.join(getManagedPromptsDir(), getManagedPromptFileName(promptName));
}
exports.getManagedPromptFilePath = getManagedPromptFilePath;
function hasManagedPromptFileArg(params) {
    const expectedPath = path.resolve(getManagedPromptFilePath(params.promptName));
    for (let index = 0; index < params.args.length; index += 1) {
        const arg = params.args[index];
        if (arg === exports.PROMPT_FLAG_FILE) {
            const filePath = params.args[index + 1];
            if (filePath && path.resolve(filePath) === expectedPath) {
                return true;
            }
            continue;
        }
        if (arg.startsWith(`${exports.PROMPT_FLAG_FILE}=`) &&
            path.resolve(arg.slice(exports.PROMPT_FLAG_FILE.length + 1)) === expectedPath) {
            return true;
        }
    }
    return false;
}
exports.hasManagedPromptFileArg = hasManagedPromptFileArg;
/**
 * Detect which prompt injection mode to use based on user-provided args.
 *
 * - `--append-system-prompt-file` found (space or `=` form) → 'file'
 * - `--append-system-prompt` found (space or `=` form) → 'inline'
 * - Neither → 'inline' (default)
 */
function detectPromptInjectionMode(args) {
    for (let i = 0; i < args.length; i += 1) {
        const arg = args[i];
        if (arg === exports.PROMPT_FLAG_FILE || arg.startsWith(`${exports.PROMPT_FLAG_FILE}=`)) {
            return 'file';
        }
    }
    return 'inline';
}
exports.detectPromptInjectionMode = detectPromptInjectionMode;
/**
 * Build a `--append-system-prompt <text>` arg pair.
 */
function buildInlineSteeringArg(params) {
    return [exports.PROMPT_FLAG_INLINE, params.promptContent];
}
exports.buildInlineSteeringArg = buildInlineSteeringArg;
/**
 * Build a `--append-system-prompt-file <path>` arg pair.
 * Writes the prompt to a temp file first.
 */
function buildFileSteeringArg(params) {
    const promptsFolder = getManagedPromptsDir();
    if (!fs.existsSync(promptsFolder)) {
        fs.mkdirSync(promptsFolder, { recursive: true });
    }
    const promptFile = path.join(promptsFolder, params.promptFileName);
    fs.writeFileSync(promptFile, params.promptContent);
    return [exports.PROMPT_FLAG_FILE, promptFile];
}
exports.buildFileSteeringArg = buildFileSteeringArg;
/**
 * Build steering prompt args in the given mode.
 */
function buildSteeringArg(params) {
    const mode = detectPromptInjectionMode(params.args);
    if (mode === 'file') {
        return buildFileSteeringArg({
            promptFileName: getManagedPromptFileName(params.promptName),
            promptContent: params.promptContent,
        });
    }
    return buildInlineSteeringArg({ promptContent: params.promptContent });
}
exports.buildSteeringArg = buildSteeringArg;
//# sourceMappingURL=prompt-injection-strategy.js.map