import{parse_simple}from"../common.js";import Index from"../index.js";import WorkerIndex from"../worker.js";import{EnrichedDocumentSearchResults,EnrichedSearchResults,HighlightOptions}from"../type.js";export function highlight_fields(a,b,c,d,e){let f,g,h;"string"==typeof e?(f=e,e=""):f=e.template;g=f.indexOf("$1");h=f.substring(g+2),g=f.substring(0,g);let i,l=e&&e.boundary,m=!e||!1!==e.clip,n=e&&e.merge&&h&&g&&new RegExp(h+" "+g,"g"),o=e&&e.ellipsis,p=0;"object"==typeof o&&(i=o.template,p=i.length-2,o=o.pattern),"string"!=typeof o&&(o=!1===o?"":"..."),p&&(o=i.replace("$1",o));let q,r,s=o.length-p;"object"==typeof l&&(q=l.before,0===q&&(q=-1),r=l.after,0===r&&(r=-1),l=l.total||9e5);let t,u=new Map;for(let j,k,p,v=0;v<b.length;v++){let e;if(d)e=b,p=d;else{const a=b[v];if(p=a.field,!p)continue;e=a.result}k=c.get(p),j=k.encoder,t=u.get(j),"string"!=typeof t&&(t=j.encode(a),u.set(j,t));for(let a=0;a<e.length;a++){const b=e[a].doc;if(!b)continue;const c=parse_simple(b,p);if(!c)continue;const d=c.trim().split(/\s+/);if(!d.length)continue;let i="",u=[],v=[],w=-1,x=-1,y=0;for(let a=0;a<d.length;a++){let b=d[a],c=j.encode(b);c=1<c.length?c.join(" "):c[0];let e;if(c&&b){let a=b.length,d=(j.split?b.replace(j.split,""):b).length-c.length,f="",k=0;for(let i,j=0;j<t.length;j++){if(i=t[j],!i)continue;let l=i.length;if(l+=d,k&&l<=k)continue;const m=c.indexOf(i);-1<m&&(f=(m?b.substring(0,m):"")+g+b.substring(m,m+l)+h+(m+l<a?b.substring(m+l):""),k=l,e=!0)}f&&(l&&(0>w&&(w=i.length+(i?1:0)),x=i.length+(i?1:0)+f.length,y+=a,v.push(u.length),u.push({match:f})),i+=(i?" ":"")+f)}if(!e){const b=d[a];i+=(i?" ":"")+b,l&&u.push({text:b})}else if(l&&y>=l)break}let z=v.length*(f.length-2);if(q||r||l&&i.length-z>l){let a,b,c=l+z-2*s,e=x-w;if(0<q&&(e+=q),0<r&&(e+=r),e<=c)a=q?w-(0<q?q:0):w-(0|(c-e)/2),b=r?x+(0<r?r:0):a+c,m||(0<a&&(" "===i.charAt(a)||" "!==i.charAt(a-1)&&(a=i.indexOf(" ",a),0>a&&(a=0))),b<i.length&&(" "===i.charAt(b-1)||" "!==i.charAt(b)&&(b=i.lastIndexOf(" ",b),b<x?b=x:++b))),i=(a?o:"")+i.substring(a,b)+(b<i.length?o:"");else{const a=[],b={},c={},e={},g={},h={};for(let j=0,n=0,o=0,p=1,t=1;;){let w;for(let x,y=0;y<v.length;y++){if(x=v[y],!o){i=u[x].match,q&&(g[y]=q),r&&(h[y]=r),y&&j++;let b;if(x?!y&&s&&(j+=s):(c[y]=1,e[y]=1),x>=d.length-1?b=1:x<u.length-1&&u[x+1].match?b=1:s&&(j+=s),j-=f.length-2,!y||j+i.length<=l)a[y]=i;else{c[y]=0,w=p=t=0;break}b&&(c[y+1]=1,e[y+1]=1)}else if(n!=o){if(e[y+1])continue;if(x+=o,b[x]){j-=s,c[y+1]=1,e[y+1]=1;continue}if(x>=u.length-1){if(x>=u.length){e[y+1]=1,x>=d.length&&(c[y+1]=1);continue}j-=s}i=u[x].text;let f=r&&h[y];if(f)if(0<f){if(i.length>f)if(e[y+1]=1,m)i=i.substring(0,f);else continue;f-=i.length,f||(f=-1),h[y]=f}else{e[y+1]=1;continue}if(j+i.length+1<=l)i=" "+i,a[y]+=i;else if(m){const b=l-j-1;0<b&&(i=" "+i.substring(0,b),a[y]+=i),e[y+1]=1}else{e[y+1]=1;continue}}else{if(e[y])continue;if(x-=n,b[x]){j-=s,e[y]=1,c[y]=1;continue}if(0>=x){if(0>x){e[y]=1,c[y]=1;continue}j-=s}i=u[x].text;let d=q&&g[y];if(d)if(0<d){if(i.length>d)if(e[y]=1,m)i=i.substring(i.length-d);else continue;d-=i.length,d||(d=-1),g[y]=d}else{e[y]=1;continue}if(j+i.length+1<=l)i+=" ",a[y]=i+a[y];else if(m){const b=i.length+1-(l-j);0<=b&&b<i.length&&(i=i.substring(b)+" ",a[y]=i+a[y]),e[y]=1}else{e[y]=1;continue}}j+=i.length,b[x]=1,w=1}if(w)n==o?o++:n++;else{if(n==o?p=0:t=0,!p&&!t)break;p?(n++,o=n):o++}}i="";for(let b,d=0;d<a.length;d++)b=(d&&c[d]?" ":(d&&!o?" ":"")+o)+a[d],i+=b;o&&!c[a.length]&&(i+=o)}}n&&(i=i.replace(n," ")),e[a].highlight=i}if(d)break}return b}