/** Interactive-shell workflow for importing and exposing ported skills to runtime sessions. */
import { discoverExternalSkillRoots, getExternalSkillProviderLabel, syncExternalSkillRoots } from '../../app/resources/external-skill-roots.js';
import { CONFIG_DIR_NAME, getAgentDir } from '../../config.js';
import { Spacer, Text } from '../../tui/index.js';
import { PortSkillsSelectorComponent } from '../components/selectors/index.js';
import { theme } from '../theme/runtime.js';
export class PortSkillsWorkflow {
    deps;
    constructor(deps) {
        this.deps = deps;
    }
    async handlePortSkillsCommand() {
        const agentDir = this.deps.agentDir ?? getAgentDir();
        const discoveries = discoverExternalSkillRoots({
            cwd: this.deps.cwd,
            agentDir,
            settingsManager: this.deps.settingsManager
        });
        if (discoveries.length === 0) {
            this.deps.showWarning('No external skill roots found. Expected .claude/skills, .codex/skills, or .agents/skills in the project tree or home directory.');
            return;
        }
        const items = discoveries.map((entry) => ({
            id: entry.rootPath,
            providerLabel: getExternalSkillProviderLabel(entry.provider),
            scope: entry.scope,
            settingsPath: entry.settingsPath,
            validSkillCount: entry.validSkillCount,
            brokenSkillCount: entry.brokenSkillCount,
            warningCount: entry.warnings.length,
            active: entry.alreadyRegistered
        }));
        this.deps.showSelector((done) => {
            const selector = new PortSkillsSelectorComponent(items, () => {
                done();
                this.deps.requestRender();
            }, () => void this.deps.shutdown(), (draftItems) => {
                void this.applySelection(draftItems, discoveries, agentDir, done);
            }, () => this.deps.requestRender());
            return { component: selector, focus: selector.getList() };
        });
    }
    async applySelection(draftItems, discoveries, agentDir, done) {
        const activeRootPaths = draftItems.filter((item) => item.active).map((item) => item.id);
        const result = syncExternalSkillRoots({
            discoveries,
            activeRootPaths,
            cwd: this.deps.cwd,
            agentDir,
            settingsManager: this.deps.settingsManager
        });
        done();
        const changed = result.activated.length > 0 || result.deactivated.length > 0;
        if (changed) {
            this.deps.showStatus('Updating external skill sources...');
            await this.deps.reloadSkills();
        }
        this.renderSummary('External Skill Porting', this.buildSummaryLines(result));
    }
    buildSummaryLines(result) {
        const lines = [];
        if (result.activated.length > 0) {
            lines.push(`  ${theme.fg('success', 'Activated:')}`);
            for (const entry of result.activated) {
                lines.push(`    ${theme.fg('accent', `${getExternalSkillProviderLabel(entry.provider)} (${entry.scope})`)} ${entry.settingsPath} (${entry.validSkillCount} skill${entry.validSkillCount === 1 ? '' : 's'})`);
            }
        }
        if (result.deactivated.length > 0) {
            lines.push(`  ${theme.fg('warning', 'Deactivated:')}`);
            for (const entry of result.deactivated) {
                lines.push(`    ${theme.fg('warning', `${getExternalSkillProviderLabel(entry.provider)} (${entry.scope})`)} ${entry.settingsPath}`);
            }
        }
        if (result.skippedNoValidSkills.length > 0) {
            lines.push(`  ${theme.fg('warning', 'Skipped (no valid skills):')}`);
            for (const entry of result.skippedNoValidSkills) {
                lines.push(`    ${theme.fg('warning', `${getExternalSkillProviderLabel(entry.provider)} (${entry.scope})`)} ${entry.settingsPath}`);
            }
        }
        if (result.activated.length === 0 && result.deactivated.length === 0) {
            lines.push(`  ${theme.fg('dim', 'No source changes.')}`);
        }
        lines.push(`  ${theme.fg('dim', `Tip: use /skills to control individual imported skills after registration. Project roots are saved to ./${CONFIG_DIR_NAME}/settings.json.`)}`);
        return lines;
    }
    renderSummary(title, lines) {
        this.deps.chatContainer.addChild(new Spacer(1));
        this.deps.chatContainer.addChild(new Text(theme.bold(theme.fg('accent', title)), 1, 0));
        this.deps.chatContainer.addChild(new Text(lines.join('\n'), 0, 0));
        this.deps.chatContainer.addChild(new Spacer(1));
        this.deps.requestRender();
    }
}
//# sourceMappingURL=port-skills-workflow.js.map