"use strict";
/**
 * Shared Data Routes (Phase 07)
 *
 * Thin Express router — delegates all logic to focused sub-modules.
 * API routes for commands, skills, agents, and plugins from ~/.ccs/shared/
 */
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.sharedRoutes = void 0;
const express_1 = require("express");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const config_loader_facade_1 = require("../config/config-loader-facade");
const auth_middleware_1 = require("./middleware/auth-middleware");
const shared_routes_types_1 = require("./shared-routes-types");
const shared_routes_path_guards_1 = require("./shared-routes-path-guards");
const shared_routes_collections_1 = require("./shared-routes-collections");
const shared_routes_content_1 = require("./shared-routes-content");
const shared_routes_symlink_status_1 = require("./shared-routes-symlink-status");
/** Strips extended fields so the summary endpoint stays wire-compatible. */
function symlinkStatusForSummary() {
    const { valid, message } = (0, shared_routes_symlink_status_1.checkSymlinkStatus)();
    return { valid, message };
}
exports.sharedRoutes = (0, express_1.Router)();
exports.sharedRoutes.use((req, res, next) => {
    if ((0, auth_middleware_1.requireLocalAccessWhenAuthDisabled)(req, res, 'Shared-content endpoints require localhost access when dashboard auth is disabled.')) {
        next();
    }
});
/**
 * GET /api/shared/commands
 */
exports.sharedRoutes.get('/commands', (_req, res) => {
    const items = (0, shared_routes_collections_1.getSharedItems)('commands');
    res.json({ items });
});
/**
 * GET /api/shared/skills
 */
exports.sharedRoutes.get('/skills', (_req, res) => {
    const items = (0, shared_routes_collections_1.getSharedItems)('skills');
    res.json({ items });
});
/**
 * GET /api/shared/agents
 */
exports.sharedRoutes.get('/agents', (_req, res) => {
    const items = (0, shared_routes_collections_1.getSharedItems)('agents');
    res.json({ items });
});
/**
 * GET /api/shared/plugins
 */
exports.sharedRoutes.get('/plugins', (_req, res) => {
    const items = (0, shared_routes_collections_1.getSharedItems)('plugins');
    res.json({ items });
});
/**
 * GET /api/shared/content?type=commands|skills|agents|plugins|settings&path=<item-path>
 */
exports.sharedRoutes.get('/content', (req, res) => {
    const typeParam = req.query.type;
    const itemPathParam = req.query.path;
    if (!(0, shared_routes_types_1.isSharedContentType)(typeParam)) {
        res.status(400).json({ error: 'Invalid or missing type parameter' });
        return;
    }
    if (typeof itemPathParam !== 'string' || itemPathParam.trim().length === 0) {
        res.status(400).json({ error: 'Invalid or missing path parameter' });
        return;
    }
    const ccsDir = (0, config_loader_facade_1.getCcsDir)();
    const sharedDir = typeParam === 'settings' ? path.join(ccsDir, 'shared') : path.join(ccsDir, 'shared', typeParam);
    if (!fs.existsSync(sharedDir)) {
        res.status(404).json({ error: 'Shared directory not found' });
        return;
    }
    const sharedDirRoot = (0, shared_routes_path_guards_1.safeRealPath)(sharedDir) ?? path.resolve(sharedDir);
    const allowedRoots = typeParam === 'settings'
        ? (0, shared_routes_path_guards_1.resolveSettingsAllowedRoots)(ccsDir, sharedDirRoot)
        : (0, shared_routes_path_guards_1.resolveAllowedRoots)(typeParam, ccsDir, sharedDirRoot);
    const contentResult = (0, shared_routes_content_1.getSharedItemContent)(typeParam, itemPathParam, allowedRoots, sharedDirRoot);
    if (!contentResult) {
        res.status(404).json({ error: 'Shared content not found' });
        return;
    }
    res.json(contentResult);
});
/**
 * GET /api/shared/summary
 */
exports.sharedRoutes.get('/summary', (_req, res) => {
    const commands = (0, shared_routes_collections_1.getSharedItems)('commands').length;
    const skills = (0, shared_routes_collections_1.getSharedItems)('skills').length;
    const agents = (0, shared_routes_collections_1.getSharedItems)('agents').length;
    const plugins = (0, shared_routes_collections_1.getSharedItems)('plugins').length;
    const ccsDir = (0, config_loader_facade_1.getCcsDir)();
    const settingsPath = path.join(ccsDir, 'shared', 'settings.json');
    const sharedDirRoot = (0, shared_routes_path_guards_1.safeRealPath)(path.join(ccsDir, 'shared'));
    const resolvedSettingsPath = (0, shared_routes_path_guards_1.safeRealPath)(settingsPath);
    const settingsAllowedRoots = sharedDirRoot
        ? (0, shared_routes_path_guards_1.resolveSettingsAllowedRoots)(ccsDir, sharedDirRoot)
        : new Set();
    const hasSettings = Boolean(sharedDirRoot) &&
        Boolean(resolvedSettingsPath) &&
        (0, shared_routes_path_guards_1.isPathWithinAny)(resolvedSettingsPath, settingsAllowedRoots);
    res.json({
        commands,
        skills,
        agents,
        plugins,
        settings: hasSettings,
        total: commands + skills + agents + plugins + (hasSettings ? 1 : 0),
        symlinkStatus: symlinkStatusForSummary(),
    });
});
//# sourceMappingURL=shared-routes.js.map