"use strict";
/**
 * Shared Routes — Symlink / copy status checker
 *
 * Reports whether shared resources (commands, skills, agents) under
 * ~/.ccs/shared/ are configured as symlinks pointing to ~/.claude/,
 * as directory copies (Windows fallback), or are missing.
 *
 * Return shape is a superset of the original { valid, message } so
 * existing callers remain unaffected while new callers can inspect
 * mode and per-link details.
 */
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.checkSymlinkStatus = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const claude_config_path_1 = require("../utils/claude-config-path");
const config_loader_facade_1 = require("../config/config-loader-facade");
const SHARED_ENTRY_TYPES = ['commands', 'skills', 'agents'];
function classifyEntry(linkPath, expectedTarget) {
    try {
        const lstats = fs.lstatSync(linkPath);
        if (lstats.isSymbolicLink()) {
            // Validate it points to the expected Claude config sub-directory.
            const target = fs.readlinkSync(linkPath);
            const resolvedTarget = path.resolve(path.dirname(linkPath), target);
            if (resolvedTarget === path.resolve(expectedTarget)) {
                return 'symlink';
            }
            // Symlink exists but points elsewhere — treat as missing for validity.
            return 'missing';
        }
        if (lstats.isDirectory()) {
            // Best-effort non-empty check to distinguish a real copy from an
            // accidental empty directory.
            try {
                const entries = fs.readdirSync(linkPath);
                if (entries.length > 0) {
                    return 'copy';
                }
            }
            catch {
                // readdirSync failed — fall through to missing
            }
            return 'missing';
        }
    }
    catch {
        // lstatSync threw — path does not exist or is unreadable
    }
    return 'missing';
}
function checkSymlinkStatus() {
    const ccsDir = (0, config_loader_facade_1.getCcsDir)();
    const sharedDir = path.join(ccsDir, 'shared');
    const claudeConfigDir = (0, claude_config_path_1.getClaudeConfigDir)();
    if (!fs.existsSync(sharedDir)) {
        return {
            valid: false,
            message: 'Shared directory not found',
            mode: 'none',
            links: { commands: 'missing', skills: 'missing', agents: 'missing' },
        };
    }
    const details = {
        commands: { mode: 'missing' },
        skills: { mode: 'missing' },
        agents: { mode: 'missing' },
    };
    for (const entryType of SHARED_ENTRY_TYPES) {
        const linkPath = path.join(sharedDir, entryType);
        const expectedTarget = path.join(claudeConfigDir, entryType);
        if (!fs.existsSync(linkPath)) {
            details[entryType] = { mode: 'missing' };
            continue;
        }
        details[entryType] = { mode: classifyEntry(linkPath, expectedTarget) };
    }
    const modes = SHARED_ENTRY_TYPES.map((t) => details[t].mode);
    const links = {
        commands: details.commands.mode,
        skills: details.skills.mode,
        agents: details.agents.mode,
    };
    const allSymlink = modes.every((m) => m === 'symlink');
    const allCopy = modes.every((m) => m === 'copy');
    const allMissing = modes.every((m) => m === 'missing');
    if (allSymlink) {
        return { valid: true, message: 'Symlinks active', mode: 'symlink', links };
    }
    if (allCopy) {
        return {
            valid: true,
            message: 'Copies active (Windows fallback)',
            mode: 'copy',
            links,
        };
    }
    if (allMissing) {
        return { valid: false, message: 'Shared resources not configured', mode: 'none', links };
    }
    // Mixed — at least one differs from the others.
    const details_str = SHARED_ENTRY_TYPES.map((t) => `${t}:${details[t].mode}`).join(', ');
    return {
        valid: false,
        message: `Mixed configuration: ${details_str}`,
        mode: 'mixed',
        links,
    };
}
exports.checkSymlinkStatus = checkSymlinkStatus;
//# sourceMappingURL=shared-routes-symlink-status.js.map