"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SANDBOX_LIFECYCLE_CONTRACT = exports.ensureSandboxRunDirectory = void 0;
exports.scaffoldWorkspaceInit = scaffoldWorkspaceInit;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const command_write_summary_1 = require("../license/command-write-summary");
const default_assets_1 = require("./default-assets");
const sandbox_run_directory_1 = require("./sandbox-run-directory");
Object.defineProperty(exports, "ensureSandboxRunDirectory", { enumerable: true, get: function () { return sandbox_run_directory_1.ensureSandboxRunDirectory; } });
Object.defineProperty(exports, "SANDBOX_LIFECYCLE_CONTRACT", { enumerable: true, get: function () { return sandbox_run_directory_1.SANDBOX_LIFECYCLE_CONTRACT; } });
function scaffoldWorkspaceInit(options) {
    const createdDirectories = [];
    for (const relativeDirectory of default_assets_1.BASELINE_WORKSPACE_DIRECTORIES) {
        const absoluteDirectory = node_path_1.default.join(options.repositoryRoot, relativeDirectory);
        const directoryAlreadyExists = node_fs_1.default.existsSync(absoluteDirectory);
        node_fs_1.default.mkdirSync(absoluteDirectory, { recursive: true });
        if (!directoryAlreadyExists) {
            createdDirectories.push(relativeDirectory);
        }
    }
    const markdownlintPath = default_assets_1.RECOMMENDED_MARKDOWNLINT_PATH;
    const markdownlintAbsolutePath = node_path_1.default.join(options.repositoryRoot, markdownlintPath);
    let markdownlintCreated = false;
    if (!node_fs_1.default.existsSync(markdownlintAbsolutePath)) {
        node_fs_1.default.writeFileSync(markdownlintAbsolutePath, (0, default_assets_1.renderRecommendedMarkdownlintContents)(), "utf8");
        markdownlintCreated = true;
    }
    const dayShiftMarkdownlintAbsolutePath = node_path_1.default.join(options.repositoryRoot, default_assets_1.DAY_SHIFT_SCOPED_MARKDOWNLINT_PATH);
    const dayShiftMarkdownlintCreated = !node_fs_1.default.existsSync(dayShiftMarkdownlintAbsolutePath);
    if (!node_fs_1.default.existsSync(dayShiftMarkdownlintAbsolutePath)) {
        node_fs_1.default.writeFileSync(dayShiftMarkdownlintAbsolutePath, (0, default_assets_1.renderDayShiftScopedMarkdownlintContents)(), "utf8");
    }
    const specIndexAbsolutePath = node_path_1.default.join(options.repositoryRoot, default_assets_1.DEFAULT_SPEC_INDEX_PATH);
    const specIndexCreated = !node_fs_1.default.existsSync(specIndexAbsolutePath);
    if (!node_fs_1.default.existsSync(specIndexAbsolutePath)) {
        node_fs_1.default.writeFileSync(specIndexAbsolutePath, (0, default_assets_1.renderDefaultSpecIndexContents)(), "utf8");
    }
    const rootSpecRegistrationAbsolutePath = node_path_1.default.join(options.repositoryRoot, default_assets_1.DEFAULT_ROOT_SPEC_REGISTRATION_PATH);
    const rootSpecRegistrationCreated = !node_fs_1.default.existsSync(rootSpecRegistrationAbsolutePath);
    if (!node_fs_1.default.existsSync(rootSpecRegistrationAbsolutePath)) {
        node_fs_1.default.writeFileSync(rootSpecRegistrationAbsolutePath, (0, default_assets_1.renderDefaultRootSpecRegistrationContents)(), "utf8");
    }
    return {
        ensuredDirectories: default_assets_1.BASELINE_WORKSPACE_DIRECTORIES,
        createdDirectories,
        markdownlintPath,
        markdownlintCreated,
        commandWriteSummary: (0, command_write_summary_1.normalizeCommandWriteSummary)({
            commandFamily: "workspace_init",
            primaryOutputArtifacts: [
                markdownlintPath,
                default_assets_1.DAY_SHIFT_SCOPED_MARKDOWNLINT_PATH,
                default_assets_1.DEFAULT_SPEC_INDEX_PATH,
                default_assets_1.DEFAULT_ROOT_SPEC_REGISTRATION_PATH
            ],
            updatedArtifacts: [
                {
                    path: markdownlintPath,
                    action: markdownlintCreated ? "created" : "kept",
                    reason: markdownlintCreated ? "non_coordination_artifact" : "no_write"
                },
                {
                    path: default_assets_1.DAY_SHIFT_SCOPED_MARKDOWNLINT_PATH,
                    action: dayShiftMarkdownlintCreated ? "created" : "kept",
                    reason: dayShiftMarkdownlintCreated ? "non_coordination_artifact" : "no_write"
                },
                {
                    path: default_assets_1.DEFAULT_SPEC_INDEX_PATH,
                    action: specIndexCreated ? "created" : "kept",
                    reason: specIndexCreated ? "accepted_create" : "no_write",
                    isCoordinationArtifact: true,
                    materialUpdate: specIndexCreated
                },
                {
                    path: default_assets_1.DEFAULT_ROOT_SPEC_REGISTRATION_PATH,
                    action: rootSpecRegistrationCreated ? "created" : "kept",
                    reason: rootSpecRegistrationCreated ? "accepted_create" : "no_write",
                    isCoordinationArtifact: true,
                    materialUpdate: rootSpecRegistrationCreated
                }
            ]
        })
    };
}
