import Resolver from"./resolver.js";import{create_object,concat,sort_by_length_up,get_max_len}from"./common.js";import{SearchResults,IntermediateSearchResults}from"./type.js";export function intersect(a,b,c,d,e,f,g){const h=a.length;let i,j,k=[];i=create_object();for(let l,m,n,o,p=0;p<b;p++)for(let b=0;b<h;b++)if(n=a[b],p<n.length&&(l=n[p]))for(let a=0;a<l.length;a++){if(m=l[a],(j=i[m])?i[m]++:(j=0,i[m]=1),o=k[j]||(k[j]=[]),!g){let a=p+(b||!e?0:f||0);o=o[a]||(o[a]=[])}if(o.push(m),g&&c&&j===h-1&&o.length-d===c)return d?o.slice(d):o}const l=k.length;if(l)if(!e){if(l<h)return[];if(k=k[l-1],c||d)if(g)(k.length>c||d)&&(k=k.slice(d,c+d));else{const a=[];for(let b,e=0;e<k.length;e++)if(b=k[e],b){if(d&&b.length>d){d-=b.length;continue}if((c&&b.length>c||d)&&(b=b.slice(d,c+d),c-=b.length,d&&(d-=b.length)),a.push(b),!c)break}k=a}}else k=1<k.length?union(k,c,d,g,f):(k=k[0])&&c&&k.length>c||d?k.slice(d,c+d):k;return k}export function union(a,b,c,d,e){const f=[],g=create_object();let h,l,m,n=a.length;if(!d)for(let d,j=n-1,i=0;0<=j;j--){d=a[j];for(let a=0;a<d.length;a++)if(h=d[a],m=h&&h.length,m)for(let d=0;d<m;d++)if(l=h[d],!g[l])if(g[l]=1,c)c--;else{let c=0|(a+(j<n-1?e||0:0))/(j+1),d=f[c]||(f[c]=[]);if(d.push(l),++i===b)return f}}else for(let d=n-1;0<=d;d--)if(h=a[d],m=h&&h.length,m)for(let a=0;a<m;a++)if(l=h[a],!g[l])if(g[l]=1,c)c--;else if(f.push(l),f.length===b)return f;return f}export function intersect_union(a,b,c){const d=create_object(),e=[];for(let e,f=0;f<b.length;f++){e=b[f];for(let a=0;a<e.length;a++)d[e[a]]=1}if(c)for(let b,c=0;c<a.length;c++)b=a[c],d[b]&&(e.push(b),d[b]=0);else for(let b,c,f=0;f<a.result.length;f++){b=a.result[f];for(let a=0;a<b.length;a++)if(c=b[a],d[c]){const a=e[f]||(e[f]=[]);a.push(c),d[c]=0}}return e}