"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SHARED_AUTHORING_SOURCE_ROOTS = exports.DAY_SHIFT_SCOPED_MARKDOWNLINT_CONTENTS = exports.RECOMMENDED_MARKDOWNLINT_CONTENTS = exports.DEFAULT_ROOT_SPEC_REGISTRATION_PATH = exports.DEFAULT_SPEC_INDEX_PATH = exports.DAY_SHIFT_SCOPED_MARKDOWNLINT_PATH = exports.RECOMMENDED_MARKDOWNLINT_PATH = exports.BASELINE_WORKSPACE_DIRECTORIES = void 0;
exports.renderRecommendedMarkdownlintContents = renderRecommendedMarkdownlintContents;
exports.renderDayShiftScopedMarkdownlintContents = renderDayShiftScopedMarkdownlintContents;
exports.renderDefaultSpecIndexContents = renderDefaultSpecIndexContents;
exports.renderDefaultRootSpecRegistrationContents = renderDefaultRootSpecRegistrationContents;
exports.getDefaultSharedAuthoringAssets = getDefaultSharedAuthoringAssets;
exports.discoverSharedAuthoringAssetsForPackageRoot = discoverSharedAuthoringAssetsForPackageRoot;
exports.getDefaultSharedAuthoringAssetPaths = getDefaultSharedAuthoringAssetPaths;
exports.getDefaultPackagedPromptMarkdownAssets = getDefaultPackagedPromptMarkdownAssets;
exports.resolvePackageRootFromWorkspaceInitModuleDirectory = resolvePackageRootFromWorkspaceInitModuleDirectory;
exports.readPackagedWorkspaceAsset = readPackagedWorkspaceAsset;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const runtime_contract_1 = require("../runtime/runtime-contract");
exports.BASELINE_WORKSPACE_DIRECTORIES = Object.freeze([
    runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME,
    node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "planning"),
    node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "policy-packs"),
    node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "prompts"),
    node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "prompts", "user"),
    node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "prompts", "latest"),
    node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "references"),
    node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "specs"),
    node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "templates"),
    node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "workflows")
]);
exports.RECOMMENDED_MARKDOWNLINT_PATH = ".markdownlint.json";
exports.DAY_SHIFT_SCOPED_MARKDOWNLINT_PATH = node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, ".markdownlint.json");
exports.DEFAULT_SPEC_INDEX_PATH = node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "specs", "index.json");
exports.DEFAULT_ROOT_SPEC_REGISTRATION_PATH = node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "specs", "root-spec.md");
exports.RECOMMENDED_MARKDOWNLINT_CONTENTS = Object.freeze({
    default: true,
    MD013: false,
    MD024: {
        siblings_only: true
    },
    MD025: {
        front_matter_title: "^\\s*__day_shift_front_matter_title_is_metadata__\\s*:"
    },
    MD033: false
});
exports.DAY_SHIFT_SCOPED_MARKDOWNLINT_CONTENTS = Object.freeze({
    default: true,
    MD013: false,
    MD024: {
        siblings_only: true
    },
    MD025: {
        front_matter_title: "^\\s*__day_shift_front_matter_title_is_metadata__\\s*:"
    },
    MD033: false
});
exports.SHARED_AUTHORING_SOURCE_ROOTS = Object.freeze([
    node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "prompts", "user"),
    node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "policy-packs"),
    node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "references"),
    node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "workflows"),
    node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "templates")
]);
const EXCLUDED_SOURCE_BASENAMES = new Set([".gitkeep"]);
let cachedSharedAuthoringAssets = null;
function renderRecommendedMarkdownlintContents() {
    return readPackagedWorkspaceAsset(node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "templates", "markdownlint.json"), `${JSON.stringify(exports.RECOMMENDED_MARKDOWNLINT_CONTENTS, null, 2)}\n`);
}
function renderDayShiftScopedMarkdownlintContents() {
    return readPackagedWorkspaceAsset(exports.DAY_SHIFT_SCOPED_MARKDOWNLINT_PATH, `${JSON.stringify(exports.DAY_SHIFT_SCOPED_MARKDOWNLINT_CONTENTS, null, 2)}\n`);
}
function renderDefaultSpecIndexContents() {
    return `${JSON.stringify({
        spec_index_schema_version: 1,
        generated_at: null,
        notes: [
            "Day Shift initialized this spec registry so source specifications can be registered without creating the file by hand.",
            "A root-spec registration anchor is scaffolded by default so the workspace has a discoverable source-spec starting point before slice planning exists."
        ],
        specs: [
            {
                id: "root-spec",
                title: "Root Specification",
                depth: "standard",
                status: "active",
                destination: "root-spec",
                spec_path: ".day-shift/specs/root-spec.md",
                source: {
                    type: "markdown",
                    ref: "root-spec.md"
                }
            }
        ]
    }, null, 2)}\n`;
}
function renderDefaultRootSpecRegistrationContents() {
    return [
        "---",
        "artifact_schema_version: 1",
        "artifact_type: specification",
        "spec_id: root-spec",
        "title: Root Specification",
        "depth: standard",
        "status: active",
        "destination: root-spec",
        "source:",
        "  type: markdown",
        "  ref: root-spec.md",
        "---",
        "",
        "# Root Specification",
        "",
        "## Purpose",
        "",
        "Provide a stable Day Shift registration anchor for the repository's root-level source spec.",
        "",
        "## Source Of Truth",
        "",
        "Treat `root-spec.md` at repository root as the source of truth for this registration artifact.",
        "",
        "## Next Step",
        "",
        "Decompose `root-spec.md` into implementation-ready Day Shift specifications and register those scoped specs before creating downstream planning artifacts.",
        ""
    ].join("\n");
}
function getDefaultSharedAuthoringAssets() {
    if (cachedSharedAuthoringAssets !== null) {
        return cachedSharedAuthoringAssets;
    }
    const packageRoot = resolvePackageRoot();
    cachedSharedAuthoringAssets = discoverSharedAuthoringAssetsForPackageRoot(packageRoot);
    return cachedSharedAuthoringAssets;
}
function discoverSharedAuthoringAssetsForPackageRoot(packageRoot) {
    const assets = [];
    for (const sourceRoot of exports.SHARED_AUTHORING_SOURCE_ROOTS) {
        const sourceAbsolutePath = node_path_1.default.join(packageRoot, sourceRoot);
        if (!node_fs_1.default.existsSync(sourceAbsolutePath)) {
            continue;
        }
        collectSharedAuthoringAssets({
            packageRoot,
            sourceAbsolutePath,
            assets
        });
    }
    assets.sort((a, b) => a.path.localeCompare(b.path));
    return Object.freeze(assets);
}
function getDefaultSharedAuthoringAssetPaths() {
    return getDefaultSharedAuthoringAssets().map((asset) => asset.path);
}
function getDefaultPackagedPromptMarkdownAssets() {
    const promptUserRoot = node_path_1.default.posix.join(runtime_contract_1.DAY_SHIFT_DIRECTORY_NAME, "prompts", "user");
    const promptUserPrefix = `${promptUserRoot}/`;
    return Object.freeze(getDefaultSharedAuthoringAssets()
        .filter((asset) => asset.path.startsWith(promptUserPrefix) && asset.path.endsWith(".md"))
        .map((asset) => Object.freeze({
        ...asset,
        promptSuffix: asset.path.slice(promptUserPrefix.length)
    })));
}
function collectSharedAuthoringAssets(options) {
    for (const entry of node_fs_1.default.readdirSync(options.sourceAbsolutePath, { withFileTypes: true })) {
        const entryAbsolutePath = node_path_1.default.join(options.sourceAbsolutePath, entry.name);
        if (entry.isDirectory()) {
            collectSharedAuthoringAssets({
                ...options,
                sourceAbsolutePath: entryAbsolutePath
            });
            continue;
        }
        if (!entry.isFile() || EXCLUDED_SOURCE_BASENAMES.has(entry.name)) {
            continue;
        }
        const relativePath = node_path_1.default.relative(options.packageRoot, entryAbsolutePath).replace(/\\/g, "/");
        options.assets.push({
            path: relativePath,
            contents: node_fs_1.default.readFileSync(entryAbsolutePath, "utf8")
        });
    }
}
function resolvePackageRoot() {
    return resolvePackageRootFromWorkspaceInitModuleDirectory(__dirname);
}
function resolvePackageRootFromWorkspaceInitModuleDirectory(moduleDirectory) {
    // dist/lib/workspace-init/default-assets.js -> repository root
    return node_path_1.default.resolve(moduleDirectory, "../../..");
}
function readPackagedWorkspaceAsset(relativePath, fallbackContents) {
    const packageRoot = resolvePackageRoot();
    const absolutePath = node_path_1.default.join(packageRoot, ...relativePath.split("/"));
    if (node_fs_1.default.existsSync(absolutePath) && node_fs_1.default.statSync(absolutePath).isFile()) {
        return node_fs_1.default.readFileSync(absolutePath, "utf8");
    }
    return fallbackContents;
}
