=p))){a=o===61?1:2;break}}if(e.sCount[u]<0)continue;let f=!1;for(let g=0,p=r.length;g
3||e.sCount[a]<0)continue;let c=!1;for(let d=0,l=r.length;d ${t} ]*>.*?<\/a><\/p>/g,"").replace(/ne((2147483647-i)/c)&&he("overflow"),i+=(s-r)*c,r=s;for(let d of e)if(d
${t}`:t,body:e.body};let r=`
]*>.*?<\/a>/g,"").trim()}function Zd(e){return e.includes("GroupId")&&e.includes("Src")&&e.some(n=>/^S\d+_Id$/i.test(n))&&e.some(n=>/^S\d+_Q$/i.test(n))&&e.some(n=>/^S\d+_A$/i.test(n))?e.filter(n=>n==="GroupId"||n==="Src"||/^S\d+_Id$/i.test(n)):[]}function Yd(e){let t=[];return{text:e.replace(Gd,r=>{let i=`@@QA_GROUP_INLINE_CODE_${t.length}@@`;return t.push(r),i}),restore:r=>t.reduce((i,a,o)=>i.split(`@@QA_GROUP_INLINE_CODE_${o}@@`).join(a),r)}}var Xd="manual-render-v3",we=class{constructor(t=new ze){this.deckResolutionService=t}resolve(t,n){let r=Jd(t.cardType,n),i=this.deckResolutionService.resolve(t,n.defaultDeck,n.folderDeckMode),a=i.resolvedDeck.value,o=n.noteFieldMappings[L(t.cardType,r)]??null,u={version:Xd,cardType:t.cardType,noteModel:r,mapping:o,addObsidianBacklink:n.addObsidianBacklink,obsidianBacklinkLabel:n.obsidianBacklinkLabel,obsidianBacklinkPlacement:n.obsidianBacklinkPlacement,convertHighlightsToCloze:n.convertHighlightsToCloze,keepPureTagLinesInCardBody:n.keepPureTagLinesInCardBody},s=H(JSON.stringify(u));return{deck:a,noteModel:r,renderConfigHash:s,warnings:i.warnings}}};function Jd(e,t){if(vi(e)){if(!t.clozeNoteType.trim())throw new C("errors.noteFieldMapping.noteTypeNotSelected.cloze");return t.clozeNoteType}if(e==="semantic-qa"){if(!t.semanticQaNoteType.trim())throw new C("errors.noteFieldMapping.noteTypeNotSelected.semanticQa");return t.semanticQaNoteType}if(!t.qaNoteType.trim())throw new C("errors.noteFieldMapping.noteTypeNotSelected.basic");return t.qaNoteType}var yn=class{constructor(t=new we){this.renderConfigService=t}plan(t,n,r,i){let a=new Map,o=new Set(n.pendingWriteBack.filter(k=>k.markerKind!=="group-gi").map(k=>Ee(k.filePath,k.blockStartLine,k.rawBlockHash))),u=new Set,s=[],c=[],d=[],l=[],f=[],g=new Map,p=0;for(let k of t){if(a.has(k.syncKey))throw new Error(`Duplicate syncKey detected in current scan: ${k.syncKey}`);if(a.set(k.syncKey,k),k.noteId!==void 0){let y=N(k.noteId);if(u.has(y))throw new Error(`Duplicate noteId detected in current scan: ${k.noteId}`);u.add(y)}let S=k.noteId!==void 0?n.cards[N(k.noteId)]:void 0,x=this.renderConfigService.resolve(k,i);for(let y of x.warnings)g.set(Oe(y),y);let h=k.noteId,w=Ee(k.filePath,k.blockStartLine,k.rawBlockHash),v={card:{...k,noteId:h},noteId:h,deck:x.deck,noteModel:x.noteModel,renderConfigHash:x.renderConfigHash};if(!h){s.push(v);continue}if(d.push(v),(k.idMarkerState!=="present-valid"||k.noteIdSource!=="marker"||o.has(w))&&f.push(v),!S){c.push(v);continue}let _=S.rawBlockHash!==k.rawBlockHash||S.renderConfigHash!==x.renderConfigHash||!Vi(S.tagsHint,k.tagsHint)||S.orphan,A=S.deck!==x.deck;_&&c.push(v),A&&l.push(v),!_&&!A&&(p+=1)}let M=new Set(r),b=Object.values(n.cards).filter(k=>M.has(k.filePath)&&!u.has(N(k.noteId)));return{toCreate:s,toUpdate:c,toVerifyDeck:d,toChangeDeck:l,toRewriteMarker:f,toOrphan:b,unchangedCards:p,warnings:Array.from(g.values())}}};var so=new tt({breaks:!0,html:!0,linkify:!0}),el=/`[^`\n]+`/g,tl=/```[\s\S]*?```|~~~[\s\S]*?~~~/g,nl=/(?`\\[${b.trim()}\\]`),c=c.replace(rl,(M,b)=>`\\(${b.trim()}\\)`),i&&r.convertHighlightsToCloze&&(c=c.replace(co,"{$1}")),i&&(c=c.replace(al,(M,b,k)=>`{{c${b?Number(b):l++}::${k}}}`)),c=c.replace(ol,(M,b)=>{let k=r.resourceResolver.resolveEmbed(b,n.card.filePath);return k?(d.push({kind:k.kind,fileName:k.fileName,absolutePath:k.absolutePath,altText:k.altText}),k.kind==="audio"?`[sound:${k.fileName}]`:``):M}),c=c.replace(ul,(M,b)=>{let k=r.resourceResolver.resolveWikiLink(b,n.card.filePath);return k?`${rt(k.displayText)}`:M}),c=c.replace(co,"$1");let f=Ur(c,il,"MATH");c=f.text,c=s.restore(c),c=u.restore(c);let g=(a?so.renderInline(c):so.render(c)).trim(),p=f.restore(g,rt);return{html:a?p:mn(p),media:d}}};function Ur(e,t,n){let r=[];return{text:e.replace(t,a=>{let o=`@@${n}_${r.length}@@`;return r.push(a),o}),restore:(a,o=u=>u)=>r.reduce((u,s,c)=>u.split(`@@${n}_${c}@@`).join(o(s)),a)}}function sl(e){let t=new Set;return e.filter(n=>{let r=`${n.kind}:${n.absolutePath}:${n.fileName}`;return t.has(r)?!1:(t.add(r),!0)})}function rt(e){return e.replace(/&/g,"&").replace(//g,">").replace(/"/g,""").replace(/'/g,"'")}var it=class extends C{constructor(t){super("errors.currentFileOutOfScope",{filePath:t}),this.name="CurrentFileOutOfScopeError"}},Sn=class{constructor(t,n,r,i=new Vt(t),a=new yn,o=new xn,u=new qt(r),s=new jt(t),c=new we,d=()=>Date.now()){this.vaultGateway=t;this.pluginStateRepository=n;this.ankiGateway=r;this.fileIndexerService=i;this.diffPlannerService=a;this.renderer=o;this.ankiBatchExecutor=u;this.markdownWriteBackService=s;this.scanScopeService=new Le;if(this.qaGroupSyncService=new bn(r,void 0,void 0,void 0,void 0,void 0,void 0,this.vaultGateway.createBacklink.bind(this.vaultGateway)),this.groupMarkerService=new V,cl(c)){this.renderConfigService=c,this.now=d;return}this.renderConfigService=new we,this.now=c}async syncVault(t){oe(t);let n=await this.pluginStateRepository.load(),r=await this.fileIndexerService.indexVault(t,n);return this.syncIndexedResult(r,n,t)}async syncFile(t,n){if(oe(n),!this.scanScopeService.isPathInScope(t,n.scopeMode,n.includeFolders,n.excludeFolders))throw new it(t);let r=await this.pluginStateRepository.load(),i=await this.fileIndexerService.indexFile(t,n,r);return this.syncIndexedResult(i,r,n)}async rebuildIndex(t){oe(t);let n=await this.pluginStateRepository.load(),r=await this.fileIndexerService.indexVault(t,n,!0),i=this.diffPlannerService.plan(r.cards,n,r.scopedFilePaths,t),a=new Map(r.indexedFiles.map(l=>[l.filePath,l])),o=i.toRewriteMarker.filter(l=>l.noteId!==void 0),u=this.buildReindexedGroupStates(r.groupBlocks,n),s=await this.markdownWriteBackService.write(o,a,u.markerWrites),c=lo(n,r.scopedFilePaths,u.syncedGroupBlocks),d=this.buildNextState(n,r.cards,r.indexedFiles,u.syncedGroupBlocks,t,new Set(s.writtenSyncKeys),new Map,i.toOrphan,c);if(d.pendingWriteBack=[...n.pendingWriteBack.filter(l=>!new Set(r.scopedFilePaths).has(l.filePath)),...s.pendingEntries],this.markFilesDirty(d,[...s.failureFiles.map(l=>l.filePath),...s.conflictFiles]),await this.pluginStateRepository.save(d),s.failureFiles.length>0)throw this.createWriteBackFailureError(s.failureFiles);return{scannedFiles:r.scannedFiles,scannedCards:r.cards.length+r.groupBlocks.length,created:0,updated:0,migratedNoteTypes:0,migratedDecks:0,orphaned:i.toOrphan.length+c.length,uploadedMedia:0,skippedUnchangedCards:r.skippedUnchangedCards,rewrittenMarkers:s.writtenSyncKeys.length,markerWriteConflictFiles:s.conflictFiles,warnings:po(i.warnings,u.warnings)}}async syncIndexedResult(t,n,r){let i=this.diffPlannerService.plan(t.cards,n,t.scopedFilePaths,r),a={addObsidianBacklink:r.addObsidianBacklink,obsidianBacklinkLabel:r.obsidianBacklinkLabel,obsidianBacklinkPlacement:r.obsidianBacklinkPlacement,convertHighlightsToCloze:r.convertHighlightsToCloze,keepPureTagLinesInCardBody:r.keepPureTagLinesInCardBody,resourceResolver:this.vaultGateway},o=new Map;for(let g of[...i.toCreate,...i.toUpdate])o.has(g.card.syncKey)||o.set(g.card.syncKey,this.renderer.render(g,a));let u=await this.ankiBatchExecutor.execute(i,o,async g=>this.renderer.render(g,a),r.noteFieldMappings),s=await this.qaGroupSyncService.sync(t.groupBlocks,n,r),c=new Map(t.indexedFiles.map(g=>[g.filePath,g])),d=await this.markdownWriteBackService.write(u.markerWrites,c,s.markerWrites),l=lo(n,t.scopedFilePaths,s.syncedGroupBlocks),f=this.buildNextState(n,t.cards,t.indexedFiles,s.syncedGroupBlocks,r,new Set([...u.touchedSyncKeys,...s.touchedSyncKeys,...d.writtenSyncKeys]),u.resolvedNoteIds,i.toOrphan,l);if(f.pendingWriteBack=[...n.pendingWriteBack.filter(g=>!new Set(t.scopedFilePaths).has(g.filePath)),...d.pendingEntries],this.markFilesDirty(f,[...d.failureFiles.map(g=>g.filePath),...d.conflictFiles]),await this.pluginStateRepository.save(f),d.failureFiles.length>0)throw this.createWriteBackFailureError(d.failureFiles);return{scannedFiles:t.scannedFiles,scannedCards:t.cards.length+t.groupBlocks.length,created:u.created+s.created,updated:u.updated+s.updated,migratedNoteTypes:u.migratedNoteTypes+s.migratedNoteTypes,migratedDecks:u.migratedDecks+s.migratedDecks,orphaned:i.toOrphan.length+l.length,uploadedMedia:u.uploadedMedia,skippedUnchangedCards:t.skippedUnchangedCards,rewrittenMarkers:d.writtenSyncKeys.length,markerWriteConflictFiles:d.conflictFiles,warnings:po(i.warnings,s.warnings)}}buildNextState(t,n,r,i,a,o,u,s,c){let d=this.now(),l=zn(a),f=new Set(r.filter(b=>b.content!==void 0).map(b=>b.filePath)),g=new Map;for(let b of i){let k=g.get(b.filePath);k?k.push(b):g.set(b.filePath,[b])}let p={files:{...t.files},cards:{...t.cards},groupBlocks:{...t.groupBlocks??{}},pendingWriteBack:[...t.pendingWriteBack]};for(let b of f)delete p.files[b];for(let[b,k]of Object.entries(p.cards))f.has(k.filePath)&&delete p.cards[b];for(let[b,k]of Object.entries(p.groupBlocks??{}))f.has(k.filePath)&&delete p.groupBlocks?.[b];let M=new Map;for(let b of n){let k=u.get(b.syncKey)??b.noteId;k!==void 0&&M.set(b.syncKey,k)}for(let b of r){if(b.content===void 0)continue;let k=Array.from(new Set(b.cards.map(S=>M.get(S.syncKey)).filter(S=>S!==void 0)));p.files[b.filePath]={filePath:b.filePath,fileHash:b.fileHash,fileStamp:b.fileStamp,deckRulesFingerprint:l,lastIndexedAt:d,noteIds:k,groupIds:(g.get(b.filePath)??[]).map(S=>S.groupId)}}for(let b of n){let k=M.get(b.syncKey);if(k===void 0)continue;let S=t.cards[N(k)]??(b.noteId!==void 0?t.cards[N(b.noteId)]:void 0),x=this.renderConfigService.resolve(b,a);p.cards[N(k)]={noteId:k,filePath:b.filePath,heading:b.heading,backlinkHeadingText:b.backlinkHeadingText,headingLevel:b.headingLevel,bodyMarkdown:b.bodyMarkdown,cardType:b.cardType,blockStartOffset:b.blockStartOffset,blockEndOffset:b.blockEndOffset,blockStartLine:b.blockStartLine,bodyStartLine:b.bodyStartLine,blockEndLine:b.blockEndLine,contentEndLine:b.contentEndLine,markerLine:b.markerLine,rawBlockText:b.rawBlockText,rawBlockHash:b.rawBlockHash,renderConfigHash:x.renderConfigHash,deck:x.deck,deckHint:b.deckHint,deckHintSource:b.deckHintSource,deckWarnings:[...b.deckWarnings],tagsHint:b.tagsHint,lastSyncedAt:o.has(b.syncKey)?d:S?.lastSyncedAt??0,orphan:!1}}for(let b of s)p.cards[N(b.noteId)]={...b,orphan:!0};for(let b of i)p.groupBlocks??(p.groupBlocks={}),p.groupBlocks[b.groupId]=b;for(let b of c)p.groupBlocks??(p.groupBlocks={}),p.groupBlocks[b.groupId]={...b,orphan:!0};return p}buildReindexedGroupStates(t,n){let r=[],i=[],a=[],o=new Map(Object.values(n.groupBlocks??{}).filter(s=>!s.orphan).map(s=>[s.noteId,s])),u=new Map;for(let s of Object.values(n.groupBlocks??{}).filter(c=>!c.orphan)){let c=u.get(s.src);c?c.push(s):u.set(s.src,[s])}for(let s of t){let c=(s.groupId?n.groupBlocks?.[s.groupId]:void 0)??(s.noteId!==void 0?o.get(s.noteId):void 0)??dl(u.get(s.src));if(!c)continue;let d=Object.fromEntries(c.items.filter(l=>l.itemId&&l.slot).map(l=>[l.itemId,l.slot]));r.push({...c,filePath:s.filePath,headingText:s.headingText,backlinkHeadingText:s.backlinkHeadingText,headingLevel:s.headingLevel,stem:s.stem,src:s.src,blockStartOffset:s.blockStartOffset,blockEndOffset:s.blockEndOffset,blockStartLine:s.blockStartLine,bodyStartLine:s.bodyStartLine,blockEndLine:s.blockEndLine,contentEndLine:s.contentEndLine,markerLine:s.markerLine,markerIndent:s.markerIndent,rawBlockText:s.rawBlockText,rawBlockHash:s.rawBlockHash,deckHint:s.deckHint,deckHintSource:s.deckHintSource,deckWarnings:[...s.deckWarnings],tagsHint:s.tagsHint?[...s.tagsHint]:[],orphan:!1}),a.push(...s.deckWarnings),s.sourceContent&&(s.markerState!=="present-valid"||!this.groupMarkerService.isEquivalent(s.groupMarker,c.noteId,d,c.freeSlots)||ll(s))&&i.push({syncKey:s.syncKey,filePath:s.filePath,blockStartLine:s.blockStartLine,contentEndLine:s.contentEndLine,blockEndLine:s.blockEndLine,markerLine:s.markerLine,markerIndent:s.markerIndent,noteId:c.noteId,groupId:c.groupId,itemToSlot:d,freeSlots:[...c.freeSlots],sourceContent:s.sourceContent})}return{syncedGroupBlocks:r,markerWrites:i,warnings:a}}markFilesDirty(t,n){for(let r of new Set(n))t.files[r]&&(t.files[r]={...t.files[r],fileHash:"",fileStamp:""})}createWriteBackFailureError(t){return new C("errors.writeBack.summary",{fileCount:t.length},{failures:t})}};function cl(e){return typeof e=="object"&&e!==null&&"resolve"in e}function lo(e,t,n){let r=new Set(t),i=new Set(n.map(a=>a.groupId));return Object.values(e.groupBlocks??{}).filter(a=>r.has(a.filePath)&&!i.has(a.groupId)).map(a=>({...a,orphan:!0}))}function po(...e){let t=new Map;for(let n of e)for(let r of n)t.set(Oe(r),r);return[...t.values()]}function dl(e){if(!(!e||e.length!==1))return e[0]}function ll(e){if(!e.sourceContent)return!1;let t=e.sourceContent.split(/\r?\n/);for(let n=e.blockStartLine;n