"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.planBaselineDirectoryOperations = planBaselineDirectoryOperations;
exports.applyDirectoryOperation = applyDirectoryOperation;
exports.collectPlannedInitFileOperations = collectPlannedInitFileOperations;
exports.applyManagedFileWrite = applyManagedFileWrite;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const agent_entrypoint_1 = require("./agent-entrypoint");
const config_scaffold_1 = require("./config-scaffold");
const default_assets_1 = require("./default-assets");
const safety_1 = require("./safety");
function planBaselineDirectoryOperations(repositoryRoot) {
    return default_assets_1.BASELINE_WORKSPACE_DIRECTORIES.map((relativeDirectory) => {
        const absoluteDirectory = node_path_1.default.join(repositoryRoot, relativeDirectory);
        return {
            path: relativeDirectory,
            action: node_fs_1.default.existsSync(absoluteDirectory) ? "noop" : "create"
        };
    });
}
function applyDirectoryOperation(repositoryRoot, operation) {
    if (operation.action !== "create") {
        return;
    }
    node_fs_1.default.mkdirSync(node_path_1.default.join(repositoryRoot, operation.path), { recursive: true });
}
function collectPlannedInitFileOperations(options) {
    const plannedOperations = [];
    plannedOperations.push(createManagedFileOperation({
        repositoryRoot: options.repositoryRoot,
        force: options.force,
        relativePath: default_assets_1.RECOMMENDED_MARKDOWNLINT_PATH,
        expectedContent: (0, default_assets_1.renderRecommendedMarkdownlintContents)()
    }), createManagedFileOperation({
        repositoryRoot: options.repositoryRoot,
        force: options.force,
        relativePath: default_assets_1.DAY_SHIFT_SCOPED_MARKDOWNLINT_PATH,
        expectedContent: (0, default_assets_1.renderDayShiftScopedMarkdownlintContents)()
    }), createManagedFileOperation({
        repositoryRoot: options.repositoryRoot,
        force: options.force,
        relativePath: config_scaffold_1.DEFAULT_REPOSITORY_CONFIG_PATH,
        expectedContent: (0, config_scaffold_1.renderDefaultRepositoryConfig)()
    }), createManagedFileOperation({
        repositoryRoot: options.repositoryRoot,
        force: false,
        relativePath: default_assets_1.DEFAULT_SPEC_INDEX_PATH,
        expectedContent: (0, default_assets_1.renderDefaultSpecIndexContents)()
    }), createManagedFileOperation({
        repositoryRoot: options.repositoryRoot,
        force: false,
        relativePath: default_assets_1.DEFAULT_ROOT_SPEC_REGISTRATION_PATH,
        expectedContent: (0, default_assets_1.renderDefaultRootSpecRegistrationContents)()
    }));
    if (options.scaffoldAgentEntrypoint) {
        plannedOperations.push(createManagedFileOperation({
            repositoryRoot: options.repositoryRoot,
            force: options.force,
            relativePath: agent_entrypoint_1.DEFAULT_AGENT_ENTRYPOINT_PATH,
            expectedContent: (0, agent_entrypoint_1.renderDefaultAgentEntrypoint)()
        }));
    }
    else {
        plannedOperations.push({
            kind: "disabled-file",
            path: agent_entrypoint_1.DEFAULT_AGENT_ENTRYPOINT_PATH,
            disabledBy: "no-agent"
        });
    }
    const shouldScaffoldSharedAuthoringAssets = options.scaffoldSharedAuthoringAssets !== false;
    for (const sharedAuthoringAsset of (0, default_assets_1.getDefaultSharedAuthoringAssets)()) {
        if (!shouldScaffoldSharedAuthoringAssets) {
            plannedOperations.push({
                kind: "disabled-file",
                path: sharedAuthoringAsset.path,
                disabledBy: "without-templates"
            });
            continue;
        }
        plannedOperations.push(createManagedFileOperation({
            repositoryRoot: options.repositoryRoot,
            force: options.force,
            relativePath: sharedAuthoringAsset.path,
            expectedContent: sharedAuthoringAsset.contents
        }));
    }
    return plannedOperations;
}
function applyManagedFileWrite(repositoryRoot, file) {
    if (file.plan.action !== "create" && file.plan.action !== "overwrite") {
        return;
    }
    const absolutePath = node_path_1.default.join(repositoryRoot, file.plan.path);
    node_fs_1.default.mkdirSync(node_path_1.default.dirname(absolutePath), { recursive: true });
    node_fs_1.default.writeFileSync(absolutePath, file.expectedContent, "utf8");
}
function createManagedFileOperation(options) {
    return {
        kind: "managed-file",
        entry: {
            plan: (0, safety_1.planManagedFileWrite)({
                absolutePath: node_path_1.default.join(options.repositoryRoot, options.relativePath),
                relativePath: options.relativePath,
                expectedContent: options.expectedContent,
                force: options.force
            }),
            expectedContent: options.expectedContent
        }
    };
}
