"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeCreateProjectManifestFile = exports.createProjectManifest = exports.createVariantsHistoryManifestPath = exports.createVariantsHistoryPath = exports.createProjectDesignSelectedPath = exports.createProjectDesignManifestPath = exports.createProjectDesignMdPath = exports.createProjectVariantIndexPath = exports.createProjectVariantSnapshotPath = exports.createProjectVariantDiffPath = exports.createProjectVariantBriefPath = exports.createProjectVariantManifestPath = exports.createProjectVariantSessionManifestPath = exports.createProjectVariantPath = exports.createProjectVariantSessionPath = exports.createProjectVariantsPath = exports.createProjectManifestPath = exports.createProjectRivetPath = exports.createProjectVariantSlug = exports.createProjectArtifactsPath = exports.CREATE_PROJECT_MANIFEST_SCHEMA_VERSION = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const CREATE_PROJECT_ARTIFACT_SEGMENTS = ['.rivet', 'create-project'];
const VARIANTS_HISTORY_SUFFIX = '-variants';
const RIVET_DIRNAME = '.rivet';
const VARIANTS_DIRNAME = 'variants';
const DESIGN_MD_DIRNAME = 'design-md';
const MANIFEST_FILENAME = 'manifest.json';
const SELECTED_DESIGN_FILENAME = 'selected.md';
const BRIEF_FILENAME = 'brief.md';
const DIFF_FILENAME = 'diff.patch';
const INDEX_FILENAME = 'index.html';
exports.CREATE_PROJECT_MANIFEST_SCHEMA_VERSION = 1;
const ensureManifestParentDirectory = (manifestPath) => {
    fs_1.default.mkdirSync(path_1.default.dirname(manifestPath), { recursive: true });
};
const createProjectArtifactsPath = (projectPath) => {
    return path_1.default.join(projectPath, ...CREATE_PROJECT_ARTIFACT_SEGMENTS);
};
exports.createProjectArtifactsPath = createProjectArtifactsPath;
const createProjectVariantSlug = (variantName) => {
    const slug = variantName
        .trim()
        .toLowerCase()
        .replace(/[^a-z0-9]+/g, '-')
        .replace(/^-+|-+$/g, '')
        .slice(0, 80);
    return slug || 'variant';
};
exports.createProjectVariantSlug = createProjectVariantSlug;
const createProjectRivetPath = (projectPath) => {
    return path_1.default.join(projectPath, RIVET_DIRNAME);
};
exports.createProjectRivetPath = createProjectRivetPath;
const createProjectManifestPath = (projectPath) => {
    return path_1.default.join((0, exports.createProjectArtifactsPath)(projectPath), MANIFEST_FILENAME);
};
exports.createProjectManifestPath = createProjectManifestPath;
const createProjectVariantsPath = (projectPath) => {
    return path_1.default.join((0, exports.createProjectArtifactsPath)(projectPath), VARIANTS_DIRNAME);
};
exports.createProjectVariantsPath = createProjectVariantsPath;
const createProjectVariantSessionPath = (projectPath, sessionId) => {
    return path_1.default.join((0, exports.createProjectVariantsPath)(projectPath), sessionId);
};
exports.createProjectVariantSessionPath = createProjectVariantSessionPath;
const createProjectVariantPath = (projectPath, variantFolderName) => {
    return path_1.default.join((0, exports.createProjectRivetPath)(projectPath), variantFolderName);
};
exports.createProjectVariantPath = createProjectVariantPath;
const createProjectVariantSessionManifestPath = (projectPath, sessionId) => {
    return path_1.default.join((0, exports.createProjectVariantSessionPath)(projectPath, sessionId), MANIFEST_FILENAME);
};
exports.createProjectVariantSessionManifestPath = createProjectVariantSessionManifestPath;
const createProjectVariantManifestPath = (projectPath, variantFolderName) => {
    return path_1.default.join((0, exports.createProjectVariantPath)(projectPath, variantFolderName), MANIFEST_FILENAME);
};
exports.createProjectVariantManifestPath = createProjectVariantManifestPath;
const createProjectVariantBriefPath = (projectPath, variantFolderName) => {
    return path_1.default.join((0, exports.createProjectVariantPath)(projectPath, variantFolderName), BRIEF_FILENAME);
};
exports.createProjectVariantBriefPath = createProjectVariantBriefPath;
const createProjectVariantDiffPath = (projectPath, variantFolderName) => {
    return path_1.default.join((0, exports.createProjectVariantPath)(projectPath, variantFolderName), DIFF_FILENAME);
};
exports.createProjectVariantDiffPath = createProjectVariantDiffPath;
const createProjectVariantSnapshotPath = (projectPath, variantFolderName) => {
    return (0, exports.createProjectVariantPath)(projectPath, variantFolderName);
};
exports.createProjectVariantSnapshotPath = createProjectVariantSnapshotPath;
const createProjectVariantIndexPath = (projectPath, variantFolderName) => {
    return path_1.default.join((0, exports.createProjectVariantPath)(projectPath, variantFolderName), INDEX_FILENAME);
};
exports.createProjectVariantIndexPath = createProjectVariantIndexPath;
const createProjectDesignMdPath = (projectPath) => {
    return path_1.default.join((0, exports.createProjectArtifactsPath)(projectPath), DESIGN_MD_DIRNAME);
};
exports.createProjectDesignMdPath = createProjectDesignMdPath;
const createProjectDesignManifestPath = (projectPath) => {
    return path_1.default.join((0, exports.createProjectDesignMdPath)(projectPath), MANIFEST_FILENAME);
};
exports.createProjectDesignManifestPath = createProjectDesignManifestPath;
const createProjectDesignSelectedPath = (projectPath) => {
    return path_1.default.join((0, exports.createProjectDesignMdPath)(projectPath), SELECTED_DESIGN_FILENAME);
};
exports.createProjectDesignSelectedPath = createProjectDesignSelectedPath;
/**
 * Path to the sibling-history directory that preserves unchosen vite_app
 * variants after a commit, alongside the chosen project. Layout:
 *
 *   <destinationParent>/<slug>/            (chosen project, promoted in place)
 *   <destinationParent>/<slug>-variants/   (history dir — contains 4 unchosen)
 */
const createVariantsHistoryPath = (destinationParent, slug) => {
    return path_1.default.join(destinationParent, `${slug}${VARIANTS_HISTORY_SUFFIX}`);
};
exports.createVariantsHistoryPath = createVariantsHistoryPath;
const createVariantsHistoryManifestPath = (destinationParent, slug) => {
    return path_1.default.join((0, exports.createVariantsHistoryPath)(destinationParent, slug), MANIFEST_FILENAME);
};
exports.createVariantsHistoryManifestPath = createVariantsHistoryManifestPath;
const createProjectManifest = (input) => {
    return {
        schemaVersion: exports.CREATE_PROJECT_MANIFEST_SCHEMA_VERSION,
        ...input,
    };
};
exports.createProjectManifest = createProjectManifest;
/**
 * Write a manifest into the project-local artifact area.
 */
const writeCreateProjectManifestFile = (manifestPath, manifest) => {
    ensureManifestParentDirectory(manifestPath);
    fs_1.default.writeFileSync(`${manifestPath}`, `${JSON.stringify(manifest, null, 2)}\n`, 'utf8');
};
exports.writeCreateProjectManifestFile = writeCreateProjectManifestFile;
//# sourceMappingURL=createProjectArtifacts.js.map