import { log } from '@hlink/core';
import schedule from 'node-schedule';
import { ToadScheduler, SimpleIntervalJob, Task } from 'toad-scheduler';
import TaskSDK from './TaskSDK.js';
const schedules = {};
function createSchedule(options, runTask) {
    cancelSchedule(options.name);
    let cancel;
    if (options.scheduleType === 'cron') {
        const cronJob = schedule.scheduleJob(options.scheduleValue, function () {
            runTask();
        });
        cancel = () => cronJob.cancel();
    }
    else if (options.scheduleType === 'loop') {
        const scheduler = new ToadScheduler();
        const task = new Task(options.name, async () => {
            runTask();
        });
        const job = new SimpleIntervalJob({ seconds: Number(options.scheduleValue), runImmediately: true }, task, options.name);
        scheduler.addSimpleIntervalJob(job);
        cancel = () => {
            scheduler.stopById(options.name);
            scheduler.removeById(options.name);
            return true;
        };
    }
    else {
        throw new Error('未知计划任务类型!');
    }
    schedules[options.name] = cancel;
    return true;
}
export function cancelSchedule(name) {
    const shouldCancel = schedules[name];
    if (shouldCancel) {
        shouldCancel();
        schedules[name] = null;
    }
    log.success(`已取消计划任务 ${name}`);
    return true;
}
export function renameSchedule(preName, name) {
    if (schedules[preName]) {
        schedules[name] = schedules[preName];
        schedules[preName] = null;
    }
    return true;
}
export function cancelAllSchedule() {
    Object.keys(schedules).forEach((name) => {
        TaskSDK.cancelSchedule(name);
    });
}
export function hasSchedule(name) {
    return !!schedules[name];
}
export default createSchedule;
