import * as path from 'node:path';
import { prepareTaskSpecDirectory, cleanupPreparedTaskSpec } from '../attachments.js';
import { readTaskSpecFile } from '../taskSpecFile.js';
function hasAttachments(attachments) {
    return attachments !== undefined && attachments.length > 0;
}
function buildRetryTaskSpecContent(taskContent, retryNote) {
    return [
        taskContent.trimEnd(),
        '',
        '## 追加指示',
        '',
        retryNote,
    ].join('\n');
}
function resolveTaskDirPath(projectDir, taskDir) {
    return path.join(projectDir, taskDir);
}
function resolveRetryTaskSpecBaseContent(projectDir, taskContent, taskDir) {
    if (!taskDir) {
        return taskContent;
    }
    return readTaskSpecFile(path.join(resolveTaskDirPath(projectDir, taskDir), 'order.md'));
}
function getImageAttachmentExtension(fileName) {
    const ext = path.extname(fileName);
    if (ext === '') {
        throw new Error(`Task attachment file must have an extension: ${fileName}`);
    }
    return ext;
}
function resolveMaxImageIndex(content) {
    const matches = content.matchAll(/\[Image #(\d+)\]|attachments\/image-(\d+)\.[A-Za-z0-9]+/g);
    let maxIndex = 0;
    for (const match of matches) {
        const rawIndex = match[1] ?? match[2];
        if (rawIndex === undefined) {
            continue;
        }
        maxIndex = Math.max(maxIndex, Number(rawIndex));
    }
    return maxIndex;
}
function renumberRetryAttachments(baseContent, retryNote, attachments) {
    let nextImageIndex = resolveMaxImageIndex(baseContent) + 1;
    const placeholderReplacements = new Map();
    const adjustedAttachments = attachments.map((attachment) => {
        const placeholder = `[Image #${nextImageIndex}]`;
        const fileName = `image-${nextImageIndex}${getImageAttachmentExtension(attachment.fileName)}`;
        nextImageIndex += 1;
        placeholderReplacements.set(attachment.placeholder, placeholder);
        return {
            ...attachment,
            placeholder,
            fileName,
        };
    });
    const adjustedRetryNote = retryNote.replace(/\[Image #\d+\]/g, (placeholder) => placeholderReplacements.get(placeholder) ?? placeholder);
    return { retryNote: adjustedRetryNote, attachments: adjustedAttachments };
}
export function prepareRetryTaskSpecWithAttachments(projectDir, taskContent, retryNote, attachments, taskDir) {
    if (!hasAttachments(attachments)) {
        return undefined;
    }
    const taskSpecBaseContent = resolveRetryTaskSpecBaseContent(projectDir, taskContent, taskDir);
    const adjusted = renumberRetryAttachments(taskSpecBaseContent, retryNote, attachments);
    const taskSpecContent = buildRetryTaskSpecContent(taskSpecBaseContent, adjusted.retryNote);
    const prepareArgs = taskDir
        ? [
            projectDir,
            taskSpecContent,
            adjusted.attachments,
            { sourceTaskDir: resolveTaskDirPath(projectDir, taskDir) },
        ]
        : [projectDir, taskSpecContent, adjusted.attachments];
    const preparedSpec = prepareTaskSpecDirectory(...prepareArgs);
    return {
        taskDir: preparedSpec.taskDir,
        taskDirRelative: preparedSpec.taskDirRelative,
    };
}
export function cleanupPreparedRetryTaskSpec(preparedSpec) {
    if (preparedSpec) {
        cleanupPreparedTaskSpec(preparedSpec.taskDir);
    }
}
//# sourceMappingURL=retryTaskSpecAttachments.js.map