let markit = async () => {
    const promise = import("markit-ai").then(({ Markit }) => {
        const instance = new Markit();
        markit = () => instance;
        return instance;
    });
    markit = () => promise;
    return promise;
};
function normalizeExtension(extension) {
    const trimmed = extension.trim().toLowerCase();
    return trimmed ? trimmed.startsWith(".") ? trimmed : `.${trimmed}` : ".bin";
}
function normalizeError(error) { return error instanceof Error && error.message.trim().length > 0 ? error.message.trim() : "Conversion failed"; }
function abortError() { const error = new Error("Aborted"); error.name = "AbortError"; return error; }
async function runMarkitConversion(task, signal) {
    if (signal?.aborted)
        throw abortError();
    const instance = await markit();
    if (!signal)
        return task(instance);
    return await new Promise((resolve, reject) => {
        const abort = () => reject(abortError());
        signal.addEventListener("abort", abort, { once: true });
        void task(instance).then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
    });
}
function finalizeConversion(markdown) {
    return typeof markdown === "string" && markdown.length > 0 ? { content: markdown, ok: true } : { content: "", ok: false, error: "Conversion produced no output" };
}
export async function convertFileWithMarkit(filePath, signal) {
    try {
        return finalizeConversion((await runMarkitConversion((instance) => instance.convertFile(filePath), signal)).markdown);
    }
    catch (error) {
        if (error instanceof Error && error.name === "AbortError")
            throw error;
        return { content: "", ok: false, error: normalizeError(error) };
    }
}
export async function convertBufferWithMarkit(buffer, extension, signal) {
    const normalizedExtension = normalizeExtension(extension);
    const streamInfo = { extension: normalizedExtension, filename: `input${normalizedExtension}` };
    try {
        return finalizeConversion((await runMarkitConversion((instance) => instance.convert(Buffer.from(buffer), streamInfo), signal)).markdown);
    }
    catch (error) {
        if (error instanceof Error && error.name === "AbortError")
            throw error;
        return { content: "", ok: false, error: normalizeError(error) };
    }
}
//# sourceMappingURL=markit.js.map