"use strict";
/**
 * Shared Routes — Item content readers
 *
 * Resolves the full markdown content for a given shared item path,
 * routing by collection type and enforcing allowed-root checks.
 */
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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSharedItemContent = exports.getSharedSettingsContent = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const shared_routes_path_guards_1 = require("./shared-routes-path-guards");
const shared_routes_markdown_1 = require("./shared-routes-markdown");
const shared_routes_plugins_1 = require("./shared-routes-plugins");
const shared_routes_plugin_registry_content_1 = require("./shared-routes-plugin-registry-content");
// ---------------------------------------------------------------------------
// Settings
// ---------------------------------------------------------------------------
function getSharedSettingsContent(itemPath, allowedRoots) {
    if (path.basename(itemPath) !== 'settings.json') {
        return null;
    }
    const sharedRoot = Array.from(allowedRoots)[0];
    if (!sharedRoot) {
        return null;
    }
    const settingsPath = path.join(sharedRoot, 'settings.json');
    const resolvedSettingsPath = (0, shared_routes_path_guards_1.safeRealPath)(settingsPath);
    if (!resolvedSettingsPath || !(0, shared_routes_path_guards_1.isPathWithinAny)(resolvedSettingsPath, allowedRoots)) {
        return null;
    }
    let stats;
    try {
        stats = fs.statSync(resolvedSettingsPath);
    }
    catch {
        return null;
    }
    if (!stats.isFile() || stats.size > shared_routes_markdown_1.MAX_CONTENT_FILE_BYTES) {
        return null;
    }
    try {
        const rawContent = fs.readFileSync(resolvedSettingsPath, 'utf8');
        const parsed = JSON.parse(rawContent);
        return {
            content: JSON.stringify(parsed, null, 2),
            contentPath: resolvedSettingsPath,
        };
    }
    catch {
        const content = (0, shared_routes_markdown_1.readMarkdownContent)(resolvedSettingsPath, allowedRoots);
        return content ? { content, contentPath: resolvedSettingsPath } : null;
    }
}
exports.getSharedSettingsContent = getSharedSettingsContent;
// ---------------------------------------------------------------------------
// Generic item content
// ---------------------------------------------------------------------------
function getSharedItemContent(type, itemPath, allowedRoots, sharedDir) {
    if (type === 'settings') {
        return getSharedSettingsContent(itemPath, allowedRoots);
    }
    if (type === 'plugins') {
        const pluginRegistryContent = (0, shared_routes_plugin_registry_content_1.getPluginRegistryContent)(itemPath, sharedDir, allowedRoots);
        if (pluginRegistryContent) {
            return pluginRegistryContent;
        }
    }
    const resolvedItemPath = (0, shared_routes_path_guards_1.safeRealPath)(itemPath);
    if (!resolvedItemPath || !(0, shared_routes_path_guards_1.isPathWithinAny)(resolvedItemPath, allowedRoots)) {
        return null;
    }
    let itemStats;
    try {
        itemStats = fs.statSync(resolvedItemPath);
    }
    catch {
        return null;
    }
    let markdownPath = null;
    if (type === 'commands') {
        if (!itemStats.isFile() || !itemPath.toLowerCase().endsWith('.md')) {
            return null;
        }
        markdownPath = resolvedItemPath;
    }
    else if (type === 'skills') {
        if (!itemStats.isDirectory()) {
            return null;
        }
        markdownPath = (0, shared_routes_markdown_1.resolveReadableMarkdownPath)([path.join(resolvedItemPath, 'SKILL.md')], allowedRoots);
    }
    else if (type === 'plugins') {
        if (!itemStats.isDirectory() || (0, shared_routes_plugins_1.isPluginInfrastructurePath)(resolvedItemPath, sharedDir)) {
            return null;
        }
        markdownPath = (0, shared_routes_markdown_1.resolveReadableMarkdownPath)([
            path.join(resolvedItemPath, 'README.md'),
            path.join(resolvedItemPath, 'readme.md'),
            path.join(resolvedItemPath, 'PLUGIN.md'),
        ], allowedRoots);
        if (!markdownPath) {
            return null;
        }
    }
    else {
        // agents
        if (itemStats.isDirectory()) {
            markdownPath = (0, shared_routes_markdown_1.resolveReadableMarkdownPath)([
                path.join(resolvedItemPath, 'prompt.md'),
                path.join(resolvedItemPath, 'AGENT.md'),
                path.join(resolvedItemPath, 'agent.md'),
            ], allowedRoots);
        }
        else if (itemStats.isFile() && itemPath.toLowerCase().endsWith('.md')) {
            markdownPath = resolvedItemPath;
        }
    }
    if (!markdownPath) {
        return null;
    }
    const content = (0, shared_routes_markdown_1.readMarkdownContent)(markdownPath, allowedRoots);
    if (!content) {
        return null;
    }
    return { content, contentPath: markdownPath };
}
exports.getSharedItemContent = getSharedItemContent;
//# sourceMappingURL=shared-routes-content.js.map