/**
 * Fork skillset command
 *
 * Copies an existing skillset to a new name under ~/.nori/profiles/.
 */
import * as fs from "fs/promises";
import * as path from "path";
import { log, note } from "@clack/prompts";
import { bold } from "../../../cli/logger.js";
import { ensureNoriJson, readSkillsetMetadata, writeSkillsetMetadata, } from "../../../norijson/nori.js";
import { MANIFEST_FILE, getNoriSkillsetsDir } from "../../../norijson/skillset.js";
export const forkSkillsetMain = async (args) => {
    const { baseSkillset, newSkillset } = args;
    const skillsetsDir = getNoriSkillsetsDir();
    const sourcePath = path.join(skillsetsDir, baseSkillset);
    const destPath = path.join(skillsetsDir, newSkillset);
    // Validate source exists and is a valid skillset (has nori.json)
    await ensureNoriJson({ skillsetDir: sourcePath });
    try {
        await fs.access(path.join(sourcePath, MANIFEST_FILE));
    }
    catch {
        log.error(`Skillset '${baseSkillset}' not found. Run 'nori-skillsets list' to see available skillsets.`);
        return {
            success: false,
            cancelled: false,
            message: `Skillset "${baseSkillset}" not found`,
        };
    }
    // Validate destination does not already exist
    try {
        await fs.access(destPath);
        log.error(`Skillset '${newSkillset}' already exists. Choose a different name.`);
        return {
            success: false,
            cancelled: false,
            message: `Skillset "${newSkillset}" already exists`,
        };
    }
    catch {
        // Expected — destination should not exist
    }
    // Create parent directory if needed (for namespaced profiles like org/name)
    const parentDir = path.dirname(destPath);
    await fs.mkdir(parentDir, { recursive: true });
    // Copy the skillset
    await fs.cp(sourcePath, destPath, { recursive: true });
    // Update the name in nori.json to match the new skillset name
    const metadata = await readSkillsetMetadata({ skillsetDir: destPath });
    metadata.name = newSkillset;
    await writeSkillsetMetadata({ skillsetDir: destPath, metadata });
    const nextSteps = [
        `To switch:  nori-skillsets switch ${newSkillset}`,
        `To edit:    ~/.nori/profiles/${newSkillset}/`,
    ].join("\n");
    note(nextSteps, "Next Steps");
    return {
        success: true,
        cancelled: false,
        message: `Forked "${bold({ text: baseSkillset })}" to "${bold({ text: newSkillset })}"`,
    };
};
//# sourceMappingURL=forkSkillset.js.map