export function createSlideOutputState(initialSlides) {
    const slidesByIndex = new Map();
    const pending = new Map();
    let order = [];
    let slidesDir = initialSlides?.slidesDir ?? "";
    let sourceUrl = initialSlides?.sourceUrl ?? "";
    let done = false;
    const updateSlideEntry = (slide) => {
        const existing = slidesByIndex.get(slide.index);
        const next = {
            index: slide.index,
            timestamp: Number.isFinite(slide.timestamp) && slide.timestamp >= 0
                ? slide.timestamp
                : (existing?.timestamp ?? 0),
            imagePath: slide.imagePath ? slide.imagePath : (existing?.imagePath ?? null),
        };
        slidesByIndex.set(slide.index, next);
        if (slide.imagePath) {
            const waiters = pending.get(slide.index);
            if (waiters && waiters.length > 0) {
                pending.delete(slide.index);
                for (const resolve of waiters) {
                    resolve(next);
                }
            }
        }
    };
    const setMeta = (meta) => {
        if (meta.slidesDir)
            slidesDir = meta.slidesDir;
        if (meta.sourceUrl)
            sourceUrl = meta.sourceUrl;
    };
    const updateFromSlides = (slides) => {
        slidesDir = slides.slidesDir;
        sourceUrl = slides.sourceUrl;
        const ordered = slides.slides
            .filter((slide) => Number.isFinite(slide.timestamp))
            .map((slide) => ({ index: slide.index, timestamp: slide.timestamp }))
            .sort((a, b) => a.timestamp - b.timestamp);
        order = ordered.map((slide) => slide.index);
        for (const slide of slides.slides) {
            updateSlideEntry(slide);
        }
    };
    if (initialSlides)
        updateFromSlides(initialSlides);
    const markDone = () => {
        if (done)
            return;
        done = true;
        for (const [index, waiters] of pending.entries()) {
            const entry = slidesByIndex.get(index) ?? null;
            for (const resolve of waiters) {
                resolve(entry);
            }
        }
        pending.clear();
    };
    const waitForSlide = (index) => {
        const existing = slidesByIndex.get(index);
        if (existing?.imagePath)
            return Promise.resolve(existing);
        if (done)
            return Promise.resolve(existing ?? null);
        return new Promise((resolve) => {
            const list = pending.get(index) ?? [];
            list.push(resolve);
            pending.set(index, list);
        });
    };
    return {
        setMeta,
        updateFromSlides,
        updateSlideEntry,
        waitForSlide,
        markDone,
        getSlides: () => order.map((index) => slidesByIndex.get(index)).filter(Boolean),
        getSlide: (index) => slidesByIndex.get(index) ?? null,
        getOrder: () => order.slice(),
        getSlidesDir: () => slidesDir,
        getSourceUrl: () => sourceUrl,
        isDone: () => done,
    };
}
//# sourceMappingURL=slides-output-state.js.map