"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.getArchitectureConfigPath = getArchitectureConfigPath;
exports.getArchpilotInitializationState = getArchpilotInitializationState;
exports.renderInitializationBlockedMessage = renderInitializationBlockedMessage;
const path = __importStar(require("node:path"));
const node_fs_1 = require("node:fs");
function getArchitectureConfigPath(workspaceRoot) {
    return path.join(workspaceRoot, '.archpilot', 'architecture.json');
}
async function getArchpilotInitializationState(workspaceRoot) {
    const configPath = getArchitectureConfigPath(workspaceRoot);
    let content;
    try {
        content = await node_fs_1.promises.readFile(configPath, { encoding: 'utf8' });
    }
    catch (error) {
        if (error.code === 'ENOENT') {
            return { kind: 'not_initialized', configPath };
        }
        return {
            kind: 'invalid',
            configPath,
            details: error instanceof Error
                ? error.message
                : 'Unable to read .archpilot/architecture.json.',
        };
    }
    let parsed;
    try {
        parsed = JSON.parse(content);
    }
    catch {
        return {
            kind: 'invalid',
            configPath,
            details: '.archpilot/architecture.json is invalid JSON.',
        };
    }
    if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
        return {
            kind: 'invalid',
            configPath,
            details: '.archpilot/architecture.json must be a JSON object.',
        };
    }
    const projectName = parsed.projectName;
    if (typeof projectName !== 'string' || projectName.trim().length === 0) {
        return {
            kind: 'invalid',
            configPath,
            details: '.archpilot/architecture.json is missing required "projectName".',
        };
    }
    return { kind: 'initialized', configPath };
}
function renderInitializationBlockedMessage(state) {
    if (state.kind === 'not_initialized') {
        return 'ArchPilot is not initialized for this repository. Open the Architecture Inspector and select Initialize Architecture.';
    }
    if (state.kind === 'invalid') {
        return `ArchPilot initialization is blocked because .archpilot/architecture.json is malformed. ${state.details} Fix the file, then initialize from the Architecture Inspector.`;
    }
    return 'ArchPilot is already initialized.';
}
//# sourceMappingURL=initializationState.js.map