export class ArrayRecombiner {
    static standard(opt) {
        return async (results, originalItems) => {
            const iterator = await results();
            const itemMap = new Map();
            const items = [];
            for await (const result of iterator) {
                const index = result.metadata().index;
                const text = result.text();
                if (opt.unique && itemMap.has(text)) {
                    continue;
                }
                if (opt.limit && items.length >= opt.limit) {
                    break;
                }
                itemMap.set(text, index);
                items.push({
                    item: originalItems[index],
                    index,
                });
            }
            const sorted = opt.sort && opt.sort === 'index' ? items.sort((a, b) => a.index - b.index) : items;
            return sorted.map(x => x.item);
        };
    }
}
