"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.tasksFilePath = tasksFilePath;
exports.readTasksFile = readTasksFile;
exports.writeTasksFile = writeTasksFile;
exports.processLocalTasks = processLocalTasks;
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
const cron_1 = require("./cron");
const logger_1 = require("./logger");
const TASKS_FILENAME = 'aidev.tasks.json';
function tasksFilePath(dir = process.cwd()) {
    return path.join(dir, TASKS_FILENAME);
}
function readTasksFile(dir) {
    const p = tasksFilePath(dir);
    if (!fs.existsSync(p))
        return [];
    try {
        const raw = JSON.parse(fs.readFileSync(p, 'utf8'));
        if (!Array.isArray(raw))
            return [];
        return raw;
    }
    catch {
        return [];
    }
}
function writeTasksFile(tasks, dir) {
    fs.writeFileSync(tasksFilePath(dir), JSON.stringify(tasks, null, 2) + '\n', 'utf8');
}
function resolveTag(task, config) {
    if (task.type === 'code')
        return config.clickupTag;
    return config.nonCodeTag || config.clickupTag;
}
function resolveProvider(task, codeProvider, nonCodeProvider) {
    if (task.type === 'non-code' && nonCodeProvider)
        return nonCodeProvider;
    return codeProvider;
}
async function processLocalTasks(config, provider, nonCodeProvider) {
    const tasks = readTasksFile();
    if (tasks.length === 0)
        return { pushed: 0, skipped: 0 };
    let pushed = 0;
    let skipped = 0;
    let modified = false;
    const toRemove = new Set();
    for (const task of tasks) {
        if (task.cron) {
            if (!(0, cron_1.shouldCronFire)(task.cron, task.lastPushedAt)) {
                skipped++;
                continue;
            }
        }
        const tag = resolveTag(task, config);
        const targetProvider = resolveProvider(task, provider, nonCodeProvider);
        const tags = [tag, ...(task.tags ?? [])];
        try {
            const result = await targetProvider.createTask({
                title: task.title,
                description: task.description,
                tags,
                priority: task.priority,
                dueDate: task.dueDate ? new Date(task.dueDate).getTime() : undefined,
                listId: task.listId,
            });
            logger_1.logger.success(`Pushed task "${task.title}" → ${result.url}`);
            pushed++;
            if (task.cron) {
                task.lastPushedAt = Date.now();
                modified = true;
            }
            else {
                toRemove.add(task.id);
                modified = true;
            }
        }
        catch (err) {
            logger_1.logger.error(`Failed to push task "${task.title}": ${err}`);
            skipped++;
        }
    }
    if (modified) {
        const remaining = tasks.filter((t) => !toRemove.has(t.id));
        writeTasksFile(remaining);
    }
    return { pushed, skipped };
}
//# sourceMappingURL=tasks.js.map