@gracious.tech/fetch-client
Version:
Interact with a fetch(bible) collection in an API-like way
18 lines (11 loc) • 30.6 kB
JavaScript
var re=Object.defineProperty;var ne=(o,e,t)=>e in o?re(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var p=(o,e,t)=>(ne(o,typeof e!="symbol"?e+"":e,t),t);function j(o,e,t){let s=parseInt(o,10);return Number.isNaN(s)?null:(e!==void 0&&(s=Math.max(s,e)),t!==void 0&&(s=Math.min(s,t)),s)}var d=Object.freeze(["gen","exo","lev","num","deu","jos","jdg","rut","1sa","2sa","1ki","2ki","1ch","2ch","ezr","neh","est","job","psa","pro","ecc","sng","isa","jer","lam","ezk","dan","hos","jol","amo","oba","jon","mic","nam","hab","zep","hag","zec","mal","mat","mrk","luk","jhn","act","rom","1co","2co","gal","eph","php","col","1th","2th","1ti","2ti","tit","phm","heb","jas","1pe","2pe","1jn","2jn","3jn","jud","rev"]),k=Object.freeze({gen:"Genesis",exo:"Exodus",lev:"Leviticus",num:"Numbers",deu:"Deuteronomy",jos:"Joshua",jdg:"Judges",rut:"Ruth","1sa":"1 Samuel","2sa":"2 Samuel","1ki":"1 Kings","2ki":"2 Kings","1ch":"1 Chronicles","2ch":"2 Chronicles",ezr:"Ezra",neh:"Nehemiah",est:"Esther",job:"Job",psa:"Psalms",pro:"Proverbs",ecc:"Ecclesiastes",sng:"Song of Songs",isa:"Isaiah",jer:"Jeremiah",lam:"Lamentations",ezk:"Ezekiel",dan:"Daniel",hos:"Hosea",jol:"Joel",amo:"Amos",oba:"Obadiah",jon:"Jonah",mic:"Micah",nam:"Nahum",hab:"Habakkuk",zep:"Zephaniah",hag:"Haggai",zec:"Zechariah",mal:"Malachi",mat:"Matthew",mrk:"Mark",luk:"Luke",jhn:"John",act:"Acts",rom:"Romans","1co":"1 Corinthians","2co":"2 Corinthians",gal:"Galatians",eph:"Ephesians",php:"Philippians",col:"Colossians","1th":"1 Thessalonians","2th":"2 Thessalonians","1ti":"1 Timothy","2ti":"2 Timothy",tit:"Titus",phm:"Philemon",heb:"Hebrews",jas:"James","1pe":"1 Peter","2pe":"2 Peter","1jn":"1 John","2jn":"2 John","3jn":"3 John",jud:"Jude",rev:"Revelation"}),C=Object.freeze({gen:"Gen",exo:"Exo",lev:"Lev",num:"Num",deu:"Deut",jos:"Josh",jdg:"Judg",rut:"Ruth","1sa":"1 Sam","2sa":"2 Sam","1ki":"1 King","2ki":"2 King","1ch":"1 Chr","2ch":"2 Chr",ezr:"Ezra",neh:"Neh",est:"Est",job:"Job",psa:"Psalm",pro:"Prov",ecc:"Ecc",sng:"Song",isa:"Isa",jer:"Jer",lam:"Lam",ezk:"Ezek",dan:"Dan",hos:"Hos",jol:"Joel",amo:"Amos",oba:"Obad",jon:"Jonah",mic:"Micah",nam:"Nahum",hab:"Hab",zep:"Zeph",hag:"Hag",zec:"Zech",mal:"Mal",mat:"Matt",mrk:"Mark",luk:"Luke",jhn:"John",act:"Acts",rom:"Rom","1co":"1 Cor","2co":"2 Cor",gal:"Gal",eph:"Eph",php:"Phil",col:"Col","1th":"1 Thes","2th":"2 Thes","1ti":"1 Tim","2ti":"2 Tim",tit:"Titus",phm:"Phil",heb:"Heb",jas:"James","1pe":"1 Pet","2pe":"2 Pet","1jn":"1 John","2jn":"2 John","3jn":"3 John",jud:"Jude",rev:"Rev"}),R=Object.freeze([["num","nm"],["ezr","ez"],["mic","mc"],["hab","hb"],["jhn","jn"],["php","phil"],["phm","pm"],["jas","jm"],["jud","jud"],["jud","jd"]]),G=Object.freeze(["is","so","at","am","me","he","hi"]);var O=Object.freeze({"1ch":[54,55,24,43,26,81,40,40,44,14,47,40,14,17,29,43,27,17,19,8,30,19,32,31,31,32,34,21,30],"1co":[31,16,23,21,13,20,40,13,27,33,34,31,13,40,58,24],"1jn":[10,29,24,21,21],"1ki":[53,46,28,34,18,38,51,66,28,29,43,33,34,31,34,34,24,46,21,43,29,53],"1pe":[25,25,22,19,14],"1sa":[28,36,21,22,12,21,17,22,27,27,15,25,23,52,35,23,58,30,24,42,15,23,29,22,44,25,12,25,11,31,13],"1th":[10,20,13,18,28],"1ti":[20,15,16,16,25,21],"2ch":[17,18,17,22,14,42,22,18,31,19,23,16,22,15,19,14,19,34,11,37,20,12,21,27,28,23,9,27,36,27,21,33,25,33,27,23],"2co":[24,17,18,18,21,18,16,24,15,18,33,21,14],"2jn":[13],"2ki":[18,25,27,44,27,33,20,29,37,36,21,21,25,29,38,20,41,37,37,21,26,20,37,20,30],"2pe":[21,22,18],"2sa":[27,32,39,12,25,23,29,18,13,19,27,31,39,33,37,23,29,33,43,26,22,51,39,25],"2th":[12,17,18],"2ti":[18,26,17,22],"3jn":[15],act:[26,47,26,37,42,15,60,40,43,48,30,25,52,28,41,40,34,28,41,38,40,30,35,27,27,32,44,31],amo:[15,16,15,13,27,14,17,14,15],col:[29,23,25,18],dan:[21,49,30,37,31,28,28,27,27,21,45,13],deu:[46,37,29,49,33,25,26,20,29,22,32,32,18,29,23,22,20,22,21,20,23,30,25,22,19,19,26,68,29,20,30,52,29,12],ecc:[18,26,22,16,20,12,29,17,18,20,10,14],eph:[23,22,21,32,33,24],est:[22,23,15,17,14,14,10,17,32,3],exo:[22,25,22,31,23,30,25,32,35,29,10,51,22,31,27,36,16,27,25,26,36,31,33,18,40,37,21,43,46,38,18,35,23,35,35,38,29,31,43,38],ezk:[28,10,27,17,17,14,27,18,11,22,25,28,23,23,8,63,24,32,14,49,32,31,49,27,17,21,36,26,21,26,18,32,33,31,15,38,28,23,29,49,26,20,27,31,25,24,23,35],ezr:[11,70,13,24,17,22,28,36,15,44],gal:[24,21,29,31,26,18],gen:[31,25,24,26,32,22,24,22,29,32,32,20,18,24,21,16,27,33,38,18,34,24,20,67,34,35,46,22,35,43,55,32,20,31,29,43,36,30,23,23,57,38,34,34,28,34,31,22,33,26],hab:[17,20,19],hag:[15,23],heb:[14,18,19,16,14,20,28,13,28,39,40,29,25],hos:[11,23,5,19,15,11,16,14,17,15,12,14,16,9],isa:[31,22,26,6,30,13,25,22,21,34,16,6,22,32,9,14,14,7,25,6,17,25,18,23,12,21,13,29,24,33,9,20,24,17,10,22,38,22,8,31,29,25,28,28,25,13,15,22,26,11,23,15,12,17,13,12,21,14,21,22,11,12,19,12,25,24],jas:[27,26,18,17,20],jdg:[36,23,31,24,31,40,25,35,57,18,40,15,25,20,20,31,13,31,30,48,25],jer:[19,37,25,31,31,30,34,22,26,25,23,17,27,22,21,21,27,23,15,18,14,30,40,10,38,24,22,17,32,24,40,44,26,22,19,32,21,28,18,16,18,22,13,30,5,28,7,47,39,46,64,34],jhn:[51,25,36,54,47,71,53,59,41,42,57,50,38,31,27,33,26,40,42,31,25],job:[22,13,26,21,27,30,21,22,35,22,20,25,28,22,35,22,16,21,29,29,34,30,17,25,6,14,23,28,25,31,40,22,33,37,16,33,24,41,30,24,34,17],jol:[20,32,21],jon:[17,10,10,11],jos:[18,24,17,24,15,27,26,35,27,43,23,24,33,15,63,10,18,28,51,9,45,34,16,33],jud:[25],lam:[22,22,66,22,22],lev:[17,16,17,35,19,30,38,36,24,20,47,8,59,57,33,34,16,30,37,27,24,33,44,23,55,46,34],luk:[80,52,38,44,39,49,50,56,62,42,54,59,35,35,32,31,37,43,48,47,38,71,56,53],mal:[14,17,18,6],mat:[25,23,17,25,48,34,29,34,38,42,30,50,58,36,39,28,27,35,30,34,46,46,39,51,46,75,66,20],mic:[16,13,12,13,15,16,20],mrk:[45,28,35,41,43,56,37,38,50,52,33,44,37,72,47,20],nam:[15,13,19],neh:[11,20,32,23,19,19,73,18,38,39,36,47,31],num:[54,34,51,49,31,27,89,26,23,36,35,16,33,45,41,50,13,32,22,29,35,41,30,25,18,65,23,31,40,16,54,42,56,29,34,13],oba:[21],phm:[25],php:[30,30,21,23],pro:[33,22,35,27,23,35,27,36,18,32,31,28,25,35,33,33,28,24,29,30,31,29,35,34,28,28,27,28,27,33,31],psa:[6,12,8,8,12,10,17,9,20,18,7,8,6,7,5,11,15,50,14,9,13,31,6,10,22,12,14,9,11,12,24,11,22,22,28,12,40,22,13,17,13,11,5,26,17,11,9,14,20,23,19,9,6,7,23,13,11,11,17,12,8,12,11,10,13,20,7,35,36,5,24,20,28,23,10,12,20,72,13,19,16,8,18,12,13,17,7,18,52,17,16,15,5,23,11,13,12,9,9,5,8,28,22,35,45,48,43,13,31,7,10,10,9,8,18,19,2,29,176,7,8,9,4,8,5,6,5,6,8,8,3,18,3,3,21,26,9,8,24,13,10,7,12,15,21,10,20,14,9,6],rev:[20,29,22,11,14,17,17,13,21,11,19,18,18,20,8,21,18,24,21,15,27,21],rom:[32,29,31,25,21,23,25,39,33,21,36,21,14,26,33,27],rut:[22,23,18,22],sng:[17,17,11,16,16,13,13,14],tit:[16,15,15],zec:[21,13,10,14,11,15,14,23,17,12,17,14,9,21],zep:[18,15,20]});var ie=Object.defineProperty,F=Object.getOwnPropertySymbols,oe=Object.prototype.hasOwnProperty,ae=Object.prototype.propertyIsEnumerable,N=(o,e,t)=>e in o?ie(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,le=(o,e)=>{for(var t in e||(e={}))oe.call(e,t)&&N(o,t,e[t]);if(F)for(var t of F(e))ae.call(e,t)&&N(o,t,e[t]);return o},x=(o,e,t)=>(N(o,typeof e!="symbol"?e+"":e,t),t);function V(o){var e,t,s,n,r,i,l,a,h;o=o.replace(/ /g,"").replace(/\./g,":").replace(/:/gu,":").replace(new RegExp("\\p{Dash}","gu"),"-");let _,c,g,u;if(o.includes(":")){let m=o.split("-"),b=m[0].split(":");if(_=(n=j(b[0]))!=null?n:void 0,c=(i=j((r=b[1])!=null?r:""))!=null?i:void 0,m[1]){let f=m[1].split(":");f.length>1?(g=(l=j(f[0]))!=null?l:void 0,u=(a=j(f[1]))!=null?a:void 0):u=(h=j(f[0]))!=null?h:void 0}}else{let m=o.split("-");_=(e=j(m[0]))!=null?e:void 0,g=(s=j((t=m[1])!=null?t:""))!=null?s:void 0}return{start_chapter:_,start_verse:c,end_chapter:g,end_verse:u}}function _e(o,e,t=[],s=!0){let n=_=>_.trim().toLowerCase().replace(/^i /,"1").replace("1st ","1").replace("first ","1").replace(/^ii /,"2").replace("2nd ","2").replace("second ","2").replace(/^iii /,"3").replace("3rd ","3").replace("third ","3").replace(/[^\d\p{Letter}]/gui,"");if(o=n(o),!o||(t=t.map(_=>n(_)),t.includes(o)))return null;if(d.includes(o))return o;let r=e.map(([_,c])=>[_,n(c)]).filter(([_,c])=>c),i=[];for(let[_,c]of r){if(o===c)return _;c.startsWith(o)&&i.push([_,c])}if(i.length===1)return i[0][0];if(i.length)return null;let l=o.split("").join(".{0,4}");s&&(["1","2","3"].includes(o[0])?l="^"+o[0]+o.slice(1).split("").join(".{0,4}"):l="^"+l);let a=new RegExp(l),h=r.filter(([_,c])=>a.test(c));return h.length===1?h[0][0]:null}var T=class o{constructor(e,t,s){x(this,"type"),x(this,"range"),x(this,"book"),x(this,"ot"),x(this,"nt"),x(this,"start_chapter"),x(this,"start_verse"),x(this,"end_chapter"),x(this,"end_verse"),x(this,"args_valid"),x(this,"_args");var n,r,i,l,a;typeof e!="string"?this._args={book:e.book,start_chapter:e.start_chapter,start_verse:e.start_verse,end_chapter:e.end_chapter,end_verse:e.end_verse}:this._args={book:e,start_chapter:t,start_verse:s};let h=typeof this._args.start_chapter=="number"||typeof this._args.end_chapter=="number",_=typeof this._args.start_verse=="number"||typeof this._args.end_verse=="number";this.book=this._args.book,this.start_chapter=(n=this._args.start_chapter)!=null?n:1,this.start_verse=(r=this._args.start_verse)!=null?r:1,this.end_chapter=(l=(i=this._args.end_chapter)!=null?i:this._args.start_chapter)!=null?l:1,this.end_verse=(a=this._args.end_verse)!=null?a:this._args.end_chapter?999:1,d.indexOf(this.book)===-1&&(this.book="gen");let c=O[this.book];this.start_chapter<1?(this.start_chapter=1,this.start_verse=1):this.start_chapter>c.length&&(this.start_chapter=c.length,this.start_verse=c[c.length-1]),this.start_verse=Math.min(Math.max(this.start_verse,1),c[this.start_chapter-1]),(this.end_chapter<this.start_chapter||this.end_chapter===this.start_chapter&&this.end_verse<this.start_verse)&&(this.end_chapter=this.start_chapter,this.end_verse=this.start_verse),this.end_chapter>c.length&&(this.end_chapter=c.length,this.end_verse=c[c.length-1]),this.end_verse=Math.min(Math.max(this.end_verse,1),c[this.end_chapter-1]);let g=this.start_chapter===this.end_chapter,u=this.start_verse===this.end_verse;g&&u?this.type=h?_?"verse":"chapter":"book":this.type=g?"range_verses":_?"range_multi":"range_chapters",this.type==="range_multi"&&this.start_verse===1&&this.end_verse===c[this.end_chapter-1]&&(this.type="range_chapters"),this.range=this.type.startsWith("range_"),this.ot=d.indexOf(this.book)<39,this.nt=!this.ot;let m=()=>{if(this._args.book!==this.book)return!1;let b=["start_chapter","start_verse","end_chapter","end_verse"];for(let f of b)if(Number.isInteger(this._args[f])&&this._args[f]!==this[f])return!1;return!(!this._args.start_chapter&&(this._args.end_chapter||this._args.start_verse||this._args.end_verse)||this._args.end_verse&&!this._args.start_verse||this._args.start_verse&&this._args.end_chapter&&!this._args.end_verse)};this.args_valid=m()}static from_string(e,t,s,n=2,r=!0){t||(t=[...Object.entries(k),...R],s||(s=[...G]));let i=Array.isArray(t)?t:Object.entries(t);e=e.trim();let l=e.slice(1).search(/\d/)+1;l===0&&(l=e.length);let a=e.slice(0,l).trim();if(a.length<n)return null;let h=_e(a,i,s,r);if(!h)return null;let _=e.slice(l),c=V(_);return["2jn","3jn","jud","oba","phm"].includes(h)&&c.start_chapter&&c.start_verse===void 0&&c.end_verse===void 0&&(c=V("1:"+_)),new o(le({book:h},c))}static from_refs(e,t){return new o({book:e.book,start_chapter:e.start_chapter,start_verse:e.start_verse,end_chapter:t.end_chapter,end_verse:t.end_verse})}get_book_string(e={}){return e[this.book]||k[this.book]}get_verses_string(e=":",t="-"){if(this.type==="book")return"";if(this.type==="chapter")return`${this.start_chapter}`;if(this.type==="range_chapters")return`${this.start_chapter}${t}${this.end_chapter}`;if(this.type==="verse")return`${this.start_chapter}${e}${this.start_verse}`;let s=`${this.start_chapter}${e}${this.start_verse}${t}`;return this.end_chapter!==this.start_chapter&&(s+=`${this.end_chapter}${e}`),s+`${this.end_verse}`}toString(e={},t=":",s="-"){return(this.get_book_string(e)+" "+this.get_verses_string(t,s)).trim()}is_before(e,t){return this.end_chapter<e||this.end_chapter===e&&this.end_verse<t}is_after(e,t){return this.start_chapter>e||this.start_chapter===e&&this.start_verse>t}includes(e,t){return!this.is_before(e,t)&&!this.is_after(e,t)}get_start(){return new o({book:this.book,start_chapter:this.start_chapter,start_verse:this.start_verse})}get_end(){return new o({book:this.book,start_chapter:this.end_chapter,start_verse:this.end_verse})}get_prev_verse(e=!1){let t=e?this.end_chapter:this.start_chapter,s=e?this.end_verse:this.start_verse;return t===1&&s===1?null:(s===1?(t-=1,s=O[this.book][t-1]):s-=1,new o({book:this.book,start_chapter:t,start_verse:s}))}get_next_verse(e=!1){let t=e?this.end_chapter:this.start_chapter,s=e?this.end_verse:this.start_verse,n=O[this.book];return t===n.length&&s===n[n.length-1]?null:(s===n[t-1]?(t+=1,s=1):s+=1,new o({book:this.book,start_chapter:t,start_verse:s}))}};var Z="[:\uFF1A\\.]",q="(?:(?:[123]|I{1,3}) ? ?)?",ce="\\p{Letter}[\\p{Letter}\\p{Dash} ]{MIN_MID,16}END_LETTER\\.? ? ?",K="\\d{1,3}[abc]?(?: ? ?"+Z+" ? ?\\d{1,3}[abc]?)?",W=K+"(?: ? ?\\p{Dash} ? ?"+K+")?",Y="(?![\\d\\p{Letter}@#$%])",Q=" ? ?[,;] ? ?",he=Q+"("+W+")"+Y;function*U(o,e,t,s=2,n=!0){let r=c=>T.from_string(c,e,t,s,n),i=ce.replace("MIN_MID",String(Math.max(0,s-2))).replace("END_LETTER",s>1?"\\p{Letter}":""),l=q+i+W+Y,a=Q+"("+q+i+")",h=new RegExp(l,"uig"),_=0;for(;;){let c=h.exec(o);if(!c)return null;let g=r(c[0]);if(g&&g.args_valid){yield{ref:g,text:c[0],index:c.index,index_from_prev_match:c.index-_},_=c.index+c[0].length;let u=new RegExp(he,"uiy");for(u.lastIndex=h.lastIndex;;){let m=new RegExp(a,"uiy");m.lastIndex=u.lastIndex;let b=m.exec(o);if(b&&r(b[1]))break;let f=u.exec(o);if(!f)break;let y=f.index+f[0].indexOf(f[1]),v=g.book;!new RegExp(Z).test(f[1])&&["verse","range_verses","range_multi"].includes(g.type)&&(v+=`${g.end_chapter}:`);let D=r(v+f[1]);if(!D||!D.args_valid)break;yield{ref:D,text:f[1],index:y,index_from_prev_match:y-_},_=y+f[1].length,u.lastIndex>h.lastIndex&&(h.lastIndex=u.lastIndex)}}else{let u=c[0].indexOf(" ",1);u>=1&&(h.lastIndex-=c[0].length-u-1)}}}function ge(o){return[...Array(O[o].length).keys()].map(e=>e+1)}function ue(o,e){return[...Array(O[o][e-1]).keys()].map(t=>t+1)}async function B(o){let e=await fetch(o,{mode:"cors"});if(e.ok)return await e.text();throw new Error(`${e.status} ${e.statusText}: ${o}`)}function S(o){let e=Array.isArray(o)?[]:{};for(let t in o){let s=o[t];e[t]=typeof s=="object"&&s!==null?S(s):s}return e}function X(o){return o.normalize("NFKD").replace(/\p{Diacritic}/gu,"")}function me(o,e){if(!o||!e)return-1/0;let t=0,s=0;for(let n=0;n<e.length;n++)if(e[n]===o[s]){if(s++,s>=o.length)return t*-1}else{if(s===0)return-1/0;if(t++,t>4)return-1/0}return-1/0}function ee(o,e,t){if(!o.trim())return e.slice();let s=X(o).toLowerCase().trim().split(" "),n=e.map(r=>{let i=X(t(r)).toLowerCase().trim().split(" "),l=s.reduce((a,h)=>a+Math.max(...i.map(_=>me(h,_))),0);return{candidate:r,score:l}}).filter(r=>r.score>-4);return n.sort((r,i)=>i.score-r.score),n.map(r=>r.candidate)}function te(o){let e={"&":"&","<":"<",">":">",'"':""","'":"'"};return o.replace(/[&<>"']/g,t=>e[t])}function $(o){if(o<=0)return"";let e=97;return $(Math.floor((o-1)/26))+String.fromCharCode((o-1)%26+e)}function se(o,e,t,s){for(let n of[o,e,t,s]){if(typeof n!="number")throw new Error("Chapter/verse arguments must all be numbers");if(n<0)throw new Error("Chapter/verse arguments cannot be negative")}if(o>t||o===t&&e>s)throw new Error("Passage end is less than passage start")}var I=class{constructor(e,t){p(this,"_copyright");p(this,"_html");this._copyright=t,this._html=JSON.parse(e)}get_attribution(e){if(!this._copyright)return"";e||(e=this._copyright.licenses[0]);let t=this._copyright.attribution_url,s=te(this._copyright.attribution);return`
<p class="fb-attribution">
<a href="${t}" target="_blank">${s}</a>
(<a href="${e.url}" target="_blank">${e.name}</a>)
</p>
`}_attribution(e){return e===!1?"":this.get_attribution(!e||e===!0?void 0:e)}get_whole({attribute:e}={}){return this._html.contents.flat().map(t=>t[1]).join("")+this._attribution(e)}get_passage(e,t,s,n,r={}){let i=this._get_list(e,t,s,n);if(!i.length)return"";let l="";for(let a of i)l+=`<div class="fb-chapter" data-c="${a[0].chapter}">`+a[0].content[0]+a.map(h=>h.content[1]).join("")+a[a.length-1].content[2]+"</div>";return l+this._attribution(r.attribute)}get_passage_from_ref(e,t={}){return e.type==="book"?this.get_whole(t):e.type==="chapter"||e.type==="range_chapters"?this.get_chapters(e.start_chapter,e.end_chapter,t):this.get_passage(e.start_chapter,e.start_verse,e.end_chapter,e.end_verse,t)}get_chapters(e,t,s={}){if(typeof e!="number"||typeof t!="number")throw new Error("First/last chapters must be numbers");return this.get_passage(e,1,t+1,0,s)}get_chapter(e,t={}){return this.get_chapters(e,e,t)}get_verse(e,t,s={}){return this.get_passage(e,t,e,t,s)}_get_list(e=1,t=1,s,n){s?typeof n!="number"&&(s+=1,n=0):(s=this._html.contents.length,n=0),se(e,t,s,n);let r=(a,h,_)=>{var g,u;return((u=(g=this._html.contents[a])==null?void 0:g.slice(h,_&&_+1))!=null?u:[]).map((m,b)=>{let f=h+b;return{id:a*1e3+f,chapter:a,verse:f,content:m}})},l=[r(e,t,e===s?n:void 0)];for(let a=e+1;a<s;a++)l.push(r(a,1));return s>e&&l.push(r(s,1,n)),l.filter(a=>a.length)}get_list(e,t,s,n){return this._get_list(e,t,s,n).flat().map(r=>({...r,content:r.content.join("")}))}get_list_from_ref(e){let t=e.end_chapter,s=e.end_verse;return e.type==="book"?(t=void 0,s=void 0):e.type==="chapter"&&(t+=1,s=0),this.get_list(e.start_chapter,e.start_verse,t,s)}},L=class{constructor(e,t){p(this,"_copyright");p(this,"_usx");this._copyright=t,this._usx=e}get_whole(){return this._usx}},P=class{constructor(e,t){p(this,"_copyright");p(this,"_usfm");this._copyright=t,this._usfm=e}get_whole(){return this._usfm}};function pe(o,e=!0,t=!0){let s=[],n=o.map(r=>typeof r=="string"?r:r.type==="heading"&&e?`
`+"#".repeat(r.level)+" "+r.contents:r.type==="note"&&t?(s.push(r.contents),`[^${$(s.length)}]`):"").join("");return s.length&&(n+=`
`,n+=s.map((r,i)=>`[^${$(i+1)}]: ${r}`).join(`
`)),n}var E=class{constructor(e,t){p(this,"_copyright");p(this,"_txt");this._copyright=t,this._txt=JSON.parse(e)}get_attribution(e){return this._copyright?(e||(e=this._copyright.licenses[0]),`
[${e.name} - ${this._copyright.attribution}]`):""}_attribution(e){return e===!1?"":this.get_attribution(!e||e===!0?void 0:e)}get_whole(e={}){return this.get_passage(1,1,this._txt.contents.length,0,e)}get_passage(e,t,s,n,r={}){se(e,t,s,n);let i=(_,c,g)=>{var m,b;let u=(b=(m=this._txt.contents[_])==null?void 0:m.slice(c,g&&g+1))!=null?b:[];return r.verse_nums?u.map((f,y)=>{let v="",w=c+y;return(w===1||c>1&&y===0)&&(v+=`${_}:`),v+=w,v=`[${v}] `,[v,...f]}):u},a=i(e,t,e===s?n:void 0);for(let _=e+1;_<s;_++)a.push(...i(_,1));return s>e&&a.push(...i(s,1,n)),pe(a.flat(),r.headings,r.notes)+this._attribution(r.attribute)}get_chapters(e,t,s={}){if(typeof e!="number"||typeof t!="number")throw new Error("First/last chapters must be numbers");return this.get_passage(e,1,t+1,0,s)}get_chapter(e,t={}){return this.get_chapters(e,e,t)}get_verse(e,t,s={}){return this.get_passage(e,t,e,t,s)}};function H(o,e){return o.filter(t=>{let s=t.restrictions;return(!e.commercial||!s.forbid_commercial)&&(!e.attributionless||!s.forbid_attributionless)&&(!e.derivatives||!s.forbid_derivatives||e.derivatives==="same-license"&&s.forbid_derivatives==="same-license")&&(!e.limitless||!s.forbid_limitless)})}var z=class{constructor(e){p(this,"_extra");this._extra=e}_ensure_book_exists(e){if(!d.includes(e))throw new Error(`Book id "${e}" is not valid (should be 3 letters lowercase)`);if(!(e in this._extra.book_names))throw new Error(`Translation does not have book "${e}"`)}get_book_name(e){return this._ensure_book_exists(e),{...this._extra.book_names[e]}}get_chapters(e){return this._ensure_book_exists(e),this._extra.chapter_headings[e].map((t,s)=>({number:s,heading:t})).slice(1)}get_chapter_heading(e,t){return this._ensure_book_exists(e),this._extra.chapter_headings[e][t]}get_sections(e){return this._ensure_book_exists(e),this._extra.sections[e].map(t=>{var n;let s=(n=t.heading)!=null?n:this._extra.chapter_headings[e][t.start_chapter];return{...t,heading:s}})}};var J=class{constructor(e,t,s){p(this,"_usage");p(this,"_remember_fetches");p(this,"_fetch_book_cache",{});p(this,"_fetch_extras_cache",{});p(this,"_local_book_names",{});p(this,"_manifest");p(this,"_endpoints",{});p(this,"_modern_year",new Date().getFullYear()-70);this._usage=e,this._remember_fetches=t,this._manifest={licenses:s[0][1].licenses,languages:{},language2to3:{},languages_most_spoken:s[0][1].languages_most_spoken,translations:{}};let n=new Set;for(let[r,i]of s.reverse()){for(let[l,a]of Object.entries(i.translations)){let h=a.copyright.licenses.map(g=>typeof g.license=="string"?{id:g.license,name:i.licenses[g.license].name,restrictions:i.licenses[g.license].restrictions,url:g.url}:{id:null,name:"Custom license",restrictions:g.license,url:g.url});if(h=H(h,this._usage),!h.length)continue;n.add(l.slice(0,3));let _=a.books_ot===!0?d.slice(0,39):a.books_ot,c=a.books_nt===!0?d.slice(39):a.books_nt;this._manifest.translations[l]={...a,books_ot_list:_,books_nt_list:c,copyright:{...a.copyright,licenses:h}},this._endpoints[l]=r}for(let l in i.languages)n.has(l)&&(this._manifest.languages[l]=i.languages[l]);for(let[l,a]of Object.entries(i.language2to3))n.has(a)&&(this._manifest.language2to3[l]=i.language2to3[l])}}_ensure_trans_exists(e){if(!this.has_translation(e))throw new Error(`Translation with id "${e}" does not exist in collection(s)`)}_ensure_book_exists(e,t){if(this._ensure_trans_exists(e),!d.includes(t))throw new Error(`Book id "${t}" is not valid (should be 3 letters lowercase)`);if(!this.has_book(e,t))throw new Error(`Translation "${e}" does not have book "${t}"`)}has_language(e){return e in this._manifest.languages}has_translation(e){return e in this._manifest.translations}has_book(e,t){this._ensure_trans_exists(e);let s=this._manifest.translations[e];return s.books_ot_list.includes(t)||s.books_nt_list.includes(t)}get_language(e){let t=this._manifest.languages[e];if(t)return{code:e,name_local:t.local,name_english:t.english,name_bilingual:t.english===t.local?t.local:`${t.local} (${t.english})`,population:t.pop}}get_languages({object:e,exclude_old:t,sort_by:s,search:n}={}){let r=Object.keys(this._manifest.languages).map(i=>this.get_language(i));if(t&&(r=r.filter(i=>i.population!==null)),n!==void 0&&(r=ee(n,r,i=>i.name_local+" "+i.name_english)),e)return Object.fromEntries(r.map(i=>[i.code,i]));if(!n)if(s==="population_L1")r.sort((i,l)=>{var a,h;return((a=l.population)!=null?a:-1)-((h=i.population)!=null?h:-1)});else if(s==="population"){let i=l=>{var h;let a=this._manifest.languages_most_spoken.indexOf(l.code);return a!==-1?(this._manifest.languages_most_spoken.length-a)*9999999999:(h=l.population)!=null?h:-1};r.sort((l,a)=>i(a)-i(l))}else r.sort((i,l)=>{let a=s==="english"?"name_english":"name_local";return i[a].localeCompare(l[a])});return r}get_preferred_language(e=[]){return this.get_language(this._get_preferred_language_code(e))}_get_preferred_language_code(e=[]){var t,s,n;e.length===0&&typeof self!="undefined"&&(e=[...(s=self.navigator.languages)!=null?s:[(t=self.navigator.language)!=null?t:"eng"]]);for(let r of e){if(r=(n=r.toLowerCase().split("-")[0])!=null?n:"",r in this._manifest.languages)return r;if(r in this._manifest.language2to3)return this._manifest.language2to3[r]}return"eng"in this._manifest.languages?"eng":Object.keys(this._manifest.languages)[0]}get_translation(e){return this._get_translation(e)}_get_translation(e,t){let s=this._manifest.translations[e];if(!s)return;let n=s.name.local||s.name.english;s.name.local&&s.name.english&&s.name.local.toLowerCase()!==s.name.english.toLowerCase()&&(n=`${s.name.local} (${s.name.english})`);let r=s.name.local_abbrev||s.name.english_abbrev;return s.name.local_abbrev&&s.name.english_abbrev&&s.name.local_abbrev!==s.name.english_abbrev&&(r=`${s.name.local_abbrev} (${s.name.english_abbrev})`),{id:e,language:e.slice(0,3),direction:s.direction,year:s.year,name:s.name.local||s.name.english,name_abbrev:s.name.local_abbrev||s.name.english_abbrev,name_english:s.name.english,name_english_abbrev:s.name.english_abbrev,name_local:s.name.local,name_local_abbrev:s.name.local_abbrev,name_bilingual:n,name_bilingual_abbrev:r,attribution:s.copyright.attribution,attribution_url:s.copyright.attribution_url,licenses:S(H(s.copyright.licenses,{...this._usage,...t})),liternalness:s.literalness,tags:[...s.tags]}}get_translations({language:e,object:t,sort_by_year:s,usage:n,exclude_obsolete:r,exclude_incomplete:i}={}){let l=Object.keys(this._manifest.translations).map(a=>this._get_translation(a,n)).filter(a=>a.licenses.length);if(e&&(l=l.filter(a=>a.language===e)),r){let a=l.filter(h=>h.year>=this._modern_year);a.length&&(l=a);for(let h of["archaic","questionable","niche"]){let _=l.filter(c=>!c.tags.includes(h));_.length&&(l=_)}}return i&&(l=l.filter(a=>{let h=this._manifest.translations[a.id];return h.books_ot===!0&&h.books_nt===!0})),t?Object.fromEntries(l.map(a=>[a.id,a])):(l.sort((a,h)=>s?h.year-a.year:a.name_local.localeCompare(h.name_local)),l)}get_preferred_translation(e=[]){return this.get_translation(this._get_preferred_translation_id(e))}_get_preferred_translation_id(e=[]){let t=this._get_preferred_language_code(e),s=null,n=!1,r=-9999;for(let[i,l]of Object.entries(this._manifest.translations))if(i.slice(0,3)===t){if(l.tags.includes("recommended"))return i;let a=l.books_ot===!0&&l.books_nt===!0;(!s||!n&&a||a&&l.year>r)&&(s=i,r=l.year,n=a)}return s!=null?s:Object.keys(this._manifest.translations)[0]}get_books(e,{object:t,sort_by_name:s,testament:n,whole:r}={}){let i=d;if(e){this._ensure_trans_exists(e);let _=this._manifest.translations[e];i=[..._.books_ot_list,..._.books_nt_list]}let l={};e&&e in this._local_book_names&&(l=this._local_book_names[e]);let a=n?n==="ot"?[0,39]:[39]:[],h=d.slice(...a).filter(_=>r||i.includes(_)).map(_=>{var f,y,v,w;let c=d.indexOf(_)<39,g=(f=l[_])==null?void 0:f.normal,u=k[_];g&&u.toLowerCase()!==g.toLowerCase()&&(u=`${g} (${u})`);let m=(y=l[_])==null?void 0:y.abbrev,b=C[_];return m&&b.toLowerCase()!==m.toLowerCase()&&(b=`${m} (${b})`),{id:_,name:g||k[_],name_abbrev:m||C[_],name_english:k[_],name_english_abbrev:C[_],name_local:g!=null?g:"",name_local_abbrev:m!=null?m:"",name_local_long:(w=(v=l[_])==null?void 0:v.long)!=null?w:"",name_bilingual:u,name_bilingual_abbrev:b,ot:c,nt:!c,available:!!e&&i.includes(_)}});return t?Object.fromEntries(h.map(_=>[_.id,_])):(s&&h.sort((_,c)=>_.name_english.localeCompare(c.name_english)),h)}get_book_url(e,t,s="html"){let n=["html","txt"].includes(s)?"json":s;return`${this._endpoints[e]}bibles/${e}/${s}/${t}.${n}`}get_completion(e){this._ensure_trans_exists(e);let t={nt:{available:[],missing:[]},ot:{available:[],missing:[]}},s=this._manifest.translations[e],n=[...s.books_ot_list,...s.books_nt_list],r="ot";for(let i of d){i==="mat"&&(r="nt");let l=n.includes(i)?"available":"missing";t[r][l].push(i)}return t}async fetch_book(e,t,s="html"){this._ensure_book_exists(e,t);let n=`${e} ${t} ${s}`;if(n in this._fetch_book_cache)return this._fetch_book_cache[n];let r=B(this.get_book_url(e,t,s)).then(i=>{let l={html:I,usx:L,usfm:P,txt:E}[s];return new l(i,this._manifest.translations[e].copyright)});return this._remember_fetches&&(this._fetch_book_cache[n]=r,r.catch(()=>{delete this._fetch_book_cache[n]})),r}async fetch_translation_extras(e){if(this._ensure_trans_exists(e),e in this._fetch_extras_cache)return this._fetch_extras_cache[e];let t=`${this._endpoints[e]}bibles/${e}/extra.json`,s=B(t).then(n=>{let r=JSON.parse(n);return this._local_book_names[e]=r.book_names,new z(r)});return this._remember_fetches&&(this._fetch_extras_cache[e]=s,s.catch(()=>{delete this._fetch_book_cache[e]})),s}_from_string_args(e=[],t=!0){var _,c,g;let s=[],n=typeof e=="string"?[e]:e;for(let u of n)for(let[m,b]of Object.entries((_=this._local_book_names[u])!=null?_:{}))b.normal&&s.push([m,b.normal]),b.abbrev&&s.push([m,b.abbrev]);if(t){for(let[u,m]of Object.entries(k))s.push([u,m]);for(let[u,m]of R)s.push([u,m])}let r=["zho","lzh","gan","hak","czh","cjy","cmn","mnp","cdo","nan","czo","cnp","cpx","csp","wuu","hsn","yue","jpn","kor"],i=(g=(c=n[0])==null?void 0:c.split("_")[0])!=null?g:"eng",l=i==="eng"?[...G]:[],a=r.includes(i)?1:2,h=!r.includes(i);return[s,l,a,h]}detect_references(e,t=[],s=!0){return U(e,...this._from_string_args(t,s))}string_to_reference(e,t=[],s=!0){return T.from_string(e,...this._from_string_args(t,s))}reference_to_string(e,t,s){var r;let n={...s?C:k};if(t){let i=s?"abbrev":"normal";for(let[l,a]of Object.entries((r=this._local_book_names[t])!=null?r:{}))a[i]&&(n[l]=a[i])}return e.toString(n)}};var M=class{constructor(e){p(this,"_data");this._data=e}get_refs(e,t){var s,n,r;return(r=(n=(s=this._data[e])==null?void 0:s[t])==null?void 0:n.map(i=>{var l,a;return{book:i[0],start_chapter:i[1],start_verse:i[2],end_chapter:(l=i[3])!=null?l:i[1],end_verse:(a=i[4])!=null?a:i[2],single_verse:!!i[2]&&i[3]===void 0}}))!=null?r:[]}};var A=class{constructor(e={}){p(this,"_endpoints");p(this,"_data_endpoint");p(this,"_usage",{commercial:!1,attributionless:!1,derivatives:!1,limitless:!0});p(this,"_remember_fetches");p(this,"_collection_promise");p(this,"_crossref_cache",{});p(this,"collection");var t,s;this._endpoints=(t=e.endpoints)!=null?t:["https://v1.fetch.bible/"],this._data_endpoint=(s=e.data_endpoint)!=null?s:this._endpoints[0],this._usage={...this._usage,...e.usage},this._remember_fetches=e.remember_fetches!==!1}async fetch_collection(){return this._collection_promise?this._collection_promise:(this._collection_promise=Promise.all(this._endpoints.map(async e=>[e,JSON.parse(await B(e+"bibles/manifest.json"))])).then(e=>(this.collection=new J(this._usage,this._remember_fetches,e),this.collection)),this._collection_promise.catch(()=>{this._collection_promise=void 0}),this._collection_promise)}async fetch_crossref(e,t="medium"){let s=`${e} ${t}`;if(s in this._crossref_cache)return this._crossref_cache[s];let n=this._data_endpoint+`crossref/${t}/${e}.json`,r=B(n).then(i=>new M(JSON.parse(i)));return this._remember_fetches&&(this._crossref_cache[s]=r,r.catch(()=>{delete this._crossref_cache[s]})),r}};export{A as BibleClient,T as PassageReference,C as book_abbrev_english,k as book_names_english,d as books_ordered,U as detect_references,G as english_abbrev_exclude,R as english_abbrev_include,ge as get_chapters,ue as get_verses,O as last_verse};
//# sourceMappingURL=bundled.mjs.map