"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.DesignMdMode = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class DesignMdMode {
    workspaceId;
    statePath;
    designPath;
    profilesDir;
    constructor(workspaceId = 'default') {
        this.workspaceId = workspaceId;
        const base = path.join(process.cwd(), '.artifacts', 'workspaces', workspaceId, 'designmd');
        fs.mkdirSync(base, { recursive: true });
        this.statePath = path.join(base, 'state.json');
        this.designPath = path.join(process.cwd(), 'DESIGN.md');
        this.profilesDir = path.join(process.cwd(), 'design', 'profiles');
    }
    applyProfile(profile, content, options) {
        if (fs.existsSync(this.designPath) && !options?.force) {
            const existing = fs.readFileSync(this.designPath, 'utf-8');
            if (existing.trim().length > 0) {
                throw new Error('DESIGN.md já existe. Use force para sobrescrever explicitamente.');
            }
        }
        fs.writeFileSync(this.designPath, content, 'utf-8');
        fs.writeFileSync(this.statePath, JSON.stringify(profile, null, 2), 'utf-8');
        return profile;
    }
    applyByProfileId(profileId, options) {
        const dir = path.join(this.profilesDir, profileId);
        const metaPath = path.join(dir, 'meta.json');
        const designPath = path.join(dir, 'DESIGN.md');
        if (!fs.existsSync(metaPath) || !fs.existsSync(designPath)) {
            throw new Error(`Perfil designmd não encontrado: ${profileId}`);
        }
        const meta = JSON.parse(fs.readFileSync(metaPath, 'utf-8'));
        const content = fs.readFileSync(designPath, 'utf-8');
        return this.applyProfile({ id: meta.id, source: meta.source, title: meta.title, appliedAt: new Date().toISOString(), checksum: meta.checksum }, content, options);
    }
    status() {
        if (!fs.existsSync(this.statePath))
            return null;
        return JSON.parse(fs.readFileSync(this.statePath, 'utf-8'));
    }
    listProfiles() {
        if (!fs.existsSync(this.profilesDir))
            return [];
        return fs.readdirSync(this.profilesDir, { withFileTypes: true })
            .filter((d) => d.isDirectory())
            .map((d) => {
            const metaPath = path.join(this.profilesDir, d.name, 'meta.json');
            if (!fs.existsSync(metaPath))
                return { id: d.name };
            const meta = JSON.parse(fs.readFileSync(metaPath, 'utf-8'));
            return { id: meta.id || d.name, source: meta.source, title: meta.title, checksum: meta.checksum };
        });
    }
}
exports.DesignMdMode = DesignMdMode;
