"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeInitSafetyPlan = executeInitSafetyPlan;
const init_operation_planning_1 = require("./init-operation-planning");
const scaffold_1 = require("./scaffold");
function executeInitSafetyPlan(options) {
    const operations = [];
    let sandbox = null;
    for (const directoryOperation of (0, init_operation_planning_1.planBaselineDirectoryOperations)(options.repositoryRoot)) {
        operations.push({
            type: "directory",
            path: withOperationPathPrefix(directoryOperation.path, options.operationPathPrefix),
            action: directoryOperation.action,
            writeOutcome: directoryOperation.action === "create" ? "create" : "keep",
            recoveryGuidance: []
        });
        if (!options.dryRun) {
            (0, init_operation_planning_1.applyDirectoryOperation)(options.repositoryRoot, directoryOperation);
        }
    }
    for (const fileOperation of (0, init_operation_planning_1.collectPlannedInitFileOperations)({
        repositoryRoot: options.repositoryRoot,
        force: options.force,
        scaffoldAgentEntrypoint: options.scaffoldAgentEntrypoint,
        scaffoldSharedAuthoringAssets: options.scaffoldSharedAuthoringAssets
    })) {
        if (fileOperation.kind === "disabled-file") {
            operations.push({
                type: "file",
                path: withOperationPathPrefix(fileOperation.path, options.operationPathPrefix),
                action: "noop",
                writeOutcome: "keep",
                reason: "disabled",
                disabledBy: fileOperation.disabledBy,
                recoveryGuidance: []
            });
            continue;
        }
        operations.push({
            type: "file",
            path: withOperationPathPrefix(fileOperation.entry.plan.path, options.operationPathPrefix),
            action: fileOperation.entry.plan.action,
            writeOutcome: fileOperation.entry.plan.writeOutcome,
            reason: fileOperation.entry.plan.reason,
            recoveryGuidance: fileOperation.entry.plan.recoveryGuidance
        });
        if (!options.dryRun) {
            (0, init_operation_planning_1.applyManagedFileWrite)(options.repositoryRoot, fileOperation.entry);
        }
    }
    if (options.createSandboxRunDirectory === true || options.sandboxRunId !== undefined) {
        sandbox = (0, scaffold_1.ensureSandboxRunDirectory)({
            repositoryRoot: options.repositoryRoot,
            runId: options.sandboxRunId,
            dryRun: options.dryRun,
            now: options.now
        });
        operations.push({
            type: "directory",
            path: withOperationPathPrefix(sandbox.parentDirectory.path, options.operationPathPrefix),
            action: sandbox.parentDirectory.action === "create" ? "create" : "noop",
            writeOutcome: sandbox.parentDirectory.action === "create" ? "create" : "keep",
            recoveryGuidance: []
        });
        operations.push({
            type: "directory",
            path: withOperationPathPrefix(sandbox.runDirectory.path, options.operationPathPrefix),
            action: sandbox.runDirectory.action === "create" ? "create" : "noop",
            writeOutcome: sandbox.runDirectory.action === "create" ? "create" : "keep",
            recoveryGuidance: []
        });
    }
    return {
        dryRun: options.dryRun,
        operations,
        sandbox
    };
}
function withOperationPathPrefix(pathValue, pathPrefix) {
    if (pathPrefix === undefined || pathPrefix.length === 0) {
        return pathValue;
    }
    return `${pathPrefix}/${pathValue}`;
}
