import * as fs from "fs/promises";
import * as path from "path";
import { loadConfig, getActiveSkillset, updateConfig } from "../../../cli/config.js";
import { getNoriSkillsetsDir } from "../../../norijson/skillset.js";
/**
 * Unlink a symlinked skillset from ~/.nori/profiles/.
 * Only removes symlinks — refuses to delete real directories.
 * @param args - Function arguments
 * @param args.name - Name of the skillset to unlink
 *
 * @returns Command result with success status and message
 */
export const unlinkSkillsetMain = async (args) => {
    const { name } = args;
    const skillsetsDir = getNoriSkillsetsDir();
    const linkPath = path.join(skillsetsDir, ...name.split("/"));
    // Verify the path exists
    let stat;
    try {
        stat = await fs.lstat(linkPath);
    }
    catch {
        return {
            success: false,
            cancelled: false,
            message: `Skillset "${name}" not found at: ${linkPath}`,
        };
    }
    // Only remove symlinks — refuse to delete real directories
    if (!stat.isSymbolicLink()) {
        return {
            success: false,
            cancelled: false,
            message: `"${name}" is not a linked skillset. Use a different command to remove installed skillsets.`,
        };
    }
    // Remove the symlink
    await fs.unlink(linkPath);
    // Clear active skillset if this was the active one
    try {
        const config = await loadConfig();
        if (config != null) {
            const activeSkillset = getActiveSkillset({ config });
            if (activeSkillset === name) {
                await updateConfig({ activeSkillset: null });
            }
        }
    }
    catch {
        // Config errors shouldn't block the unlink
    }
    return {
        success: true,
        cancelled: false,
        message: `Unlinked "${name}"`,
    };
};
//# sourceMappingURL=unlinkSkillset.js.map