import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * /schedule — read-only view of cron subscriptions declared across all
 * loaded skills.
 *
 * The legacy `.nanocoder/schedules.json`-backed scheduler was replaced by
 * skill subscriptions. Mutations now happen by editing a command's
 * frontmatter or a bundle's `skill.yaml`. The daemon owns execution.
 */
import { Box, Text } from 'ink';
import React from 'react';
import { TitledBoxWithPreferences } from '../components/ui/titled-box.js';
import { useTerminalWidth } from '../hooks/useTerminalWidth.js';
import { useTheme } from '../hooks/useTheme.js';
import { formatCronHuman } from '../schedule/cron.js';
import { generateKey } from '../session/key-generator.js';
import { getLoadedSkills } from '../skills/skill-registry.js';
import { infoMsg } from '../utils/message-factory.js';
function collectCronSubscriptions() {
    const out = [];
    for (const skill of getLoadedSkills()) {
        for (const trig of skill.subscribe ?? []) {
            if (trig.kind !== 'schedule.cron')
                continue;
            out.push({
                skill: skill.name,
                target: trig.target ?? '(self)',
                cron: trig.cron,
                human: formatCronHuman(trig.cron),
                confirm: trig.confirm,
            });
        }
    }
    return out;
}
function ScheduleView({ entries }) {
    const boxWidth = useTerminalWidth();
    const { colors } = useTheme();
    if (entries.length === 0) {
        return (_jsx(TitledBoxWithPreferences, { title: "Schedules", width: boxWidth, borderColor: colors.primary, paddingX: 2, paddingY: 1, flexDirection: "column", children: _jsx(Text, { color: colors.secondary, children: "No cron subscriptions declared. Add a `schedule.cron` entry to a command's frontmatter or a bundle's `skill.yaml` and run `nanocoder daemon start` to make it fire." }) }));
    }
    return (_jsxs(TitledBoxWithPreferences, { title: `Schedules (${entries.length})`, width: boxWidth, borderColor: colors.primary, paddingX: 2, paddingY: 1, flexDirection: "column", children: [_jsx(Text, { color: colors.secondary, children: "Cron subscriptions only fire while the daemon is running (`nanocoder daemon start`)." }), entries.map((e, i) => (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsxs(Text, { bold: true, children: [e.cron, " \u2192 ", e.target, e.confirm ? ' [confirm: plan mode]' : ''] }), _jsxs(Text, { color: colors.secondary, children: [e.human, " \u2014 declared in skill \"", e.skill, "\""] })] }, `${e.skill}-${i}`)))] }));
}
export const scheduleCommand = {
    name: 'schedule',
    description: 'List cron-triggered skills (single-file frontmatter + bundle skill.yaml). Read-only - edit the source file to change.',
    handler: async (args) => {
        if (args.length > 0) {
            return infoMsg("Usage: /schedule\n\nMutations happen by editing a command's frontmatter `subscribe:` block, or a bundle's `skill.yaml`. See docs.", 'schedule');
        }
        return React.createElement(ScheduleView, {
            key: generateKey('schedule'),
            entries: collectCronSubscriptions(),
        });
    },
};
//# sourceMappingURL=schedule.js.map