@vue-skuilder/platform-ui 0.1.38 → 0.1.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/dist/assets/{About-BmmLOXxW.js → About-BGB7Xnk6.js} +2 -2
  2. package/dist/assets/{About-BmmLOXxW.js.map → About-BGB7Xnk6.js.map} +1 -1
  3. package/dist/assets/{AdminDashboard-CErUEgh7.js → AdminDashboard-iNWJ0DEZ.js} +2 -2
  4. package/dist/assets/{AdminDashboard-CErUEgh7.js.map → AdminDashboard-iNWJ0DEZ.js.map} +1 -1
  5. package/dist/assets/{ClassroomCtrlPanel-CQlQ7U0H.js → ClassroomCtrlPanel-DF2rs7CR.js} +2 -2
  6. package/dist/assets/{ClassroomCtrlPanel-CQlQ7U0H.js.map → ClassroomCtrlPanel-DF2rs7CR.js.map} +1 -1
  7. package/dist/assets/{Classrooms-CruLt4WS.js → Classrooms-Cds87MEv.js} +2 -2
  8. package/dist/assets/{Classrooms-CruLt4WS.js.map → Classrooms-Cds87MEv.js.map} +1 -1
  9. package/dist/assets/{CourseRouter-CG2xLBSN.js → CourseRouter-BmYgrlh9.js} +2 -2
  10. package/dist/assets/{CourseRouter-CG2xLBSN.js.map → CourseRouter-BmYgrlh9.js.map} +1 -1
  11. package/dist/assets/{CourseWare-BTFRjgBR-DzHuJePR.js → CourseWare-BTFRjgBR-Djhqr7kc.js} +3 -3
  12. package/dist/assets/{CourseWare-BTFRjgBR-DzHuJePR.js.map → CourseWare-BTFRjgBR-Djhqr7kc.js.map} +1 -1
  13. package/dist/assets/{Courses-DT_e7SM_.js → Courses-D910P_cq.js} +2 -2
  14. package/dist/assets/{Courses-DT_e7SM_.js.map → Courses-D910P_cq.js.map} +1 -1
  15. package/dist/assets/{EloModeration-CRi4H0dg.js → EloModeration-Bz9h6sZo.js} +2 -2
  16. package/dist/assets/{EloModeration-CRi4H0dg.js.map → EloModeration-Bz9h6sZo.js.map} +1 -1
  17. package/dist/assets/{JoinCode-B5BsiUGo.js → JoinCode-KxUHyJpf.js} +2 -2
  18. package/dist/assets/{JoinCode-B5BsiUGo.js.map → JoinCode-KxUHyJpf.js.map} +1 -1
  19. package/dist/assets/{NewCourseDialog-uYOn28uu.js → NewCourseDialog-WAOXxQ3Q.js} +2 -2
  20. package/dist/assets/{NewCourseDialog-uYOn28uu.js.map → NewCourseDialog-WAOXxQ3Q.js.map} +1 -1
  21. package/dist/assets/{ReleaseNotes-cmPGfYDr.js → ReleaseNotes-1XFtTIvr.js} +2 -2
  22. package/dist/assets/{ReleaseNotes-cmPGfYDr.js.map → ReleaseNotes-1XFtTIvr.js.map} +1 -1
  23. package/dist/assets/{RequestPasswordReset-B6y-gK1D.js → RequestPasswordReset-bUrIWjSL.js} +2 -2
  24. package/dist/assets/{RequestPasswordReset-B6y-gK1D.js.map → RequestPasswordReset-bUrIWjSL.js.map} +1 -1
  25. package/dist/assets/{ResetPassword-DujhlXIY.js → ResetPassword-DVWDFCo3.js} +2 -2
  26. package/dist/assets/{ResetPassword-DujhlXIY.js.map → ResetPassword-DVWDFCo3.js.map} +1 -1
  27. package/dist/assets/{Study-BZVEhSPH.js → Study-D51p-shx.js} +2 -2
  28. package/dist/assets/{Study-BZVEhSPH.js.map → Study-D51p-shx.js.map} +1 -1
  29. package/dist/assets/{TagInformation-CSxHlSB_.js → TagInformation-DkMk7Stg.js} +2 -2
  30. package/dist/assets/{TagInformation-CSxHlSB_.js.map → TagInformation-DkMk7Stg.js.map} +1 -1
  31. package/dist/assets/{User-CPBeLVtj.js → User-Bp1AnmLz.js} +2 -2
  32. package/dist/assets/{User-CPBeLVtj.js.map → User-Bp1AnmLz.js.map} +1 -1
  33. package/dist/assets/{UserStats-D_0jGd_V.js → UserStats-BYiC_k-m.js} +2 -2
  34. package/dist/assets/{UserStats-D_0jGd_V.js.map → UserStats-BYiC_k-m.js.map} +1 -1
  35. package/dist/assets/{VerifyEmail-DNWkeVaK.js → VerifyEmail-Dy1h9JYW.js} +2 -2
  36. package/dist/assets/{VerifyEmail-DNWkeVaK.js.map → VerifyEmail-Dy1h9JYW.js.map} +1 -1
  37. package/dist/assets/{chess-BbHATAzk-QdPb2NKL.js → chess-BbHATAzk-pOrUYAZ0.js} +2 -2
  38. package/dist/assets/{chess-BbHATAzk-QdPb2NKL.js.map → chess-BbHATAzk-pOrUYAZ0.js.map} +1 -1
  39. package/dist/assets/chess-X1bmWmh3-D2SHv0Qi.js +1 -0
  40. package/dist/assets/common-ui.es-BvHCzp9O.js +1 -0
  41. package/dist/assets/{common-ui.es-BKENyPWw.js → common-ui.es-lR5lAjyE.js} +4 -4
  42. package/dist/assets/{common-ui.es-BKENyPWw.js.map → common-ui.es-lR5lAjyE.js.map} +1 -1
  43. package/dist/assets/{dist-DXR8aZcS.js → dist-B8xeftM2.js} +6 -6
  44. package/dist/assets/{dist-DXR8aZcS.js.map → dist-B8xeftM2.js.map} +1 -1
  45. package/dist/assets/dist-Dww9R8oE.js +1 -0
  46. package/dist/assets/dist-Fzmv5Raq.js +1 -0
  47. package/dist/assets/{dist-DkO2jIq6.js → dist-gNsavZQG.js} +3 -3
  48. package/dist/assets/{dist-DkO2jIq6.js.map → dist-gNsavZQG.js.map} +1 -1
  49. package/dist/assets/edit-ui.es-BpaEhZ3z.js +1 -0
  50. package/dist/assets/{edit-ui.es-BqKuNIaF.js → edit-ui.es-DQ_QxgcF.js} +2 -2
  51. package/dist/assets/{edit-ui.es-BqKuNIaF.js.map → edit-ui.es-DQ_QxgcF.js.map} +1 -1
  52. package/dist/assets/{french-Dk7YG8Td-D8yIv5X0.js → french-Dk7YG8Td-JC0vxa-w.js} +2 -2
  53. package/dist/assets/{french-Dk7YG8Td-D8yIv5X0.js.map → french-Dk7YG8Td-JC0vxa-w.js.map} +1 -1
  54. package/dist/assets/french-evUMlbbq-BuUWAV-J.js +1 -0
  55. package/dist/assets/{index-C6oRctJ1.js → index-Bu4mzQrw.js} +4 -4
  56. package/dist/assets/{index-C6oRctJ1.js.map → index-Bu4mzQrw.js.map} +1 -1
  57. package/dist/assets/{math-B4HbgYf6-OCtLj5EC.js → math-B4HbgYf6-Kurre5to.js} +2 -2
  58. package/dist/assets/{math-B4HbgYf6-OCtLj5EC.js.map → math-B4HbgYf6-Kurre5to.js.map} +1 -1
  59. package/dist/assets/math-DYni7rRl-B1C3iCP1.js +1 -0
  60. package/dist/assets/piano-BN5Btq91-CtjFp2A3.js +1 -0
  61. package/dist/assets/{piano-DF1g6yaX-5qaBtlIS.js → piano-DF1g6yaX-BJpKartB.js} +2 -2
  62. package/dist/assets/{piano-DF1g6yaX-5qaBtlIS.js.map → piano-DF1g6yaX-BJpKartB.js.map} +1 -1
  63. package/dist/assets/{pitch-CgGJFkZ1-Dzk_IhJm.js → pitch-CgGJFkZ1-DkV3oB-6.js} +2 -2
  64. package/dist/assets/{pitch-CgGJFkZ1-Dzk_IhJm.js.map → pitch-CgGJFkZ1-DkV3oB-6.js.map} +1 -1
  65. package/dist/assets/pitch-Dn0iNqiS-DPkXZ2T5.js +1 -0
  66. package/dist/assets/{server-BGaHYgjL.js → server-DoenpmEE.js} +2 -2
  67. package/dist/assets/{server-BGaHYgjL.js.map → server-DoenpmEE.js.map} +1 -1
  68. package/dist/assets/{sightsing-BFQ7HRig-C5c1duh9.js → sightsing-BFQ7HRig-ain8LgTr.js} +2 -2
  69. package/dist/assets/{sightsing-BFQ7HRig-C5c1duh9.js.map → sightsing-BFQ7HRig-ain8LgTr.js.map} +1 -1
  70. package/dist/assets/sightsing-CJX3k0Fd-CauzoxqQ.js +1 -0
  71. package/dist/assets/{typing-BevtfWlp-DGWRejzw.js → typing-BevtfWlp-BKIHTbO9.js} +2 -2
  72. package/dist/assets/{typing-BevtfWlp-DGWRejzw.js.map → typing-BevtfWlp-BKIHTbO9.js.map} +1 -1
  73. package/dist/assets/typing-k-ojjO7G-CWnss5na.js +1 -0
  74. package/dist/assets/{word-work-BOnRlZgd-CWYZMFn6.js → word-work-BOnRlZgd-BV5yYzT0.js} +2 -2
  75. package/dist/assets/{word-work-BOnRlZgd-CWYZMFn6.js.map → word-work-BOnRlZgd-BV5yYzT0.js.map} +1 -1
  76. package/dist/assets/word-work-Dd6tIKrt-CRz1ejBN.js +1 -0
  77. package/dist/index.html +3 -3
  78. package/dist/sw.js +1 -1
  79. package/dist/sw.js.map +1 -1
  80. package/package.json +7 -7
  81. package/dist/assets/chess-X1bmWmh3-BIJ2c9qj.js +0 -1
  82. package/dist/assets/common-ui.es-CiEkt-YW.js +0 -1
  83. package/dist/assets/dist-DybUdPG2.js +0 -1
  84. package/dist/assets/dist-ynKxKfpE.js +0 -1
  85. package/dist/assets/edit-ui.es-BELQ9vhV.js +0 -1
  86. package/dist/assets/french-evUMlbbq-DthHsC1k.js +0 -1
  87. package/dist/assets/math-DYni7rRl-BYtdSUem.js +0 -1
  88. package/dist/assets/piano-BN5Btq91-CHcAB3LR.js +0 -1
  89. package/dist/assets/pitch-Dn0iNqiS-BtLu6y7C.js +0 -1
  90. package/dist/assets/sightsing-CJX3k0Fd-BHdlbkRw.js +0 -1
  91. package/dist/assets/typing-k-ojjO7G-IjY29br_.js +0 -1
  92. package/dist/assets/word-work-Dd6tIKrt-DTcaMQ_N.js +0 -1
@@ -268,7 +268,7 @@ ${o.rows.map(e=>` ${e.id}-${e.key}
268
268
  `+l),c}async getCourseConfig(){let e=await getCredentialledCourseConfig(this.id);if(e)return e;throw Error(`Course config not found for course ID: ${this.id}`)}async updateCourseConfig(e){logger.debug(`Updating: ${JSON.stringify(e)}`);try{return await updateCredentialledCourseConfig(this.id,e)}catch(e){throw logger.error(`Error updating course config in course DB: ${e}`),e}}async updateCardElo(e,t){if(!t)throw Error(`Cannot update card elo with null or undefined value for card ID: ${e}`);try{return{ok:!0,id:e,rev:(await this.updateQueue.update(e,e=>(logger.debug(`Replacing ${JSON.stringify(e.elo)} with ${JSON.stringify(t)}`),e.elo=t,e)))._rev}}catch(t){throw logger.error(`Failed to update card elo for card ID: ${e}`,t),Error(`Failed to update card elo for card ID: ${e}`)}}async getAppliedTags(e){let t=await getAppliedTags(this.id,e);if(t)return t;throw Error(`Failed to find tags for card ${this.id}-${e}`)}async getAppliedTagsBatch(e){if(e.length===0)return new Map;let t=await this.db.query(`getTags`,{keys:e,include_docs:!1}),n=new Map;for(let t of e)n.set(t,[]);for(let e of t.rows){let t=e.key,r=e.value?.name;r&&n.has(t)&&n.get(t).push(r)}return n}async getAllCardIds(){return(await this.db.allDocs({startkey:`CARD-`,endkey:`CARD-￰`,include_docs:!1})).rows.map(e=>e.id)}async addTagToCard(e,t,n){return await addTagToCard(this.id,e,t,(await this._getCurrentUser()).getUsername(),n)}async removeTagFromCard(e,t){return await removeTagFromCard(this.id,e,t)}async createTag(e,t){return await createTag(this.id,e,t)}async getTag(e){return await getTag(this.id,e)}async updateTag(e){if(e.course!==this.id)throw Error(`Tag ${JSON.stringify(e)} does not belong to course ${this.id}`);return await updateTag(e)}async getCourseTagStubs(){return getCourseTagStubs(this.id)}async addNote(e,t,n,r,a,o,s=blankCourseElo()){try{let c=await addNote55(this.id,e,t,n,r,a,o,s);return c.ok?c.cardCreationFailed?(logger.warn(`[courseDB.addNote] Note added but card creation failed: ${c.cardCreationError}`),{status:Status.error,message:`Note was added but no cards were created: ${c.cardCreationError}`,id:c.id}):{status:Status.ok,message:``,id:c.id}:{status:Status.error,message:`Unexpected error adding note`}}catch(e){let t=e;return logger.error(`[addNote] error ${t.name}
269
269
  reason: ${t.reason}
270
270
  message: ${t.message}`),{status:Status.error,message:`Error adding note to course. ${e.reason||t.message}`}}}async getCourseDoc(e,t){return await this.db.get(e,t??{})}async getCourseDocs(e,t={}){return await this.db.allDocs({...t,keys:e})}getNavigationStrategy(e){if(logger.debug(`[courseDB] Getting navigation strategy: ${e}`),e==``){let e={_id:`NAVIGATION_STRATEGY-ELO`,docType:`NAVIGATION_STRATEGY`,name:`ELO`,description:`ELO-based navigation strategy for ordering content by difficulty`,implementingClass:`elo`,course:this.id,serializedData:``};return Promise.resolve(e)}else return this.db.get(e)}async getAllNavigationStrategies(){let e=DocTypePrefixes.NAVIGATION_STRATEGY;return(await this.db.allDocs({startkey:e,endkey:`${e}\uFFF0`,include_docs:!0})).rows.map(e=>e.doc)}async addNavigationStrategy(e){return logger.debug(`[courseDB] Adding navigation strategy: ${e._id}`),this.remoteDB.put(e).then(()=>{})}updateNavigationStrategy(e,t){return logger.debug(`[courseDB] Updating navigation strategy: ${e}`),logger.debug(JSON.stringify(t)),Promise.resolve()}async createNavigator(e){try{let t=await this.getAllNavigationStrategies();if(t.length===0)return logger.debug(`[courseDB] No strategy documents found, using default Pipeline(Composite(ELO, SRS), [eloDistanceFilter])`),createDefaultPipeline(e,this);let{pipeline:n,generatorStrategies:r,filterStrategies:a,warnings:o}=await new PipelineAssembler().assemble({strategies:t,user:e,course:this});for(let e of o)logger.warn(`[PipelineAssembler] ${e}`);return n?(logger.debug(`[courseDB] Using assembled pipeline with ${r.length} generator(s) and ${a.length} filter(s)`),n):(logger.debug(`[courseDB] Pipeline assembly failed, using default pipeline`),createDefaultPipeline(e,this))}catch(e){let t=e instanceof Error?`${e.message}
271
- ${e.stack}`:JSON.stringify(e);throw logger.error(`[courseDB] Error creating navigator: ${t}`),e}}setEphemeralHints(e){this._pendingHints=e}async getWeightedCards(e){let t=await this._getCurrentUser();try{let n=await this.createNavigator(t);return this._pendingHints&&(n.setEphemeralHints(this._pendingHints),this._pendingHints=null),n.getWeightedCards(e)}catch(e){throw logger.error(`[courseDB] Error getting weighted cards: ${e}`),e}}async getCardsCenteredAtELO(e={limit:99,elo:`user`},t){let n;if(e.elo===`user`){let e=await this._getCurrentUser();n=-1;try{n=EloToNumber((await e.getCourseRegistrationsDoc()).courses.find(e=>e.courseID===this.id).elo)}catch{n=1e3}}else if(e.elo===`random`){let e=await GET_CACHED(`elo-bounds-${this.id}`,()=>this.getELOBounds());n=Math.round(e.low+Math.random()*(e.high-e.low))}else n=e.elo;let r=[],a=4,o=-1,s=0;for(;r.length<e.limit&&s!==o;)r=await this.getCardsByELO(n,a*e.limit),o=s,s=r.length,logger.debug(`Found ${r.length} elo neighbor cards...`),t&&(r=r.filter(t),logger.debug(`Filtered to ${r.length} cards...`)),a*=2;let c=[];for(;c.length<e.limit&&r.length>0;){let e=randIntWeightedTowardZero(r.length),t=r.splice(e,1)[0];c.push(t)}return c.map(e=>({courseID:this.id,cardID:e.cardID,contentSourceType:`course`,contentSourceID:this.id,elo:e.elo,status:`new`}))}async searchCards(e){logger.log(`[CourseDB ${this.id}] Searching for: "${e}"`);let t;try{t=await this.db.find({selector:{docType:`DISPLAYABLE_DATA`,"data.0.data":{$regex:`.*${e}.*`}}}),logger.log(`[CourseDB ${this.id}] Regex search on data[0].data successful`)}catch(n){logger.log(`[CourseDB ${this.id}] Regex search failed, falling back to manual search:`,n);let r=await this.db.find({selector:{docType:`DISPLAYABLE_DATA`}});logger.log(`[CourseDB ${this.id}] Retrieved ${r.docs.length} documents for manual filtering`),t={docs:r.docs.filter(t=>{let n=JSON.stringify(t).toLowerCase().includes(e.toLowerCase());return n&&logger.log(`[CourseDB ${this.id}] Manual match found in document: ${t._id}`),n})}}if(logger.log(`[CourseDB ${this.id}] Found ${t.docs.length} displayable data documents`),t.docs.length===0){let e=await this.db.find({selector:{docType:`DISPLAYABLE_DATA`},limit:5});logger.log(`[CourseDB ${this.id}] Sample displayable data:`,e.docs.map(e=>({id:e._id,docType:e.docType,dataStructure:e.data?Object.keys(e.data):`no data field`,dataContent:e.data,fullDoc:e})))}let n=[];for(let e of t.docs){let t=await this.db.find({selector:{docType:`CARD`,id_displayable_data:{$in:[e._id]}}});logger.log(`[CourseDB ${this.id}] Displayable data ${e._id} linked to ${t.docs.length} cards`),n.push(...t.docs)}return logger.log(`[CourseDB ${this.id}] Total cards found: ${n.length}`),n}async find(e){return this.db.find(e)}}}}),classroomLookupDBTitle,CLASSROOM_CONFIG,ClassroomDBBase,StudentClassroomDB,TeacherClassroomDB,ClassroomLookupDB,init_classroomDB2=__esm({"src/impl/couch/classroomDB.ts"(){init_factory(),init_logger(),init_pouchdb_setup(),init_couch(),init_courseDB(),classroomLookupDBTitle=`classdb-lookup`,CLASSROOM_CONFIG=`ClassroomConfig`,ClassroomDBBase=class{constructor(){_defineProperty(this,`_id`,void 0),_defineProperty(this,`_db`,void 0),_defineProperty(this,`_cfg`,void 0),_defineProperty(this,`_initComplete`,!1),_defineProperty(this,`_content_prefix`,`content`)}get _content_searchkeys(){return getStartAndEndKeys2(this._content_prefix)}async getAssignedContent(){return logger.info(`Getting assigned content...`),(await this._db.allDocs({startkey:this._content_prefix,endkey:this._content_prefix+`￰`,include_docs:!0})).rows.map(e=>e.doc)}getContentId(e){return e.type===`tag`?`${this._content_prefix}-${e.courseID}-${e.tagID}`:`${this._content_prefix}-${e.courseID}`}get ready(){return this._initComplete}getConfig(){return this._cfg}},StudentClassroomDB=class _StudentClassroomDB extends ClassroomDBBase{constructor(e,t){super(),_defineProperty(this,`userMessages`,void 0),_defineProperty(this,`_user`,void 0),this._id=e,this._user=t}async init(){let e=`classdb-student-${this._id}`;this._db=new pouchdb_setup_default(ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+e,createPouchDBConfig());try{this._cfg=await this._db.get(CLASSROOM_CONFIG),this.userMessages=this._db.changes({since:`now`,live:!0,include_docs:!0}),this._initComplete=!0;return}catch(e){throw Error(`Error in StudentClassroomDB constructor: ${JSON.stringify(e)}`)}}static async factory(e,t){let n=new _StudentClassroomDB(e,t);return await n.init(),n}setChangeFcn(e){this.userMessages.on(`change`,e)}async getWeightedCards(e){let t=[],n=(await this._user.getPendingReviews()).filter(e=>e.scheduledFor===`classroom`&&e.schedulingAgentId===this._id);for(let e of n)t.push({cardId:e.cardId,courseId:e.courseId,score:1,reviewID:e._id,provenance:[{strategy:`classroom`,strategyName:`Classroom`,strategyId:`CLASSROOM`,action:`generated`,score:1,reason:`Classroom scheduled review`}]});let r=await this._user.getActiveCards(),a=new Set(r.map(e=>e.cardID)),o=hooks.utc(),s=(await this.getAssignedContent()).filter(e=>o.isAfter(hooks.utc(e.activeOn,REVIEW_TIME_FORMAT2)));logger.info(`[StudentClassroomDB] Due content: ${JSON.stringify(s)}`);for(let n of s)if(n.type===`course`){let{cards:r}=await new CourseDB(n.courseID,async()=>this._user).getWeightedCards(e);for(let e of r)a.has(e.cardId)||t.push({...e,provenance:[...e.provenance,{strategy:`classroom`,strategyName:`Classroom`,strategyId:`CLASSROOM`,action:`passed`,score:e.score,reason:`Assigned via classroom from course ${n.courseID}`}]})}else if(n.type===`tag`){let e=await getTag(n.courseID,n.tagID);for(let r of e.taggedCards)a.has(r)||t.push({cardId:r,courseId:n.courseID,score:1,provenance:[{strategy:`classroom`,strategyName:`Classroom`,strategyId:`CLASSROOM`,action:`generated`,score:1,reason:`Classroom assigned tag: ${n.tagID}, new card`}]})}else n.type===`card`&&(a.has(n.cardID)||t.push({cardId:n.cardID,courseId:n.courseID,score:1,provenance:[{strategy:`classroom`,strategyName:`Classroom`,strategyId:`CLASSROOM`,action:`generated`,score:1,reason:`Classroom assigned card, new card`}]}));return logger.info(`[StudentClassroomDB] New cards from classroom ${this._cfg.name}: ${t.length} total (reviews + new)`),{cards:t.sort((e,t)=>t.score-e.score).slice(0,e)}}},TeacherClassroomDB=class _TeacherClassroomDB extends ClassroomDBBase{constructor(e){super(),_defineProperty(this,`_stuDb`,void 0),this._id=e}async init(){let e=`classdb-teacher-${this._id}`,t=`classdb-student-${this._id}`;this._db=new pouchdb_setup_default(ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+e,createPouchDBConfig()),this._stuDb=new pouchdb_setup_default(ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+t,createPouchDBConfig());try{return this._db.get(CLASSROOM_CONFIG).then(e=>{this._cfg=e,this._initComplete=!0}).then(()=>{})}catch(e){throw Error(`Error in TeacherClassroomDB constructor: ${JSON.stringify(e)}`)}}static async factory(e){let t=new _TeacherClassroomDB(e);return await t.init(),t}async removeContent(e){let t=this.getContentId(e);try{let e=await this._db.get(t);await this._db.remove(e),this._db.replicate.to(this._stuDb,{doc_ids:[t]})}catch(e){logger.error(`Failed to remove content:`,t,e)}}async assignContent(e){let t,n=this.getContentId(e);return t=e.type===`tag`?await this._db.put({courseID:e.courseID,tagID:e.tagID,type:`tag`,_id:n,assignedBy:e.assignedBy,assignedOn:hooks.utc(),activeOn:e.activeOn||hooks.utc()}):await this._db.put({courseID:e.courseID,type:`course`,_id:n,assignedBy:e.assignedBy,assignedOn:hooks.utc(),activeOn:e.activeOn||hooks.utc()}),t.ok?(this._db.replicate.to(this._stuDb,{doc_ids:[n]}),!0):!1}},ClassroomLookupDB=()=>new pouchdb_setup_default(ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+classroomLookupDBTitle,{skip_setup:!0})}}),AdminDB,init_adminDB2=__esm({"src/impl/couch/adminDB.ts"(){init_pouchdb_setup(),init_factory(),init_couch(),init_classroomDB2(),init_courseLookupDB(),init_logger(),AdminDB=class{constructor(){_defineProperty(this,`usersDB`,void 0),this.usersDB=new pouchdb_setup_default(ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+`_users`,createPouchDBConfig())}async getUsers(){return(await this.usersDB.allDocs({include_docs:!0,...getStartAndEndKeys2(`org.couchdb.user:`)})).rows.map(e=>e.doc)}async getCourses(){let e=await CourseLookup.allCourseWare();return await Promise.all(e.map(e=>getCredentialledCourseConfig(e._id)))}async removeCourse(e){let t=await CourseLookup.delete(e),n=await getCredentialledCourseConfig(e);n.deleted=!0;let r=await updateCredentialledCourseConfig(e,n);return{ok:t.ok&&r.ok,id:t.id,rev:t.rev}}async getClassrooms(){let e=(await ClassroomLookupDB().allDocs({include_docs:!0})).rows.map(e=>e.doc.uuid);logger.debug(e.join(`, `));let t=[];for(let n=0;n<e.length;n++)try{let r=await TeacherClassroomDB.factory(e[n]);t.push(r)}catch(t){let r=t;r.error&&r.error===`not_found`&&logger.warn(`db ${e[n]} not found`)}return t.map(e=>({...e.getConfig(),_id:e._id}))}}}}),CourseSyncService,init_CourseSyncService=__esm({"src/impl/couch/CourseSyncService.ts"(){var e;init_pouchdb_setup(),init_couch(),init_logger(),CourseSyncService=(e=class _CourseSyncService{constructor(){_defineProperty(this,`entries`,new Map)}static getInstance(){return _CourseSyncService.instance||(_CourseSyncService.instance=new _CourseSyncService),_CourseSyncService.instance}static resetInstance(){if(_CourseSyncService.instance){for(let[,e]of _CourseSyncService.instance.entries)e.localDB&&e.localDB.close().catch(()=>{});_CourseSyncService.instance.entries.clear()}_CourseSyncService.instance=null}async ensureSynced(e,t){let n=this.entries.get(e);if(n?.status.state===`ready`&&n.localDB){if(!await this.isLocalEpochStale(e,n.localDB))return;logger.info(`[CourseSyncService] Remote DB epoch changed for course ${e} \u2014 destroying stale local replica`);try{await n.localDB.destroy()}catch{}n.localDB=null,n.readyPromise=null}if(n?.status.state===`disabled`)return;if(n?.readyPromise)return n.readyPromise;let r={localDB:null,status:{state:`not-started`},readyPromise:null};return this.entries.set(e,r),r.readyPromise=this.performSync(e,r,t),r.readyPromise}getLocalDB(e){let t=this.entries.get(e);return t?.status.state===`ready`&&t.localDB?t.localDB:null}isReady(e){return this.entries.get(e)?.status.state===`ready`}getStatus(e){return this.entries.get(e)?.status??{state:`not-started`}}async performSync(e,t,n){try{if(!n&&(t.status={state:`checking-config`},!await this.checkLocalSyncEnabled(e))){t.status={state:`disabled`},t.readyPromise=null,logger.debug(`[CourseSyncService] Local sync disabled for course ${e}`);return}t.status={state:`syncing`};let r=this.localDBName(e),a=new pouchdb_setup_default(r);await this.isLocalEpochStale(e,a)&&(logger.info(`[CourseSyncService] Stale local DB detected for course ${e} \u2014 destroying before sync`),await a.destroy(),a=new pouchdb_setup_default(r)),t.localDB=a;let o=this.getRemoteDB(e),s=Date.now();logger.info(`[CourseSyncService] Starting one-shot replication for course ${e}`);let c=await this.replicate(o,a),l=Date.now()-s;logger.info(`[CourseSyncService] Replication complete for course ${e}: ${c.docs_written} docs in ${l}ms`),t.status={state:`warming-views`};let u=Date.now();await this.warmViewIndices(a);let d=Date.now()-u;logger.info(`[CourseSyncService] View indices warmed for course ${e} in ${d}ms`),t.status={state:`ready`,docsReplicated:c.docs_written,syncTimeMs:l,viewWarmTimeMs:d}}catch(n){let r=n instanceof Error?n.message:String(n);if(logger.error(`[CourseSyncService] Sync failed for course ${e}: ${r}`),t.status={state:`error`,error:r},t.readyPromise=null,t.localDB){try{await t.localDB.destroy()}catch{}t.localDB=null}}}async checkLocalSyncEnabled(e){try{return(await this.getRemoteDB(e).get(`CourseConfig`)).localSync?.enabled===!0}catch(t){return logger.warn(`[CourseSyncService] Could not read CourseConfig for ${e}, assuming local sync disabled: ${t}`),!1}}replicate(e,t){return new Promise((n,r)=>{pouchdb_setup_default.replicate(e,t,{}).on(`complete`,e=>{n(e)}).on(`error`,e=>{r(e)})})}async warmViewIndices(e){for(let t of[`elo`,`getTags`])try{await e.query(t,{limit:1}),logger.debug(`[CourseSyncService] Warmed view index: ${t}`)}catch(e){logger.debug(`[CourseSyncService] Could not warm view ${t}: ${e}`)}}async isLocalEpochStale(e,t){try{let n=await this.getRemoteDB(e).get(`db-epoch`),r=null;try{r=await t.get(`db-epoch`)}catch{return!0}return n.epoch!==r.epoch}catch{return!1}}getRemoteDB(e){return getCourseDB2(e)}localDBName(e){return`coursedb-local-${e}`}},_defineProperty(e,`instance`,null),e)}});async function getCurrentSession(){try{if(ENV.COUCHDB_SERVER_URL===NOT_SET||ENV.COUCHDB_SERVER_PROTOCOL===NOT_SET)throw Error(`CouchDB server configuration not properly initialized. Protocol: "${ENV.COUCHDB_SERVER_PROTOCOL}", URL: "${ENV.COUCHDB_SERVER_URL}"`);let e=ENV.COUCHDB_SERVER_URL.endsWith(`/`)?ENV.COUCHDB_SERVER_URL.slice(0,-1):ENV.COUCHDB_SERVER_URL,t=`${ENV.COUCHDB_SERVER_PROTOCOL}://${e}/_session`;logger.debug(`Attempting session check at: ${t}`);let n=await(0,import_browser_ponyfill.default)(t,{method:`GET`,credentials:`include`});if(!n.ok)throw Error(`Session check failed: ${n.status}`);return await n.json()}catch(e){let t=ENV.COUCHDB_SERVER_URL.endsWith(`/`)?ENV.COUCHDB_SERVER_URL.slice(0,-1):ENV.COUCHDB_SERVER_URL,n=`${ENV.COUCHDB_SERVER_PROTOCOL}://${t}/_session`;throw logger.error(`Session check error attempting to connect to: ${n} - ${e}`),Error(`Session check failed connecting to ${n}: ${e}`)}}async function getLoggedInUsername(){let e=await getCurrentSession();if(e.userCtx.name&&e.userCtx.name!==``)return e.userCtx.name;throw Error(`No logged in user`)}var init_auth=__esm({"src/impl/couch/auth.ts"(){init_factory(),init_logger()}}),CouchDBSyncStrategy_exports={};__export(CouchDBSyncStrategy_exports,{CouchDBSyncStrategy:()=>CouchDBSyncStrategy});var log3,CouchDBSyncStrategy,init_CouchDBSyncStrategy=__esm({"src/impl/couch/CouchDBSyncStrategy.ts"(){init_factory(),init_types_legacy(),init_logger(),init_common(),init_pouchdb_setup(),init_couch(),init_auth(),log3=e=>{logger.info(e)},CouchDBSyncStrategy=class{constructor(){_defineProperty(this,`syncHandle`,void 0)}setupRemoteDB(e){return e===GuestUsername||e.startsWith(GuestUsername)?getLocalUserDB(e):this.getUserDB(e)}getWriteDB(e){return e===GuestUsername||e.startsWith(GuestUsername)?getLocalUserDB(e):this.getUserDB(e)}startSync(e,t){e!==t&&(this.syncHandle=pouchdb_setup_default.sync(e,t,{live:!0,retry:!0}))}stopSync(){this.syncHandle&&(this.syncHandle.cancel(),this.syncHandle=void 0)}canCreateAccount(){return!0}canAuthenticate(){return!0}async createAccount(e,t){let n=await this.getCurrentUsername(),r=n.startsWith(GuestUsername);r&&logger.info(`Creating account for funnel user ${n} -> ${e}`);try{let a=await this.getRemoteCouchRootDB().signUp(e,t);if(a.ok){log3(`CREATEACCOUNT: Successfully created account for ${e}`);try{let e=await this.getRemoteCouchRootDB().logOut();log3(`CREATEACCOUNT: logged out: ${e.ok}`)}catch{}let a=await this.getRemoteCouchRootDB().logIn(e,t);if(log3(`CREATEACCOUNT: logged in as new user: ${a.ok}`),a.ok){if(r){logger.info(`Migrating data from funnel account ${n} to ${e}`);let t=await this.migrateFunnelData(n,e);t.success||logger.warn(`Migration failed: ${t.error}`)}return{status:Status.ok,error:void 0}}else return{status:Status.error,error:`Failed to log in after account creation`}}else return logger.warn(`Signup not OK: ${JSON.stringify(a)}`),{status:Status.error,error:`Account creation failed`}}catch(e){return e.reason===`Document update conflict.`?{status:Status.error,error:`This username is taken!`}:(logger.error(`Error on signup: ${JSON.stringify(e)}`),{status:Status.error,error:e.message||`Unknown error during account creation`})}}async authenticate(e,t){try{return(await this.getRemoteCouchRootDB().logIn(e,t)).ok?(log3(`Successfully logged in as ${e}`),{ok:!0}):(log3(`Login failed for ${e}`),{ok:!1,error:`Invalid username or password`})}catch(t){return logger.error(`Authentication error for ${e}:`,t),{ok:!1,error:t.message||`Authentication failed`}}}async logout(){try{let e=await this.getRemoteCouchRootDB().logOut();return{ok:e.ok,error:e.ok?void 0:`Logout failed`}}catch(e){return logger.error(`Logout error:`,e),{ok:!1,error:e.message||`Logout failed`}}}async getCurrentUsername(){logger.log(`[funnel] CouchDBSyncStrategy.getCurrentUsername() called`);try{let e=await getLoggedInUsername();return logger.log(`[funnel] getLoggedInUsername() returned:`,e),e}catch(e){logger.log(`[funnel] getLoggedInUsername() failed, calling accomodateGuest()`),logger.log(`[funnel] Error was:`,e);let t=accomodateGuest();return logger.log(`[funnel] accomodateGuest() returned:`,t),t.username}}async migrateFunnelData(e,t){try{logger.info(`Starting data migration from ${e} to ${t}`);let n=getLocalUserDB(e),r=getLocalUserDB(t),a=await n.allDocs({include_docs:!0});logger.info(`Found ${a.rows.length} documents in funnel account`);let o=a.rows.filter(e=>!e.id.startsWith(`_design/`)).map(e=>({...e.doc,_rev:void 0}));return o.length>0?(await r.bulkDocs(o),logger.info(`Successfully migrated ${o.length} documents from ${e} to ${t}`)):logger.info(`No documents to migrate from funnel account`),{success:!0}}catch(e){return logger.error(`Migration failed:`,e),{success:!1,error:e instanceof Error?e.message:`Unknown error`}}}getRemoteCouchRootDB(){let e=ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+`skuilder`;try{return new pouchdb_setup_default(e,{skip_setup:!0})}catch(e){throw logger.error(`Failed to initialize remote CouchDB connection:`,e),Error(`Failed to initialize CouchDB: ${JSON.stringify(e)}`)}}getUserDB(e){let t=!1,n=`userdb-${hexEncode(e)}`;return log3(`Fetching user database: ${n} (${e})`),new pouchdb_setup_default(ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+n,createPouchDBConfig())}}}});function createPouchDBConfig(){return ENV.COUCHDB_USERNAME&&ENV.COUCHDB_PASSWORD&&typeof window>`u`?{fetch(e,t={}){let n=btoa(`${ENV.COUCHDB_USERNAME}:${ENV.COUCHDB_PASSWORD}`),r=new Headers(t.headers||{});r.set(`Authorization`,`Basic ${n}`);let a={...t,headers:r};return pouchdb_setup_default.fetch(e,a)}}:pouchDBincludeCredentialsConfig}function getCourseDB2(e){return new pouchdb_setup_default(ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+`coursedb-`+e,createPouchDBConfig())}function filterAllDocsByPrefix2(e,t,n){let r={startkey:t,endkey:t+`￰`,include_docs:!0};return n&&Object.assign(r,n),e.allDocs(r)}function getStartAndEndKeys2(e){return{startkey:e,endkey:e+`￰`}}var isBrowser,GUEST_LOCAL_DB,localUserDB,pouchDBincludeCredentialsConfig,REVIEW_TIME_FORMAT2,init_couch=__esm({"src/impl/couch/index.ts"(){init_factory(),init_types_legacy(),init_logger(),init_pouchdb_setup(),init_contentSource(),init_adminDB2(),init_classroomDB2(),init_courseAPI(),init_courseDB(),init_CourseSyncService(),init_CouchDBSyncStrategy(),isBrowser=typeof window<`u`,isBrowser&&(window.process=import_browser.default),GUEST_LOCAL_DB=`userdb-${GuestUsername}`,new pouchdb_setup_default(GUEST_LOCAL_DB),pouchDBincludeCredentialsConfig={fetch(e,t){return t.credentials=`include`,pouchdb_setup_default.fetch(e,t)}},REVIEW_TIME_FORMAT2=`YYYY-MM-DD--kk:mm:ss-SSS`}});function accomodateGuest(){if(logger.log(`[funnel] accomodateGuest() called`),typeof localStorage>`u`)return logger.log(`[funnel] localStorage not available (Node.js environment), returning default guest`),{username:GuestUsername+`nodejs-test`,firstVisit:!0};let e=`sk-guest-uuid`,t,n=localStorage.getItem(e);if(logger.log(`[funnel] Checking localStorage for key:`,e),logger.log(`[funnel] Existing UUID value:`,n),logger.log(`[funnel] existingUUID !== null:`,n!==null),n!==null)t=!1,logger.log(`[funnel] Returning guest ${n} "logging in".`);else{t=!0,logger.log(`[funnel] No existing UUID, generating new one...`);let n=generateUUID();logger.log(`[funnel] Generated UUID:`,n),logger.log(`[funnel] UUID length:`,n.length);try{localStorage.setItem(e,n),logger.log(`[funnel] Successfully stored UUID in localStorage`);let t=localStorage.getItem(e);logger.log(`[funnel] Verification read from localStorage:`,t)}catch(e){logger.error(`[funnel] ERROR storing UUID:`,e)}logger.log(`[funnel] Accommodating a new guest with account: ${n}`)}let r=localStorage.getItem(e),a=GuestUsername+r;return logger.log(`[funnel] Final UUID from localStorage:`,r),logger.log(`[funnel] GuestUsername constant:`,GuestUsername),logger.log(`[funnel] Final username to return:`,a),{username:a,firstVisit:t};function generateUUID(){if(logger.log(`[funnel] Inside generateUUID()`),typeof crypto<`u`&&typeof crypto.randomUUID==`function`){let e=crypto.randomUUID();return logger.log(`[funnel] Generated UUID using crypto.randomUUID():`,e),e}if(typeof crypto<`u`&&typeof crypto.getRandomValues==`function`){let e=new Uint8Array(16);crypto.getRandomValues(e),e[6]=e[6]&15|64,e[8]=e[8]&63|128;let t=[Array.from(e.slice(0,4)).map(e=>e.toString(16).padStart(2,`0`)).join(``),Array.from(e.slice(4,6)).map(e=>e.toString(16).padStart(2,`0`)).join(``),Array.from(e.slice(6,8)).map(e=>e.toString(16).padStart(2,`0`)).join(``),Array.from(e.slice(8,10)).map(e=>e.toString(16).padStart(2,`0`)).join(``),Array.from(e.slice(10,16)).map(e=>e.toString(16).padStart(2,`0`)).join(``)].join(`-`);return logger.log(`[funnel] Generated UUID using crypto.getRandomValues():`,t),t}logger.warn(`[funnel] crypto API not available, using timestamp-based UUID (NOT SECURE)`);let e=new Date().getTime();typeof performance<`u`&&typeof performance.now==`function`&&(e+=performance.now());let t=`xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`.replace(/[xy]/g,t=>{let n=(e+Math.random()*16)%16|0;return e=Math.floor(e/16),(t===`x`?n:n&3|8).toString(16)});return logger.log(`[funnel] Generated UUID (fallback):`,t),t}}async function getOrCreateClassroomRegistrationsDoc(e){let t;try{t=await getLocalUserDB(e).get(userClassroomsDoc)}catch(n){let r=n;if(r.status===404)await getLocalUserDB(e).put({_id:userClassroomsDoc,registrations:[]}),t=await getOrCreateClassroomRegistrationsDoc(e);else{let e={name:r.name,status:r.status,message:r.message,reason:r.reason,error:r.error};throw logger.error(`Database error in getOrCreateClassroomRegistrationsDoc (standalone function):`,e),Error(`Database error accessing classroom registrations: ${r.message||r.name||`Unknown error`} (status: ${r.status})`)}}return t}async function getOrCreateCourseRegistrationsDoc(e){let t;try{t=await getLocalUserDB(e).get(userCoursesDoc)}catch(n){if(n.status===404)await getLocalUserDB(e).put({_id:userCoursesDoc,courses:[],studyWeight:{}}),t=await getOrCreateCourseRegistrationsDoc(e);else throw Error(`Unexpected error ${JSON.stringify(n)} in getOrCreateCourseRegistrationDoc...`)}return t}async function updateUserElo(e,t,n){let r=await getOrCreateCourseRegistrationsDoc(e),a=r.courses.find(e=>e.courseID===t);return a.elo=n,getLocalUserDB(e).put(r)}async function registerUserForClassroom(e,t,n){return log4(`Registering user: ${e} in course: ${t}`),getOrCreateClassroomRegistrationsDoc(e).then(r=>{let a={classID:t,registeredAs:n};return r.registrations.filter(e=>e.classID===a.classID&&e.registeredAs===a.registeredAs).length===0?r.registrations.push(a):log4(`User ${e} is already registered for class ${t}`),getLocalUserDB(e).put(r)})}async function dropUserFromClassroom(e,t){return getOrCreateClassroomRegistrationsDoc(e).then(n=>{let r=-1;for(let e=0;e<n.registrations.length;e++)n.registrations[e].classID===t&&(r=e);return r!==-1&&n.registrations.splice(r,1),getLocalUserDB(e).put(n)})}async function getUserClassrooms(e){return getOrCreateClassroomRegistrationsDoc(e)}var log4,BaseUser,userCoursesDoc,userClassroomsDoc,init_BaseUserDB=__esm({"src/impl/common/BaseUserDB.ts"(){var e;init_core(),init_util(),init_types_legacy(),init_logger(),init_userDBHelpers(),init_updateQueue(),init_user_course_relDB(),init_couch(),log4=e=>{logger.info(e)},BaseUser=(e=class _BaseUser{static Dummy(e){return new _BaseUser(`Me`,e)}getUsername(){return this._username}isLoggedIn(){return!this._username.startsWith(GuestUsername)}remote(){return this.remoteDB}async createAccount(e,t){if(!this.syncStrategy.canCreateAccount())throw Error(`Account creation not supported by current sync strategy`);if(!this._username.startsWith(GuestUsername))throw Error(`Cannot create a new account while logged in:
271
+ ${e.stack}`:JSON.stringify(e);throw logger.error(`[courseDB] Error creating navigator: ${t}`),e}}setEphemeralHints(e){this._pendingHints=e}async getWeightedCards(e){let t=await this._getCurrentUser();try{let n=await this.createNavigator(t);return this._pendingHints&&(n.setEphemeralHints(this._pendingHints),this._pendingHints=null),n.getWeightedCards(e)}catch(e){throw logger.error(`[courseDB] Error getting weighted cards: ${e}`),e}}async getCardsCenteredAtELO(e={limit:99,elo:`user`},t){let n;if(e.elo===`user`){let e=await this._getCurrentUser();n=-1;try{n=EloToNumber((await e.getCourseRegistrationsDoc()).courses.find(e=>e.courseID===this.id).elo)}catch{n=1e3}}else if(e.elo===`random`){let e=await GET_CACHED(`elo-bounds-${this.id}`,()=>this.getELOBounds());n=Math.round(e.low+Math.random()*(e.high-e.low))}else n=e.elo;let r=[],a=4,o=-1,s=0;for(;r.length<e.limit&&s!==o;)r=await this.getCardsByELO(n,a*e.limit),o=s,s=r.length,logger.debug(`Found ${r.length} elo neighbor cards...`),t&&(r=r.filter(t),logger.debug(`Filtered to ${r.length} cards...`)),a*=2;let c=[];for(;c.length<e.limit&&r.length>0;){let e=randIntWeightedTowardZero(r.length),t=r.splice(e,1)[0];c.push(t)}return c.map(e=>({courseID:this.id,cardID:e.cardID,contentSourceType:`course`,contentSourceID:this.id,elo:e.elo,status:`new`}))}async searchCards(e){logger.log(`[CourseDB ${this.id}] Searching for: "${e}"`);let t;try{t=await this.db.find({selector:{docType:`DISPLAYABLE_DATA`,"data.0.data":{$regex:`.*${e}.*`}}}),logger.log(`[CourseDB ${this.id}] Regex search on data[0].data successful`)}catch(n){logger.log(`[CourseDB ${this.id}] Regex search failed, falling back to manual search:`,n);let r=await this.db.find({selector:{docType:`DISPLAYABLE_DATA`}});logger.log(`[CourseDB ${this.id}] Retrieved ${r.docs.length} documents for manual filtering`),t={docs:r.docs.filter(t=>{let n=JSON.stringify(t).toLowerCase().includes(e.toLowerCase());return n&&logger.log(`[CourseDB ${this.id}] Manual match found in document: ${t._id}`),n})}}if(logger.log(`[CourseDB ${this.id}] Found ${t.docs.length} displayable data documents`),t.docs.length===0){let e=await this.db.find({selector:{docType:`DISPLAYABLE_DATA`},limit:5});logger.log(`[CourseDB ${this.id}] Sample displayable data:`,e.docs.map(e=>({id:e._id,docType:e.docType,dataStructure:e.data?Object.keys(e.data):`no data field`,dataContent:e.data,fullDoc:e})))}let n=[];for(let e of t.docs){let t=await this.db.find({selector:{docType:`CARD`,id_displayable_data:{$in:[e._id]}}});logger.log(`[CourseDB ${this.id}] Displayable data ${e._id} linked to ${t.docs.length} cards`),n.push(...t.docs)}return logger.log(`[CourseDB ${this.id}] Total cards found: ${n.length}`),n}async find(e){return this.db.find(e)}}}}),classroomLookupDBTitle,CLASSROOM_CONFIG,ClassroomDBBase,StudentClassroomDB,TeacherClassroomDB,ClassroomLookupDB,init_classroomDB2=__esm({"src/impl/couch/classroomDB.ts"(){init_factory(),init_logger(),init_pouchdb_setup(),init_couch(),init_courseDB(),classroomLookupDBTitle=`classdb-lookup`,CLASSROOM_CONFIG=`ClassroomConfig`,ClassroomDBBase=class{constructor(){_defineProperty(this,`_id`,void 0),_defineProperty(this,`_db`,void 0),_defineProperty(this,`_cfg`,void 0),_defineProperty(this,`_initComplete`,!1),_defineProperty(this,`_content_prefix`,`content`)}get _content_searchkeys(){return getStartAndEndKeys2(this._content_prefix)}async getAssignedContent(){return logger.info(`Getting assigned content...`),(await this._db.allDocs({startkey:this._content_prefix,endkey:this._content_prefix+`￰`,include_docs:!0})).rows.map(e=>e.doc)}getContentId(e){return e.type===`tag`?`${this._content_prefix}-${e.courseID}-${e.tagID}`:`${this._content_prefix}-${e.courseID}`}get ready(){return this._initComplete}getConfig(){return this._cfg}},StudentClassroomDB=class _StudentClassroomDB extends ClassroomDBBase{constructor(e,t){super(),_defineProperty(this,`userMessages`,void 0),_defineProperty(this,`_user`,void 0),this._id=e,this._user=t}async init(){let e=`classdb-student-${this._id}`;this._db=new pouchdb_setup_default(ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+e,createPouchDBConfig());try{this._cfg=await this._db.get(CLASSROOM_CONFIG),this.userMessages=this._db.changes({since:`now`,live:!0,include_docs:!0}),this._initComplete=!0;return}catch(e){throw Error(`Error in StudentClassroomDB constructor: ${JSON.stringify(e)}`)}}static async factory(e,t){let n=new _StudentClassroomDB(e,t);return await n.init(),n}setChangeFcn(e){this.userMessages.on(`change`,e)}async getWeightedCards(e){let t=[],n=(await this._user.getPendingReviews()).filter(e=>e.scheduledFor===`classroom`&&e.schedulingAgentId===this._id);for(let e of n)t.push({cardId:e.cardId,courseId:e.courseId,score:1,reviewID:e._id,provenance:[{strategy:`classroom`,strategyName:`Classroom`,strategyId:`CLASSROOM`,action:`generated`,score:1,reason:`Classroom scheduled review`}]});let r=await this._user.getActiveCards(),a=new Set(r.map(e=>e.cardID)),o=hooks.utc(),s=(await this.getAssignedContent()).filter(e=>o.isAfter(hooks.utc(e.activeOn,REVIEW_TIME_FORMAT2)));logger.info(`[StudentClassroomDB] Due content: ${JSON.stringify(s)}`);for(let n of s)if(n.type===`course`){let{cards:r}=await new CourseDB(n.courseID,async()=>this._user).getWeightedCards(e);for(let e of r)a.has(e.cardId)||t.push({...e,provenance:[...e.provenance,{strategy:`classroom`,strategyName:`Classroom`,strategyId:`CLASSROOM`,action:`passed`,score:e.score,reason:`Assigned via classroom from course ${n.courseID}`}]})}else if(n.type===`tag`){let e=await getTag(n.courseID,n.tagID);for(let r of e.taggedCards)a.has(r)||t.push({cardId:r,courseId:n.courseID,score:1,provenance:[{strategy:`classroom`,strategyName:`Classroom`,strategyId:`CLASSROOM`,action:`generated`,score:1,reason:`Classroom assigned tag: ${n.tagID}, new card`}]})}else n.type===`card`&&(a.has(n.cardID)||t.push({cardId:n.cardID,courseId:n.courseID,score:1,provenance:[{strategy:`classroom`,strategyName:`Classroom`,strategyId:`CLASSROOM`,action:`generated`,score:1,reason:`Classroom assigned card, new card`}]}));return logger.info(`[StudentClassroomDB] New cards from classroom ${this._cfg.name}: ${t.length} total (reviews + new)`),{cards:t.sort((e,t)=>t.score-e.score).slice(0,e)}}},TeacherClassroomDB=class _TeacherClassroomDB extends ClassroomDBBase{constructor(e){super(),_defineProperty(this,`_stuDb`,void 0),this._id=e}async init(){let e=`classdb-teacher-${this._id}`,t=`classdb-student-${this._id}`;this._db=new pouchdb_setup_default(ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+e,createPouchDBConfig()),this._stuDb=new pouchdb_setup_default(ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+t,createPouchDBConfig());try{return this._db.get(CLASSROOM_CONFIG).then(e=>{this._cfg=e,this._initComplete=!0}).then(()=>{})}catch(e){throw Error(`Error in TeacherClassroomDB constructor: ${JSON.stringify(e)}`)}}static async factory(e){let t=new _TeacherClassroomDB(e);return await t.init(),t}async removeContent(e){let t=this.getContentId(e);try{let e=await this._db.get(t);await this._db.remove(e),this._db.replicate.to(this._stuDb,{doc_ids:[t]})}catch(e){logger.error(`Failed to remove content:`,t,e)}}async assignContent(e){let t,n=this.getContentId(e);return t=e.type===`tag`?await this._db.put({courseID:e.courseID,tagID:e.tagID,type:`tag`,_id:n,assignedBy:e.assignedBy,assignedOn:hooks.utc(),activeOn:e.activeOn||hooks.utc()}):await this._db.put({courseID:e.courseID,type:`course`,_id:n,assignedBy:e.assignedBy,assignedOn:hooks.utc(),activeOn:e.activeOn||hooks.utc()}),t.ok?(this._db.replicate.to(this._stuDb,{doc_ids:[n]}),!0):!1}},ClassroomLookupDB=()=>new pouchdb_setup_default(ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+classroomLookupDBTitle,{skip_setup:!0})}}),AdminDB,init_adminDB2=__esm({"src/impl/couch/adminDB.ts"(){init_pouchdb_setup(),init_factory(),init_couch(),init_classroomDB2(),init_courseLookupDB(),init_logger(),AdminDB=class{constructor(){_defineProperty(this,`usersDB`,void 0),this.usersDB=new pouchdb_setup_default(ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+`_users`,createPouchDBConfig())}async getUsers(){return(await this.usersDB.allDocs({include_docs:!0,...getStartAndEndKeys2(`org.couchdb.user:`)})).rows.map(e=>e.doc)}async getCourses(){let e=await CourseLookup.allCourseWare();return await Promise.all(e.map(e=>getCredentialledCourseConfig(e._id)))}async removeCourse(e){let t=await CourseLookup.delete(e),n=await getCredentialledCourseConfig(e);n.deleted=!0;let r=await updateCredentialledCourseConfig(e,n);return{ok:t.ok&&r.ok,id:t.id,rev:t.rev}}async getClassrooms(){let e=(await ClassroomLookupDB().allDocs({include_docs:!0})).rows.map(e=>e.doc.uuid);logger.debug(e.join(`, `));let t=[];for(let n=0;n<e.length;n++)try{let r=await TeacherClassroomDB.factory(e[n]);t.push(r)}catch(t){let r=t;r.error&&r.error===`not_found`&&logger.warn(`db ${e[n]} not found`)}return t.map(e=>({...e.getConfig(),_id:e._id}))}}}}),CourseSyncService_exports={};__export(CourseSyncService_exports,{CourseSyncService:()=>CourseSyncService});var DEFAULT_REPLICATION,CourseSyncService,init_CourseSyncService=__esm({"src/impl/couch/CourseSyncService.ts"(){var e;init_pouchdb_setup(),init_couch(),init_logger(),DEFAULT_REPLICATION={batchSize:1e3,batchesLimit:5},CourseSyncService=(e=class _CourseSyncService{constructor(){_defineProperty(this,`entries`,new Map),_defineProperty(this,`replicationOptions`,DEFAULT_REPLICATION)}static getInstance(){return _CourseSyncService.instance||(_CourseSyncService.instance=new _CourseSyncService),_CourseSyncService.instance}configure(e){e.replication&&(this.replicationOptions={...DEFAULT_REPLICATION,...e.replication},logger.info(`[CourseSyncService] Replication configured: batch_size=${this.replicationOptions.batchSize}, batches_limit=${this.replicationOptions.batchesLimit}`))}static resetInstance(){if(_CourseSyncService.instance){for(let[,e]of _CourseSyncService.instance.entries)e.localDB&&e.localDB.close().catch(()=>{});_CourseSyncService.instance.entries.clear()}_CourseSyncService.instance=null}async ensureSynced(e,t){let n=this.entries.get(e);if(n?.status.state===`ready`&&n.localDB){if(!await this.isLocalEpochStale(e,n.localDB))return;logger.info(`[CourseSyncService] Remote DB epoch changed for course ${e} \u2014 destroying stale local replica`);try{await n.localDB.destroy()}catch{}n.localDB=null,n.readyPromise=null}if(n?.status.state===`disabled`)return;if(n?.readyPromise)return n.readyPromise;let r={localDB:null,status:{state:`not-started`},readyPromise:null};return this.entries.set(e,r),r.readyPromise=this.performSync(e,r,t),r.readyPromise}getLocalDB(e){let t=this.entries.get(e);return t?.status.state===`ready`&&t.localDB?t.localDB:null}isReady(e){return this.entries.get(e)?.status.state===`ready`}getStatus(e){return this.entries.get(e)?.status??{state:`not-started`}}async performSync(e,t,n){try{if(!n&&(t.status={state:`checking-config`},!await this.checkLocalSyncEnabled(e))){t.status={state:`disabled`},t.readyPromise=null,logger.debug(`[CourseSyncService] Local sync disabled for course ${e}`);return}t.status={state:`syncing`};let r=this.localDBName(e),a=new pouchdb_setup_default(r);await this.isLocalEpochStale(e,a)&&(logger.info(`[CourseSyncService] Stale local DB detected for course ${e} \u2014 destroying before sync`),await a.destroy(),a=new pouchdb_setup_default(r)),t.localDB=a;let o=this.getRemoteDB(e),s=Date.now();logger.info(`[CourseSyncService] Starting one-shot replication for course ${e} (batch_size=${this.replicationOptions.batchSize}, batches_limit=${this.replicationOptions.batchesLimit})`);let c=await this.replicate(o,a),l=Date.now()-s;logger.info(`[CourseSyncService] Replication complete for course ${e}: ${c.docs_written} docs in ${l}ms`),t.status={state:`warming-views`};let u=Date.now();await this.warmViewIndices(a);let d=Date.now()-u;logger.info(`[CourseSyncService] View indices warmed for course ${e} in ${d}ms`),t.status={state:`ready`,docsReplicated:c.docs_written,syncTimeMs:l,viewWarmTimeMs:d}}catch(n){let r=n instanceof Error?n.message:String(n);if(logger.error(`[CourseSyncService] Sync failed for course ${e}: ${r}`),t.status={state:`error`,error:r},t.readyPromise=null,t.localDB){try{await t.localDB.destroy()}catch{}t.localDB=null}}}async checkLocalSyncEnabled(e){try{return(await this.getRemoteDB(e).get(`CourseConfig`)).localSync?.enabled===!0}catch(t){return logger.warn(`[CourseSyncService] Could not read CourseConfig for ${e}, assuming local sync disabled: ${t}`),!1}}replicate(e,t){return new Promise((n,r)=>{pouchdb_setup_default.replicate(e,t,{batch_size:this.replicationOptions.batchSize,batches_limit:this.replicationOptions.batchesLimit}).on(`complete`,e=>{n(e)}).on(`error`,e=>{r(e)})})}async warmViewIndices(e){for(let t of[`elo`,`getTags`])try{await e.query(t,{limit:1}),logger.debug(`[CourseSyncService] Warmed view index: ${t}`)}catch(e){logger.debug(`[CourseSyncService] Could not warm view ${t}: ${e}`)}}async isLocalEpochStale(e,t){try{let n=await this.getRemoteDB(e).get(`db-epoch`),r=null;try{r=await t.get(`db-epoch`)}catch{return!0}return n.epoch!==r.epoch}catch{return!1}}getRemoteDB(e){return getCourseDB2(e)}localDBName(e){return`coursedb-local-${e}`}},_defineProperty(e,`instance`,null),e)}});async function getCurrentSession(){try{if(ENV.COUCHDB_SERVER_URL===NOT_SET||ENV.COUCHDB_SERVER_PROTOCOL===NOT_SET)throw Error(`CouchDB server configuration not properly initialized. Protocol: "${ENV.COUCHDB_SERVER_PROTOCOL}", URL: "${ENV.COUCHDB_SERVER_URL}"`);let e=ENV.COUCHDB_SERVER_URL.endsWith(`/`)?ENV.COUCHDB_SERVER_URL.slice(0,-1):ENV.COUCHDB_SERVER_URL,t=`${ENV.COUCHDB_SERVER_PROTOCOL}://${e}/_session`;logger.debug(`Attempting session check at: ${t}`);let n=await(0,import_browser_ponyfill.default)(t,{method:`GET`,credentials:`include`});if(!n.ok)throw Error(`Session check failed: ${n.status}`);return await n.json()}catch(e){let t=ENV.COUCHDB_SERVER_URL.endsWith(`/`)?ENV.COUCHDB_SERVER_URL.slice(0,-1):ENV.COUCHDB_SERVER_URL,n=`${ENV.COUCHDB_SERVER_PROTOCOL}://${t}/_session`;throw logger.error(`Session check error attempting to connect to: ${n} - ${e}`),Error(`Session check failed connecting to ${n}: ${e}`)}}async function getLoggedInUsername(){let e=await getCurrentSession();if(e.userCtx.name&&e.userCtx.name!==``)return e.userCtx.name;throw Error(`No logged in user`)}var init_auth=__esm({"src/impl/couch/auth.ts"(){init_factory(),init_logger()}}),CouchDBSyncStrategy_exports={};__export(CouchDBSyncStrategy_exports,{CouchDBSyncStrategy:()=>CouchDBSyncStrategy});var log3,CouchDBSyncStrategy,init_CouchDBSyncStrategy=__esm({"src/impl/couch/CouchDBSyncStrategy.ts"(){init_factory(),init_types_legacy(),init_logger(),init_common(),init_pouchdb_setup(),init_couch(),init_auth(),log3=e=>{logger.info(e)},CouchDBSyncStrategy=class{constructor(){_defineProperty(this,`syncHandle`,void 0)}setupRemoteDB(e){return e===GuestUsername||e.startsWith(GuestUsername)?getLocalUserDB(e):this.getUserDB(e)}getWriteDB(e){return e===GuestUsername||e.startsWith(GuestUsername)?getLocalUserDB(e):this.getUserDB(e)}startSync(e,t){e!==t&&(this.syncHandle=pouchdb_setup_default.sync(e,t,{live:!0,retry:!0}))}stopSync(){this.syncHandle&&(this.syncHandle.cancel(),this.syncHandle=void 0)}canCreateAccount(){return!0}canAuthenticate(){return!0}async createAccount(e,t){let n=await this.getCurrentUsername(),r=n.startsWith(GuestUsername);r&&logger.info(`Creating account for funnel user ${n} -> ${e}`);try{let a=await this.getRemoteCouchRootDB().signUp(e,t);if(a.ok){log3(`CREATEACCOUNT: Successfully created account for ${e}`);try{let e=await this.getRemoteCouchRootDB().logOut();log3(`CREATEACCOUNT: logged out: ${e.ok}`)}catch{}let a=await this.getRemoteCouchRootDB().logIn(e,t);if(log3(`CREATEACCOUNT: logged in as new user: ${a.ok}`),a.ok){if(r){logger.info(`Migrating data from funnel account ${n} to ${e}`);let t=await this.migrateFunnelData(n,e);t.success||logger.warn(`Migration failed: ${t.error}`)}return{status:Status.ok,error:void 0}}else return{status:Status.error,error:`Failed to log in after account creation`}}else return logger.warn(`Signup not OK: ${JSON.stringify(a)}`),{status:Status.error,error:`Account creation failed`}}catch(e){return e.reason===`Document update conflict.`?{status:Status.error,error:`This username is taken!`}:(logger.error(`Error on signup: ${JSON.stringify(e)}`),{status:Status.error,error:e.message||`Unknown error during account creation`})}}async authenticate(e,t){try{return(await this.getRemoteCouchRootDB().logIn(e,t)).ok?(log3(`Successfully logged in as ${e}`),{ok:!0}):(log3(`Login failed for ${e}`),{ok:!1,error:`Invalid username or password`})}catch(t){return logger.error(`Authentication error for ${e}:`,t),{ok:!1,error:t.message||`Authentication failed`}}}async logout(){try{let e=await this.getRemoteCouchRootDB().logOut();return{ok:e.ok,error:e.ok?void 0:`Logout failed`}}catch(e){return logger.error(`Logout error:`,e),{ok:!1,error:e.message||`Logout failed`}}}async getCurrentUsername(){logger.log(`[funnel] CouchDBSyncStrategy.getCurrentUsername() called`);try{let e=await getLoggedInUsername();return logger.log(`[funnel] getLoggedInUsername() returned:`,e),e}catch(e){logger.log(`[funnel] getLoggedInUsername() failed, calling accomodateGuest()`),logger.log(`[funnel] Error was:`,e);let t=accomodateGuest();return logger.log(`[funnel] accomodateGuest() returned:`,t),t.username}}async migrateFunnelData(e,t){try{logger.info(`Starting data migration from ${e} to ${t}`);let n=getLocalUserDB(e),r=getLocalUserDB(t),a=await n.allDocs({include_docs:!0});logger.info(`Found ${a.rows.length} documents in funnel account`);let o=a.rows.filter(e=>!e.id.startsWith(`_design/`)).map(e=>({...e.doc,_rev:void 0}));return o.length>0?(await r.bulkDocs(o),logger.info(`Successfully migrated ${o.length} documents from ${e} to ${t}`)):logger.info(`No documents to migrate from funnel account`),{success:!0}}catch(e){return logger.error(`Migration failed:`,e),{success:!1,error:e instanceof Error?e.message:`Unknown error`}}}getRemoteCouchRootDB(){let e=ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+`skuilder`;try{return new pouchdb_setup_default(e,{skip_setup:!0})}catch(e){throw logger.error(`Failed to initialize remote CouchDB connection:`,e),Error(`Failed to initialize CouchDB: ${JSON.stringify(e)}`)}}getUserDB(e){let t=!1,n=`userdb-${hexEncode(e)}`;return log3(`Fetching user database: ${n} (${e})`),new pouchdb_setup_default(ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+n,createPouchDBConfig())}}}});function createPouchDBConfig(){return ENV.COUCHDB_USERNAME&&ENV.COUCHDB_PASSWORD&&typeof window>`u`?{fetch(e,t={}){let n=btoa(`${ENV.COUCHDB_USERNAME}:${ENV.COUCHDB_PASSWORD}`),r=new Headers(t.headers||{});r.set(`Authorization`,`Basic ${n}`);let a={...t,headers:r};return pouchdb_setup_default.fetch(e,a)}}:pouchDBincludeCredentialsConfig}function getCourseDB2(e){return new pouchdb_setup_default(ENV.COUCHDB_SERVER_PROTOCOL+`://`+ENV.COUCHDB_SERVER_URL+`coursedb-`+e,createPouchDBConfig())}function filterAllDocsByPrefix2(e,t,n){let r={startkey:t,endkey:t+`￰`,include_docs:!0};return n&&Object.assign(r,n),e.allDocs(r)}function getStartAndEndKeys2(e){return{startkey:e,endkey:e+`￰`}}var isBrowser,GUEST_LOCAL_DB,localUserDB,pouchDBincludeCredentialsConfig,REVIEW_TIME_FORMAT2,init_couch=__esm({"src/impl/couch/index.ts"(){init_factory(),init_types_legacy(),init_logger(),init_pouchdb_setup(),init_contentSource(),init_adminDB2(),init_classroomDB2(),init_courseAPI(),init_courseDB(),init_CourseSyncService(),init_CouchDBSyncStrategy(),isBrowser=typeof window<`u`,isBrowser&&(window.process=import_browser.default),GUEST_LOCAL_DB=`userdb-${GuestUsername}`,new pouchdb_setup_default(GUEST_LOCAL_DB),pouchDBincludeCredentialsConfig={fetch(e,t){return t.credentials=`include`,pouchdb_setup_default.fetch(e,t)}},REVIEW_TIME_FORMAT2=`YYYY-MM-DD--kk:mm:ss-SSS`}});function accomodateGuest(){if(logger.log(`[funnel] accomodateGuest() called`),typeof localStorage>`u`)return logger.log(`[funnel] localStorage not available (Node.js environment), returning default guest`),{username:GuestUsername+`nodejs-test`,firstVisit:!0};let e=`sk-guest-uuid`,t,n=localStorage.getItem(e);if(logger.log(`[funnel] Checking localStorage for key:`,e),logger.log(`[funnel] Existing UUID value:`,n),logger.log(`[funnel] existingUUID !== null:`,n!==null),n!==null)t=!1,logger.log(`[funnel] Returning guest ${n} "logging in".`);else{t=!0,logger.log(`[funnel] No existing UUID, generating new one...`);let n=generateUUID();logger.log(`[funnel] Generated UUID:`,n),logger.log(`[funnel] UUID length:`,n.length);try{localStorage.setItem(e,n),logger.log(`[funnel] Successfully stored UUID in localStorage`);let t=localStorage.getItem(e);logger.log(`[funnel] Verification read from localStorage:`,t)}catch(e){logger.error(`[funnel] ERROR storing UUID:`,e)}logger.log(`[funnel] Accommodating a new guest with account: ${n}`)}let r=localStorage.getItem(e),a=GuestUsername+r;return logger.log(`[funnel] Final UUID from localStorage:`,r),logger.log(`[funnel] GuestUsername constant:`,GuestUsername),logger.log(`[funnel] Final username to return:`,a),{username:a,firstVisit:t};function generateUUID(){if(logger.log(`[funnel] Inside generateUUID()`),typeof crypto<`u`&&typeof crypto.randomUUID==`function`){let e=crypto.randomUUID();return logger.log(`[funnel] Generated UUID using crypto.randomUUID():`,e),e}if(typeof crypto<`u`&&typeof crypto.getRandomValues==`function`){let e=new Uint8Array(16);crypto.getRandomValues(e),e[6]=e[6]&15|64,e[8]=e[8]&63|128;let t=[Array.from(e.slice(0,4)).map(e=>e.toString(16).padStart(2,`0`)).join(``),Array.from(e.slice(4,6)).map(e=>e.toString(16).padStart(2,`0`)).join(``),Array.from(e.slice(6,8)).map(e=>e.toString(16).padStart(2,`0`)).join(``),Array.from(e.slice(8,10)).map(e=>e.toString(16).padStart(2,`0`)).join(``),Array.from(e.slice(10,16)).map(e=>e.toString(16).padStart(2,`0`)).join(``)].join(`-`);return logger.log(`[funnel] Generated UUID using crypto.getRandomValues():`,t),t}logger.warn(`[funnel] crypto API not available, using timestamp-based UUID (NOT SECURE)`);let e=new Date().getTime();typeof performance<`u`&&typeof performance.now==`function`&&(e+=performance.now());let t=`xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`.replace(/[xy]/g,t=>{let n=(e+Math.random()*16)%16|0;return e=Math.floor(e/16),(t===`x`?n:n&3|8).toString(16)});return logger.log(`[funnel] Generated UUID (fallback):`,t),t}}async function getOrCreateClassroomRegistrationsDoc(e){let t;try{t=await getLocalUserDB(e).get(userClassroomsDoc)}catch(n){let r=n;if(r.status===404)await getLocalUserDB(e).put({_id:userClassroomsDoc,registrations:[]}),t=await getOrCreateClassroomRegistrationsDoc(e);else{let e={name:r.name,status:r.status,message:r.message,reason:r.reason,error:r.error};throw logger.error(`Database error in getOrCreateClassroomRegistrationsDoc (standalone function):`,e),Error(`Database error accessing classroom registrations: ${r.message||r.name||`Unknown error`} (status: ${r.status})`)}}return t}async function getOrCreateCourseRegistrationsDoc(e){let t;try{t=await getLocalUserDB(e).get(userCoursesDoc)}catch(n){if(n.status===404)await getLocalUserDB(e).put({_id:userCoursesDoc,courses:[],studyWeight:{}}),t=await getOrCreateCourseRegistrationsDoc(e);else throw Error(`Unexpected error ${JSON.stringify(n)} in getOrCreateCourseRegistrationDoc...`)}return t}async function updateUserElo(e,t,n){let r=await getOrCreateCourseRegistrationsDoc(e),a=r.courses.find(e=>e.courseID===t);return a.elo=n,getLocalUserDB(e).put(r)}async function registerUserForClassroom(e,t,n){return log4(`Registering user: ${e} in course: ${t}`),getOrCreateClassroomRegistrationsDoc(e).then(r=>{let a={classID:t,registeredAs:n};return r.registrations.filter(e=>e.classID===a.classID&&e.registeredAs===a.registeredAs).length===0?r.registrations.push(a):log4(`User ${e} is already registered for class ${t}`),getLocalUserDB(e).put(r)})}async function dropUserFromClassroom(e,t){return getOrCreateClassroomRegistrationsDoc(e).then(n=>{let r=-1;for(let e=0;e<n.registrations.length;e++)n.registrations[e].classID===t&&(r=e);return r!==-1&&n.registrations.splice(r,1),getLocalUserDB(e).put(n)})}async function getUserClassrooms(e){return getOrCreateClassroomRegistrationsDoc(e)}var log4,BaseUser,userCoursesDoc,userClassroomsDoc,init_BaseUserDB=__esm({"src/impl/common/BaseUserDB.ts"(){var e;init_core(),init_util(),init_types_legacy(),init_logger(),init_userDBHelpers(),init_updateQueue(),init_user_course_relDB(),init_couch(),log4=e=>{logger.info(e)},BaseUser=(e=class _BaseUser{static Dummy(e){return new _BaseUser(`Me`,e)}getUsername(){return this._username}isLoggedIn(){return!this._username.startsWith(GuestUsername)}remote(){return this.remoteDB}async createAccount(e,t){if(!this.syncStrategy.canCreateAccount())throw Error(`Account creation not supported by current sync strategy`);if(!this._username.startsWith(GuestUsername))throw Error(`Cannot create a new account while logged in:
272
272
  Currently logged-in as ${this._username}.`);let n=await this.syncStrategy.createAccount(e,t);if(n.status===Status.ok){log4(`Account created successfully, updating username to ${e}`),this._username=e;try{localStorage.removeItem(`sk-guest-uuid`)}catch(e){logger.warn(`localStorage not available (Node.js environment):`,e)}await this.init()}return{status:n.status,error:n.error||``}}async login(e,t){if(!this.syncStrategy.canAuthenticate())throw Error(`Authentication not supported by current sync strategy`);if(!this._username.startsWith(GuestUsername)&&this._username!=e){if(this._username!=e)throw Error(`Cannot change accounts while logged in.
273
273
  Log out of account ${this.getUsername()} before logging in as ${e}.`);logger.warn(`User ${this._username} is already logged in, but executing login again.`)}let n=await this.syncStrategy.authenticate(e,t);if(n.ok){log4(`Logged in as ${e}`),this._username=e;try{localStorage.removeItem(`sk-guest-uuid`)}catch(e){logger.warn(`localStorage not available (Node.js environment):`,e)}await this.init()}return n}async resetUserData(){if(this.syncStrategy.canAuthenticate())return{status:Status.error,error:`Reset user data is only available for local-only mode. Use logout instead for remote sync.`};try{let e=getLocalUserDB(this._username),t=(await e.allDocs({include_docs:!1})).rows.filter(e=>{let t=e.id;return t.startsWith(DocTypePrefixes.CARDRECORD)||t.startsWith(DocTypePrefixes.SCHEDULED_CARD)||t.startsWith(DocTypePrefixes.STRATEGY_STATE)||t.startsWith(DocTypePrefixes.USER_OUTCOME)||t.startsWith(DocTypePrefixes.STRATEGY_LEARNING_STATE)||t===_BaseUser.DOC_IDS.COURSE_REGISTRATIONS||t===_BaseUser.DOC_IDS.CLASSROOM_REGISTRATIONS||t===_BaseUser.DOC_IDS.CONFIG}).map(e=>({_id:e.id,_rev:e.value.rev,_deleted:!0}));return t.length>0&&await e.bulkDocs(t),await this.init(),{status:Status.ok}}catch(e){return logger.error(`Failed to reset user data:`,e),{status:Status.error,error:e instanceof Error?e.message:`Unknown error during reset`}}}async logout(){if(!this.syncStrategy.canAuthenticate())return this._username=await this.syncStrategy.getCurrentUsername(),await this.init(),{ok:!0};let e=await this.syncStrategy.logout();return this._username=await this.syncStrategy.getCurrentUsername(),await this.init(),e}async get(e){return this.localDB.get(e)}update(e,t){return this.updateQueue.update(e,t)}async getCourseRegistrationsDoc(){logger.debug(`Fetching courseRegistrations for ${this.getUsername()}`);let e;try{return await this.localDB.get(_BaseUser.DOC_IDS.COURSE_REGISTRATIONS)}catch(t){if(t.status===404)await this.localDB.put({_id:_BaseUser.DOC_IDS.COURSE_REGISTRATIONS,courses:[],studyWeight:{}}),e=await this.getCourseRegistrationsDoc();else throw Error(`Unexpected error ${JSON.stringify(t)} in getOrCreateCourseRegistrationDoc...`)}return e}async getActiveCourses(){return(await this.getCourseRegistrationsDoc()).courses.filter(e=>e.status===void 0||e.status===`active`)}async getActiveCards(){let e=getStartAndEndKeys(DocTypePrefixes.SCHEDULED_CARD);return(await this.remoteDB.allDocs({startkey:e.startkey,endkey:e.endkey,include_docs:!0})).rows.map(e=>({courseID:e.doc.courseId,cardID:e.doc.cardId}))}async getActivityRecords(){try{let e=await this.getHistory(),t=[];if(!Array.isArray(e))return logger.error(`getHistory did not return an array:`,e),t;let n=0;for(let r=0;r<e.length;r++)try{e[r]&&Array.isArray(e[r].records)&&e[r].records.forEach(e=>{try{if(!e.timeStamp)return;let r;if(typeof e.timeStamp==`object`)if(typeof e.timeStamp.toDate==`function`)r=e.timeStamp.toISOString();else if(e.timeStamp instanceof Date)r=e.timeStamp.toISOString();else{n<3&&(logger.warn(`Unknown timestamp object type:`,e.timeStamp),n++);return}else if(typeof e.timeStamp==`string`){let t=new Date(e.timeStamp);if(isNaN(t.getTime()))return;r=e.timeStamp}else if(typeof e.timeStamp==`number`)r=new Date(e.timeStamp).toISOString();else return;t.push({timeStamp:r,courseID:e.courseID||`unknown`,cardID:e.cardID||`unknown`,timeSpent:e.timeSpent||0,type:`card_view`})}catch{}})}catch(e){logger.error(`Error processing history item:`,e)}return logger.debug(`Found ${t.length} activity records`),t}catch(e){return logger.error(`Error in getActivityRecords:`,e),[]}}async getReviewstoDate(e,t){let n=getStartAndEndKeys(DocTypePrefixes.SCHEDULED_CARD),r=await this.remoteDB.allDocs({startkey:n.startkey,endkey:n.endkey,include_docs:!0});return log4(`Fetching ${this._username}'s scheduled reviews${t?` for course ${t}`:``}.`),r.rows.filter(n=>{if(n.id.startsWith(DocTypePrefixes.SCHEDULED_CARD)){let r=hooks.utc(n.id.substr(DocTypePrefixes.SCHEDULED_CARD.length),REVIEW_TIME_FORMAT);if(e.isAfter(r)&&(t===void 0||n.doc.courseId===t))return!0}}).map(e=>e.doc)}async getReviewsForcast(e){let t=hooks.utc().add(e,`days`);return this.getReviewstoDate(t)}async getPendingReviews(e){let t=hooks.utc();return this.getReviewstoDate(t,e)}async getScheduledReviewCount(e){return(await this.getPendingReviews(e)).length}async getRegisteredCourses(){return(await this.getCourseRegistrationsDoc()).courses.filter(e=>!e.status||e.status===`active`||e.status===`maintenance-mode`)}async getCourseRegDoc(e){let t=(await this.getCourseRegistrationsDoc()).courses.find(t=>t.courseID===e);if(t)return t;throw Error(`Course registration not found for course ID: ${e}`)}async registerForCourse(e,t=!1){return this.getCourseRegistrationsDoc().then(n=>{let r=t?`preview`:`active`;logger.debug(`Registering for ${e} with status: ${r}`);let a={status:r,courseID:e,user:!0,admin:!1,moderator:!1,elo:{global:{score:1e3,count:0},tags:{},misc:{}}};return n.courses.filter(e=>e.courseID===a.courseID).length===0?(log4(`It's a new course registration!`),n.courses.push(a),n.studyWeight[e]=1):n.courses.forEach(t=>{log4(`Found the previously registered course!`),t.courseID===e&&(t.status=r)}),this.localDB.put(n)}).catch(e=>{throw log4(`Registration failed because of: ${JSON.stringify(e)}`),e})}async dropCourse(e,t=`dropped`){return this.getCourseRegistrationsDoc().then(n=>{let r=-1;for(let t=0;t<n.courses.length;t++)n.courses[t].courseID===e&&(r=t);if(r!==-1)delete n.studyWeight[e],n.courses[r].status=t;else throw Error(`User ${this.getUsername()} is not currently registered for course ${e}`);return this.localDB.put(n)})}async getCourseInterface(e){return new UsrCrsData(this,e)}async getUserEditableCourses(){let e=[],t=await this.getCourseRegistrationsDoc();return e=e.concat(t.courses.map(e=>e.courseID)),await Promise.all(e.map(async e=>await getCredentialledCourseConfig(e)))}async getConfig(){let e={_id:_BaseUser.DOC_IDS.CONFIG,darkMode:!1,likesConfetti:!1,sessionTimeLimit:5};try{let e=await this.localDB.get(_BaseUser.DOC_IDS.CONFIG);return logger.debug(`Raw config from DB:`,e),e}catch(t){let n=t;if(n.name&&n.name===`not_found`)return await this.localDB.put(e),this.getConfig();throw logger.error(`Error setting user default config:`,t),Error(`Error returning the user's configuration: ${JSON.stringify(t)}`)}}async setConfig(e){logger.debug(`Setting Config items ${JSON.stringify(e)}`);let t=await this.getConfig(),n=await this.localDB.put({...t,...e});n.ok?logger.debug(`Config items set: ${JSON.stringify(e)}`):logger.error(`Error setting config items: ${JSON.stringify(n)}`)}static async instance(e,t){return t?(_BaseUser._instance=new _BaseUser(t,e),await _BaseUser._instance.init(),_BaseUser._instance):_BaseUser._instance&&_BaseUser._initialized?_BaseUser._instance:_BaseUser._instance?new Promise(e=>{(function waitForUser(){if(_BaseUser._initialized)return e(_BaseUser._instance);setTimeout(waitForUser,50)})()}):(_BaseUser._instance=new _BaseUser(await e.getCurrentUsername(),e),await _BaseUser._instance.init(),_BaseUser._instance)}constructor(e,t){_defineProperty(this,`_username`,void 0),_defineProperty(this,`syncStrategy`,void 0),_defineProperty(this,`localDB`,void 0),_defineProperty(this,`remoteDB`,void 0),_defineProperty(this,`writeDB`,void 0),_defineProperty(this,`updateQueue`,void 0),_BaseUser._initialized=!1,this._username=e,this.syncStrategy=t,this.setDBandQ()}setDBandQ(){this.localDB=getLocalUserDB(this._username),this.remoteDB=this.syncStrategy.setupRemoteDB(this._username),this.writeDB=this.syncStrategy.getWriteDB?this.syncStrategy.getWriteDB(this._username):this.localDB,this.updateQueue=new UpdateQueue(this.localDB,this.writeDB)}async init(){if(_BaseUser._initialized=!1,this._username===`admin`){_BaseUser._initialized=!0;return}this.setDBandQ(),this.syncStrategy.startSync(this.localDB,this.remoteDB),this.applyDesignDocs().catch(e=>{log4(`Error in applyDesignDocs background task: ${e}`),e&&typeof e==`object`&&log4(`Full error details in applyDesignDocs: ${JSON.stringify(e)}`)}),this.deduplicateReviews().catch(e=>{log4(`Error in deduplicateReviews background task: ${e}`),e&&typeof e==`object`&&log4(`Full error details in background task: ${JSON.stringify(e)}`)}),_BaseUser._initialized=!0}async applyDesignDocs(){if(log4(`Starting applyDesignDocs for user: ${this._username}`),log4(`Remote DB name: ${this.remoteDB.name||`unknown`}`),this._username===`admin`){log4(`Skipping design docs for admin user`);return}log4(`Applying ${_BaseUser.designDocs.length} design docs`);for(let e of _BaseUser.designDocs){log4(`Applying design doc: ${e._id}`);try{try{let t=await this.remoteDB.get(e._id);await this.remoteDB.put({...e,_rev:t._rev})}catch(t){if(t?.name===`not_found`)await this.remoteDB.put(e);else throw t}}catch(t){if(t.name&&t.name===`conflict`)logger.warn(`Design doc ${e._id} update conflict - will retry`),await new Promise(e=>setTimeout(e,1e3)),await this.applyDesignDoc(e);else throw logger.error(`Failed to apply design doc ${e._id}:`,t),t}}}async applyDesignDoc(e,t=3){try{let t=await this.remoteDB.get(e._id);await this.remoteDB.put({...e,_rev:t._rev})}catch(n){if(n?.name===`conflict`&&t>0)return await new Promise(e=>setTimeout(e,1e3)),this.applyDesignDoc(e,t-1);throw n}}async putCardRecord(e){let t=getCardHistoryID(e.courseID,e.cardID);e.timeStamp=hooks.utc(e.timeStamp).toString();try{let n=await this.update(t,function(t){return t.records.push(e),t.bestInterval=t.bestInterval||0,t.lapses=t.lapses||0,t.streak=t.streak||0,t});return n.records=n.records.map(e=>{let t={...e};return t.timeStamp=hooks.utc(e.timeStamp),t}),n}catch(n){let r=n;if(r.status===404)try{let n={_id:t,cardID:e.cardID,courseID:e.courseID,records:[e],lapses:0,streak:0,bestInterval:0},r=await this.writeDB.put(n);return{...n,_rev:r.rev}}catch(e){throw Error(`Failed to create CardHistory for ${t}. Reason: ${e}`)}else throw Error(`putCardRecord failed because of:
274
274
  name:${r.name}
@@ -278,7 +278,7 @@ Currently logged-in as ${this._username}.`);let n=await this.syncStrategy.create
278
278
  if (doc._id && doc._id.indexOf('card_review') === 0 && doc.courseId && doc.cardId) {
279
279
  emit(doc._id, doc.courseId + '-' + doc.cardId);
280
280
  }
281
- }`}}}]),e),userCoursesDoc=`CourseRegistrations`,userClassroomsDoc=`ClassroomRegistrations`}}),init_common=__esm({"src/impl/common/index.ts"(){init_SyncStrategy(),init_BaseUserDB(),init_userDBHelpers()}}),PouchDataLayerProvider_exports={};__export(PouchDataLayerProvider_exports,{CouchDataLayerProvider:()=>CouchDataLayerProvider});var CouchDataLayerProvider,init_PouchDataLayerProvider=__esm({"src/impl/couch/PouchDataLayerProvider.ts"(){init_logger(),init_dataDirectory(),init_auth(),init_adminDB2(),init_classroomDB2(),init_courseDB(),init_CourseSyncService(),init_common(),init_CouchDBSyncStrategy(),CouchDataLayerProvider=class{constructor(e){_defineProperty(this,`initialized`,!1),_defineProperty(this,`userDB`,void 0),_defineProperty(this,`currentUsername`,``),_defineProperty(this,`_courseIDs`,[]),e&&(this._courseIDs=e)}async initialize(){if(!this.initialized){if(typeof process<`u`&&process.versions!=null&&process.versions.node!=null){logger.info(`CouchDataLayerProvider: Running in Node.js environment, creating guest UserDB for testing.`),await initializeDataDirectory();let e=new CouchDBSyncStrategy;this.userDB=await BaseUser.instance(e)}else{let e=new CouchDBSyncStrategy;this.userDB=await BaseUser.instance(e),this.currentUsername=this.userDB.getUsername(),logger.debug(`Current username: ${this.currentUsername}`)}this.initialized=!0}}async teardown(){this.initialized=!1}getUserDB(){return this.userDB}getCourseDB(e){let t=CourseSyncService.getInstance().getLocalDB(e);return new CourseDB(e,async()=>this.getUserDB(),t??void 0)}async ensureCourseSynced(e,t){return CourseSyncService.getInstance().ensureSynced(e,t)}getCoursesDB(){return new CoursesDB(this._courseIDs)}async getClassroomDB(e,t){return t===`student`?await StudentClassroomDB.factory(e,this.getUserDB()):await TeacherClassroomDB.factory(e)}getAdminDB(){return new AdminDB}async createUserReaderForUser(e){let t=await getLoggedInUsername();if(t!==`admin`)throw Error(`Unauthorized: Only admin users can access other users' data`);logger.info(`Admin user '${t}' requesting UserDBReader for '${e}'`);let n=new CouchDBSyncStrategy;return await BaseUser.instance(n,e)}isReadOnly(){return!1}}}}),pathUtils,nodeFS,StaticDataUnpacker,init_StaticDataUnpacker=__esm({"src/impl/static/StaticDataUnpacker.ts"(){init_logger(),init_core(),pathUtils={isAbsolute:e=>!!(/^[a-zA-Z]:[\\/]/.test(e)||/^\\\\/.test(e)||e.startsWith(`/`))},nodeFS=null;try{typeof window>`u`&&typeof process<`u`&&process.versions?.node&&(nodeFS=eval(`require`)(`fs`))}catch{}StaticDataUnpacker=class{constructor(e,t){_defineProperty(this,`manifest`,void 0),_defineProperty(this,`basePath`,void 0),_defineProperty(this,`documentCache`,new Map),_defineProperty(this,`chunkCache`,new Map),_defineProperty(this,`indexCache`,new Map),this.manifest=e,this.basePath=t}async getDocument(e){if(this.documentCache.has(e)){let t=this.documentCache.get(e);return await this.hydrateAttachments(t)}let t=await this.findChunkForDocument(e);if(!t)throw logger.error(`Document ${e} not found in any chunk. Available chunks:`,this.manifest.chunks.map(e=>`${e.id} (${e.docType}): ${e.startKey} - ${e.endKey}`)),Error(`Document ${e} not found in any chunk`);if(await this.loadChunk(t.id),this.documentCache.has(e)){let t=this.documentCache.get(e);return await this.hydrateAttachments(t)}throw logger.error(`Document ${e} not found in chunk ${t.id}`),Error(`Document ${e} not found in chunk ${t.id}`)}async getAllDocumentsByPrefix(e){let t=this.manifest.chunks.filter(t=>{let n=e+`￰`;return t.startKey<=n&&t.endKey>=e});if(t.length===0)return logger.debug(`[StaticDataUnpacker] No chunks found for prefix: ${e}`),[];await Promise.all(t.map(e=>this.loadChunk(e.id)));let n=[];for(let[t,r]of this.documentCache.entries())t.startsWith(e)&&n.push(await this.hydrateAttachments(r));return logger.debug(`[StaticDataUnpacker] Found ${n.length} documents with prefix: ${e}`),n}async queryByElo(e,t=25){let n=await this.loadIndex(`elo`);if(!n||!n.sorted)return logger.warn(`ELO index not found or malformed, returning empty results`),[];let r=n.sorted,a=0,o=0,s=r.length-1;for(;o<=s;){let t=Math.floor((o+s)/2);r[t].elo<e?o=t+1:s=t-1}a=o;let c=[],l=Math.floor(t/2);for(let e=Math.max(0,a-l);e<a&&c.length<t;e++)c.push(r[e].cardId);for(let e=a;e<r.length&&c.length<t;e++)c.push(r[e].cardId);return c}async getTagsIndex(){try{return await this.loadIndex(`tags`)}catch{return{byCard:{},byTag:{}}}}getDocTypeFromId(e){for(let t in DocTypePrefixes){let n=DocTypePrefixes[t];if(e.startsWith(`${n}-`))return t}}async findChunkForDocument(e){let t=this.getDocTypeFromId(e);if(t){let n=this.manifest.chunks.filter(e=>e.docType===t);for(let t of n)if(e>=t.startKey&&e<=t.endKey&&await this.verifyDocumentInChunk(e,t))return t}else{for(let t of this.manifest.chunks)if(e>=t.startKey&&e<=t.endKey&&await this.verifyDocumentInChunk(e,t))return t;let t=this.manifest.chunks.filter(e=>e.docType!==`CARD`&&e.docType!==`DISPLAYABLE_DATA`&&e.docType!==`TAG`);for(let n of t)if(e>=n.startKey&&e<=n.endKey&&await this.verifyDocumentInChunk(e,n))return n;return}}async verifyDocumentInChunk(e,t){try{return await this.loadChunk(t.id),this.documentCache.has(e)}catch{return!1}}async loadChunk(e){if(this.chunkCache.has(e))return;let t=this.manifest.chunks.find(t=>t.id===e);if(!t)throw Error(`Chunk ${e} not found in manifest`);try{let n=`${this.basePath}/${t.path}`;logger.debug(`Loading chunk from ${n}`);let r;if(this.isLocalPath(n)&&nodeFS){let e=await nodeFS.promises.readFile(n,`utf8`);r=JSON.parse(e)}else{let t=await fetch(n);if(!t.ok)throw Error(`Failed to fetch chunk ${e}: ${t.status} ${t.statusText}`);r=await t.json()}this.chunkCache.set(e,r);for(let e of r)e._id&&this.documentCache.set(e._id,e);logger.debug(`Loaded ${r.length} documents from chunk ${e}`)}catch(t){throw logger.error(`Failed to load chunk ${e}:`,t),t}}async loadIndex(e){if(this.indexCache.has(e))return this.indexCache.get(e);let t=this.manifest.indices.find(t=>t.name===e);if(!t)throw Error(`Index ${e} not found in manifest`);try{let n=`${this.basePath}/${t.path}`;logger.debug(`Loading index from ${n}`);let r;if(this.isLocalPath(n)&&nodeFS){let e=await nodeFS.promises.readFile(n,`utf8`);r=JSON.parse(e)}else{let t=await fetch(n);if(!t.ok)throw Error(`Failed to fetch index ${e}: ${t.status} ${t.statusText}`);r=await t.json()}return this.indexCache.set(e,r),logger.debug(`Loaded index ${e}`),r}catch(t){throw logger.error(`Failed to load index ${e}:`,t),t}}async getRawDocument(e){if(this.documentCache.has(e))return this.documentCache.get(e);let t=await this.findChunkForDocument(e);if(!t)throw logger.error(`Document ${e} not found in any chunk. Available chunks:`,this.manifest.chunks.map(e=>`${e.id} (${e.docType}): ${e.startKey} - ${e.endKey}`)),Error(`Document ${e} not found in any chunk`);if(await this.loadChunk(t.id),this.documentCache.has(e))return this.documentCache.get(e);throw logger.error(`Document ${e} not found in chunk ${t.id}`),Error(`Document ${e} not found in chunk ${t.id}`)}getAttachmentUrl(e,t){return`${this.basePath}/attachments/${e}/${t}`}async getAttachmentPath(e,t){try{let n=await this.getRawDocument(e);if(n._attachments&&n._attachments[t]){let e=n._attachments[t];if(e.path)return`${this.basePath}/${e.path}`}return null}catch{return null}}async getAttachmentBlob(e,t){let n=await this.getAttachmentPath(e,t);if(!n)return null;try{if(this.isLocalPath(n)&&nodeFS)return await nodeFS.promises.readFile(n);{let r=await fetch(n);if(!r.ok)throw Error(`Failed to fetch attachment ${e}/${t}: ${r.status} ${r.statusText}`);return await r.blob()}}catch(n){return logger.error(`Failed to load attachment ${e}/${t}:`,n),null}}async hydrateAttachments(e){let t=e;if(!t._attachments)return e;let n=JSON.parse(JSON.stringify(e));for(let[e,r]of Object.entries(t._attachments)){let a=r;if(a.path)try{let r=await this.getAttachmentBlob(t._id,e);r?typeof window<`u`&&window.URL?n._attachments[e]={...a,data:r,stub:!1}:n._attachments[e]={...a,buffer:r}:logger.warn(`[hydrateAttachments] getAttachmentBlob returned null for ${t._id}/${e}. Skipping hydration for this attachment.`)}catch(n){logger.warn(`[hydrateAttachments] Failed to hydrate attachment ${t._id}/${e}:`,n)}else logger.debug(`[hydrateAttachments] Attachment ${e} for doc ${t._id} has no path. Skipping blob conversion.`)}return n}clearCaches(){this.documentCache.clear(),this.chunkCache.clear(),this.indexCache.clear()}getCacheStats(){return{documents:this.documentCache.size,chunks:this.chunkCache.size,indices:this.indexCache.size}}isLocalPath(e){return!e.startsWith(`http://`)&&!e.startsWith(`https://`)&&(pathUtils.isAbsolute(e)||e.startsWith(`./`)||e.startsWith(`../`))}}}}),StaticCourseDB,init_courseDB2=__esm({"src/impl/static/courseDB.ts"(){init_types_legacy(),init_logger(),init_defaults(),init_PipelineAssembler(),StaticCourseDB=class{constructor(e,t,n,r){_defineProperty(this,`_pendingHints`,null),this.courseId=e,this.unpacker=t,this.userDB=n,this.manifest=r}getCourseID(){return this.courseId}async getCourseConfig(){if(this.manifest.courseConfig!=null)return this.manifest.courseConfig;throw Error(`Course config not found for course ${this.courseId}`)}async updateCourseConfig(e){throw Error(`Cannot update course config in static mode`)}async getCourseInfo(){return{cardCount:this.manifest.chunks.filter(e=>e.docType===`CARD`).reduce((e,t)=>e+t.documentCount,0),registeredUsers:0}}async getCourseDoc(e,t){return this.unpacker.getDocument(e)}async getCourseDocs(e,t){let n=await Promise.all(e.map(async e=>{try{return{id:e,key:e,value:{rev:`1-static`},doc:await this.unpacker.getDocument(e)}}catch{return{key:e,error:`not_found`}}}));return{total_rows:e.length,offset:0,rows:n}}async getCardsByELO(e,t){return(await this.unpacker.queryByElo(e,t||25)).map(e=>{let[t,n,r]=e.split(`-`);return{courseID:t,cardID:n,elo:r?parseInt(r):void 0}})}async getCardEloData(e){return await Promise.all(e.map(async e=>{try{return(await this.unpacker.getDocument(e)).elo||{global:{score:1e3,count:0},tags:{},misc:{}}}catch{return{global:{score:1e3,count:0},tags:{},misc:{}}}}))}async updateCardElo(e,t){return{ok:!0,id:e,rev:`1-static`}}async getCardsCenteredAtELO(e,t){let n=typeof e.elo==`number`?e.elo:1e3;if(e.elo===`user`)try{let e=(await this.userDB.getCourseRegistrationsDoc()).courses.find(e=>e.courseID===this.courseId);e&&typeof e.elo==`object`&&(n=e.elo.global.score)}catch{n=1e3}else e.elo===`random`&&(n=800+Math.random()*400);let r=(await this.unpacker.queryByElo(n,e.limit*2)).map(e=>({cardID:e,courseID:this.courseId}));return t&&(r=r.filter(t)),r.slice(0,e.limit).map(e=>({status:`new`,cardID:e.cardID,contentSourceType:`course`,contentSourceID:this.courseId,courseID:this.courseId}))}async getAppliedTags(e){try{let t=await this.unpacker.getTagsIndex(),n=t.byCard[e]||[],r=await Promise.all(n.map(async n=>{let r=`TAG-${n}`;try{let t=await this.unpacker.getDocument(r);return{id:r,key:e,value:{name:t.name,snippet:t.snippet,count:t.taggedCards?.length||0}}}catch(a){if(a&&a.status===404)logger.warn(`Tag document not found for ${n}, creating stub`);else throw logger.error(`Error getting tag document for ${n}:`,a),a;return{id:r,key:e,value:{name:n,snippet:`Tag: ${n}`,count:t.byTag[n]?.length||0}}}}));return{total_rows:r.length,offset:0,rows:r}}catch(t){return logger.error(`Error getting applied tags for card ${e}:`,t),{total_rows:0,offset:0,rows:[]}}}async getAppliedTagsBatch(e){let t=await this.unpacker.getTagsIndex(),n=new Map;for(let r of e)n.set(r,t.byCard[r]||[]);return n}async getAllCardIds(){let e=await this.unpacker.getTagsIndex();return Object.keys(e.byCard)}async addTagToCard(e,t){throw Error(`Cannot modify tags in static mode`)}async removeTagFromCard(e,t){throw Error(`Cannot modify tags in static mode`)}async createTag(e){throw Error(`Cannot create tags in static mode`)}async getTag(e){return this.unpacker.getDocument(`TAG-${e}`)}async updateTag(e){throw Error(`Cannot update tags in static mode`)}async getCourseTagStubs(){try{let e=await this.unpacker.getTagsIndex();if(!e||!e.byTag)return logger.warn(`Tags index not found or empty`),{total_rows:0,offset:0,rows:[]};let t=Object.keys(e.byTag),n=await Promise.all(t.map(async t=>{let n=e.byTag[t]||[],r=`TAG-${t}`;try{return{id:r,key:r,value:{rev:`1-static`},doc:await this.unpacker.getDocument(r)}}catch(e){if(e&&e.status===404)return logger.warn(`Tag document not found for ${t}, creating stub`),{id:r,key:r,value:{rev:`1-static`},doc:{_id:r,_rev:`1-static`,course:this.courseId,docType:`TAG`,name:t,snippet:`Tag: ${t}`,wiki:``,taggedCards:n,author:`system`}};throw logger.error(`Error getting tag document for ${t}:`,e),e}}));return{total_rows:n.length,offset:0,rows:n}}catch(e){return logger.error(`Failed to get course tag stubs:`,e),{total_rows:0,offset:0,rows:[]}}}async addNote(e,t,n,r,a,o,s){return{status:Status.error,message:`Cannot add notes in static mode`}}async removeCard(e){throw Error(`Cannot remove cards in static mode`)}async getInexperiencedCards(){return[]}async getNavigationStrategy(e){try{return await this.unpacker.getDocument(e)}catch(t){throw logger.error(`[static/courseDB] Strategy ${e} not found: ${t}`),t}}async getAllNavigationStrategies(){let e=DocTypePrefixes.NAVIGATION_STRATEGY;try{return await this.unpacker.getAllDocumentsByPrefix(e)}catch(e){return logger.warn(`[static/courseDB] Error loading navigation strategies: ${e}`),[]}}async addNavigationStrategy(e){throw Error(`Cannot add navigation strategies in static mode`)}async updateNavigationStrategy(e,t){throw Error(`Cannot update navigation strategies in static mode`)}async createNavigator(e){try{let t=await this.getAllNavigationStrategies();if(t.length===0)return logger.debug(`[static/courseDB] No strategy documents found, using default Pipeline(Composite(ELO, SRS), [eloDistanceFilter])`),createDefaultPipeline(e,this);let{pipeline:n,generatorStrategies:r,filterStrategies:a,warnings:o}=await new PipelineAssembler().assemble({strategies:t,user:e,course:this});for(let e of o)logger.warn(`[PipelineAssembler] ${e}`);return n?(logger.debug(`[static/courseDB] Using assembled pipeline with ${r.length} generator(s) and ${a.length} filter(s)`),n):(logger.debug(`[static/courseDB] Pipeline assembly failed, using default pipeline`),createDefaultPipeline(e,this))}catch(e){throw logger.error(`[static/courseDB] Error creating navigator: ${e}`),e}}setEphemeralHints(e){this._pendingHints=e}async getWeightedCards(e){try{let t=await this.createNavigator(this.userDB);return this._pendingHints&&(t.setEphemeralHints(this._pendingHints),this._pendingHints=null),t.getWeightedCards(e)}catch(e){throw logger.error(`[static/courseDB] Error getting weighted cards: ${e}`),e}}getAttachmentUrl(e,t){return this.unpacker.getAttachmentUrl(e,t)}async getAttachmentBlob(e,t){return this.unpacker.getAttachmentBlob(e,t)}async searchCards(e){return[]}async find(e){return{docs:[],warning:`Find operations not supported in static mode`}}}}}),StaticCoursesDB,init_coursesDB=__esm({"src/impl/static/coursesDB.ts"(){init_logger(),StaticCoursesDB=class{constructor(e,t){this.manifests=e,this.dependencyNameToCourseId=t}async getCourseConfig(e){let t=this.manifests[e];if(!t&&this.dependencyNameToCourseId){let n=this.dependencyNameToCourseId.get(e);n&&(t=this.manifests[n])}if(!t)throw logger.warn(`Course manifest for ${e} not found`),Error(`Course ${e} not found`);if(t.courseConfig)return t.courseConfig;throw logger.warn(`Course config not found in manifest for course ${e}`),Error(`Course config not found for course ${e}`)}async getCourseList(){return Object.keys(this.manifests).map(e=>({courseID:e,name:this.manifests[e].courseName}))}async disambiguateCourse(e,t){logger.warn(`Cannot disambiguate courses in static mode`)}}}}),NoOpSyncStrategy,init_NoOpSyncStrategy=__esm({"src/impl/static/NoOpSyncStrategy.ts"(){init_common(),NoOpSyncStrategy=class{constructor(){_defineProperty(this,`currentUsername`,accomodateGuest().username)}setupRemoteDB(e){return getLocalUserDB(e)}getWriteDB(e){return getLocalUserDB(e)}startSync(e,t){}stopSync(){}canCreateAccount(){return!1}canAuthenticate(){return!1}async createAccount(e,t){throw Error(`Account creation not supported in static mode. Use local account switching instead.`)}async authenticate(e,t){throw Error(`Remote authentication not supported in static mode. Use local account switching instead.`)}async logout(){return this.currentUsername=accomodateGuest().username,{ok:!0}}async getCurrentUsername(){return this.currentUsername}setCurrentUsername(e){this.currentUsername=e}}}}),StaticDataLayerProvider_exports={};__export(StaticDataLayerProvider_exports,{StaticDataLayerProvider:()=>StaticDataLayerProvider});var StaticDataLayerProvider,init_StaticDataLayerProvider=__esm({"src/impl/static/StaticDataLayerProvider.ts"(){init_logger(),init_StaticDataUnpacker(),init_courseDB2(),init_coursesDB(),init_common(),init_NoOpSyncStrategy(),StaticDataLayerProvider=class{constructor(e){_defineProperty(this,`config`,void 0),_defineProperty(this,`initialized`,!1),_defineProperty(this,`courseUnpackers`,new Map),_defineProperty(this,`manifests`,{}),_defineProperty(this,`dependencyNameToCourseId`,new Map),this.config={localStoragePrefix:e.localStoragePrefix||`skuilder-static`,rootManifest:e.rootManifest||{dependencies:{}},rootManifestUrl:e.rootManifestUrl||`/`}}async resolveCourseDependencies(){logger.info(`[StaticDataLayerProvider] Starting course dependency resolution...`);let e=this.config.rootManifest;for(let[t,n]of Object.entries(e.dependencies||{}))try{logger.debug(`[StaticDataLayerProvider] Resolving dependency: ${t} from ${n}`);let e=new URL(n,this.config.rootManifestUrl).href,r=await fetch(e);if(!r.ok)throw Error(`Failed to fetch course manifest for ${t}`);let a=await r.json();if(a.content&&a.content.manifest){let n=new URL(`.`,e).href,r=new URL(a.content.manifest,e).href,o=await fetch(r);if(!o.ok)throw Error(`Failed to fetch final content manifest for ${t} at ${r}`);let s=await o.json(),c=s.courseId||s.courseConfig?.courseID;if(!c)throw Error(`Course manifest for ${t} missing courseId`);this.manifests[c]=s;let l=new StaticDataUnpacker(s,n);this.courseUnpackers.set(c,l),this.dependencyNameToCourseId.set(t,c),logger.info(`[StaticDataLayerProvider] Successfully resolved and prepared course: ${t} (courseId: ${c})`)}}catch(e){logger.error(`[StaticDataLayerProvider] Failed to resolve dependency ${t}:`,e)}logger.info(`[StaticDataLayerProvider] Course dependency resolution complete.`)}async initialize(){this.initialized||(logger.info(`Initializing static data layer provider`),await this.resolveCourseDependencies(),this.initialized=!0)}async teardown(){this.courseUnpackers.clear(),this.initialized=!1}getUserDB(){let e=new NoOpSyncStrategy;return BaseUser.Dummy(e)}getCourseDB(e){let t=this.courseUnpackers.get(e),n=e;if(!t){let r=this.dependencyNameToCourseId.get(e);r&&(t=this.courseUnpackers.get(r),n=r)}if(!t)throw Error(`Course ${e} not found or failed to initialize in static data layer.`);let r=this.manifests[n];return new StaticCourseDB(n,t,this.getUserDB(),r)}getCoursesDB(){return new StaticCoursesDB(this.manifests,this.dependencyNameToCourseId)}async getClassroomDB(e,t){throw Error(`Classrooms not supported in static mode`)}getAdminDB(){throw Error(`Admin functions not supported in static mode`)}async createUserReaderForUser(e){return logger.warn(`StaticDataLayerProvider: Multi-user access not supported in static mode`),logger.warn(`Request: trying to access data for ${e}`),logger.warn(`Returning current user's data instead`),this.getUserDB()}isReadOnly(){return!0}}}});async function initializeDataLayer(e){if(dataLayerInstance)return logger.warn(`Data layer already initialized. Returning existing instance.`),dataLayerInstance;if(await initializeNavigatorRegistry(),e.options.localStoragePrefix&&(ENV.LOCAL_STORAGE_PREFIX=e.options.localStoragePrefix),e.type===`couch`){if(!e.options.COUCHDB_SERVER_URL||!e.options.COUCHDB_SERVER_PROTOCOL)throw Error(`Missing CouchDB server URL or protocol`);if(ENV.COUCHDB_SERVER_PROTOCOL=e.options.COUCHDB_SERVER_PROTOCOL,ENV.COUCHDB_SERVER_URL=e.options.COUCHDB_SERVER_URL,ENV.COUCHDB_USERNAME=e.options.COUCHDB_USERNAME,ENV.COUCHDB_PASSWORD=e.options.COUCHDB_PASSWORD,e.options.COUCHDB_PASSWORD&&e.options.COUCHDB_USERNAME&&typeof window<`u`){let{CouchDBSyncStrategy:t}=await Promise.resolve().then(()=>(init_CouchDBSyncStrategy(),CouchDBSyncStrategy_exports)),n=new t,r=await(await BaseUser.instance(n,e.options.COUCHDB_USERNAME)).login(e.options.COUCHDB_USERNAME,e.options.COUCHDB_PASSWORD);r.ok?logger.info(`Successfully authenticated as ${e.options.COUCHDB_USERNAME}`):logger.warn(`Authentication failed: ${r.error}`)}let{CouchDataLayerProvider:t}=await Promise.resolve().then(()=>(init_PouchDataLayerProvider(),PouchDataLayerProvider_exports));dataLayerInstance=new t(e.options.COURSE_IDS)}else if(e.type===`static`){let{StaticDataLayerProvider:t}=await Promise.resolve().then(()=>(init_StaticDataLayerProvider(),StaticDataLayerProvider_exports));dataLayerInstance=new t(e.options)}else throw Error(`Unknown data layer type: ${e.type}`);return await dataLayerInstance.initialize(),dataLayerInstance}function getDataLayer(){if(!dataLayerInstance)throw Error(`Data layer not initialized. Call initializeDataLayer first.`);return dataLayerInstance}async function _resetDataLayer(){dataLayerInstance&&await dataLayerInstance.teardown(),dataLayerInstance=null}var NOT_SET,ENV,dataLayerInstance,init_factory=__esm({"src/factory.ts"(){init_common(),init_logger(),init_navigators(),NOT_SET=`NOT_SET`,ENV={COUCHDB_SERVER_PROTOCOL:NOT_SET,COUCHDB_SERVER_URL:NOT_SET,LOCAL_STORAGE_PREFIX:``},dataLayerInstance=null}}),TagFilteredContentSource,init_TagFilteredContentSource=__esm({"src/study/TagFilteredContentSource.ts"(){init_courseDB(),init_logger(),TagFilteredContentSource=class{constructor(e,t,n){_defineProperty(this,`courseId`,void 0),_defineProperty(this,`filter`,void 0),_defineProperty(this,`user`,void 0),_defineProperty(this,`resolvedCardIds`,null),this.courseId=e,this.filter=t,this.user=n,logger.info(`[TagFilteredContentSource] Created for course "${e}" with filter:`,JSON.stringify(t))}async resolveFilteredCardIds(){if(this.resolvedCardIds!==null)return this.resolvedCardIds;let e=new Set;if(this.filter.include.length>0)for(let t of this.filter.include)try{(await getTag(this.courseId,t)).taggedCards.forEach(t=>e.add(t))}catch(e){logger.warn(`[TagFilteredContentSource] Could not resolve tag "${t}" for inclusion:`,e)}if(e.size===0&&this.filter.include.length>0)return logger.warn(`[TagFilteredContentSource] No cards found for include tags: ${this.filter.include.join(`, `)}`),this.resolvedCardIds=new Set,this.resolvedCardIds;let t=new Set;if(this.filter.exclude.length>0)for(let e of this.filter.exclude)try{(await getTag(this.courseId,e)).taggedCards.forEach(e=>t.add(e))}catch(t){logger.warn(`[TagFilteredContentSource] Could not resolve tag "${e}" for exclusion:`,t)}let n=new Set;for(let r of e)t.has(r)||n.add(r);return logger.info(`[TagFilteredContentSource] Resolved ${n.size} cards (included: ${e.size}, excluded: ${t.size})`),this.resolvedCardIds=n,n}async getWeightedCards(e){if(!hasActiveFilter(this.filter))return logger.warn(`[TagFilteredContentSource] getWeightedCards called with no active filter`),{cards:[]};let t=await this.resolveFilteredCardIds(),n=await this.user.getActiveCards(),r=new Set(n.map(e=>e.cardID)),a=[];for(let n of t)if(r.has(n)||a.push({cardId:n,courseId:this.courseId,score:1,provenance:[{strategy:`tagFilter`,strategyName:`Tag Filter`,strategyId:`TAG_FILTER`,action:`generated`,score:1,reason:`Tag-filtered new card (tags: ${this.filter.include.join(`, `)})`}]}),a.length>=e)break;logger.info(`[TagFilteredContentSource] Found ${a.length} new cards matching filter`);let o=await this.user.getPendingReviews(this.courseId),s=o.filter(e=>t.has(e.cardId));return logger.info(`[TagFilteredContentSource] Found ${s.length} pending reviews matching filter (of ${o.length} total)`),{cards:[...s.map(e=>({cardId:e.cardId,courseId:e.courseId,score:1,reviewID:e._id,provenance:[{strategy:`tagFilter`,strategyName:`Tag Filter`,strategyId:`TAG_FILTER`,action:`generated`,score:1,reason:`Tag-filtered review (tags: ${this.filter.include.join(`, `)})`}]})),...a].slice(0,e)}}clearCache(){this.resolvedCardIds=null}getCourseId(){return this.courseId}getFilter(){return this.filter}}}});function isReview(e){return e.status===`review`||e.status===`failed-review`||`reviewID`in e}async function getStudySource(e,t){return e.type===`classroom`?await StudentClassroomDB.factory(e.id,t):hasActiveFilter(e.tagFilter)?new TagFilteredContentSource(e.id,e.tagFilter,t):getDataLayer().getCourseDB(e.id)}var init_contentSource=__esm({"src/core/interfaces/contentSource.ts"(){init_factory(),init_classroomDB2(),init_TagFilteredContentSource()}}),init_courseDB3=__esm({"src/core/interfaces/courseDB.ts"(){}}),init_dataLayerProvider=__esm({"src/core/interfaces/dataLayerProvider.ts"(){}}),init_userDB=__esm({"src/core/interfaces/userDB.ts"(){}}),init_interfaces=__esm({"src/core/interfaces/index.ts"(){init_adminDB(),init_classroomDB(),init_contentSource(),init_courseDB3(),init_dataLayerProvider(),init_userDB()}}),init_user=__esm({"src/core/types/user.ts"(){}});function buildStrategyStateId(e,t){return`STRATEGY_STATE::${e}::${t}`}var init_strategyState=__esm({"src/core/types/strategyState.ts"(){}}),init_userOutcome=__esm({"src/core/types/userOutcome.ts"(){}});async function importParsedCards(e,t,n){let r=[];for(let a of e)try{let e=await processCard(a,t,n);r.push(e)}catch(e){logger.error(`Error processing card:`,e);let t=a.markdown;a.tags&&a.tags.length>0&&(t+=`
281
+ }`}}}]),e),userCoursesDoc=`CourseRegistrations`,userClassroomsDoc=`ClassroomRegistrations`}}),init_common=__esm({"src/impl/common/index.ts"(){init_SyncStrategy(),init_BaseUserDB(),init_userDBHelpers()}}),PouchDataLayerProvider_exports={};__export(PouchDataLayerProvider_exports,{CouchDataLayerProvider:()=>CouchDataLayerProvider});var CouchDataLayerProvider,init_PouchDataLayerProvider=__esm({"src/impl/couch/PouchDataLayerProvider.ts"(){init_logger(),init_dataDirectory(),init_auth(),init_adminDB2(),init_classroomDB2(),init_courseDB(),init_CourseSyncService(),init_common(),init_CouchDBSyncStrategy(),CouchDataLayerProvider=class{constructor(e){_defineProperty(this,`initialized`,!1),_defineProperty(this,`userDB`,void 0),_defineProperty(this,`currentUsername`,``),_defineProperty(this,`_courseIDs`,[]),e&&(this._courseIDs=e)}async initialize(){if(!this.initialized){if(typeof process<`u`&&process.versions!=null&&process.versions.node!=null){logger.info(`CouchDataLayerProvider: Running in Node.js environment, creating guest UserDB for testing.`),await initializeDataDirectory();let e=new CouchDBSyncStrategy;this.userDB=await BaseUser.instance(e)}else{let e=new CouchDBSyncStrategy;this.userDB=await BaseUser.instance(e),this.currentUsername=this.userDB.getUsername(),logger.debug(`Current username: ${this.currentUsername}`)}this.initialized=!0}}async teardown(){this.initialized=!1}getUserDB(){return this.userDB}getCourseDB(e){let t=CourseSyncService.getInstance().getLocalDB(e);return new CourseDB(e,async()=>this.getUserDB(),t??void 0)}async ensureCourseSynced(e,t){return CourseSyncService.getInstance().ensureSynced(e,t)}getCoursesDB(){return new CoursesDB(this._courseIDs)}async getClassroomDB(e,t){return t===`student`?await StudentClassroomDB.factory(e,this.getUserDB()):await TeacherClassroomDB.factory(e)}getAdminDB(){return new AdminDB}async createUserReaderForUser(e){let t=await getLoggedInUsername();if(t!==`admin`)throw Error(`Unauthorized: Only admin users can access other users' data`);logger.info(`Admin user '${t}' requesting UserDBReader for '${e}'`);let n=new CouchDBSyncStrategy;return await BaseUser.instance(n,e)}isReadOnly(){return!1}}}}),pathUtils,nodeFS,StaticDataUnpacker,init_StaticDataUnpacker=__esm({"src/impl/static/StaticDataUnpacker.ts"(){init_logger(),init_core(),pathUtils={isAbsolute:e=>!!(/^[a-zA-Z]:[\\/]/.test(e)||/^\\\\/.test(e)||e.startsWith(`/`))},nodeFS=null;try{typeof window>`u`&&typeof process<`u`&&process.versions?.node&&(nodeFS=eval(`require`)(`fs`))}catch{}StaticDataUnpacker=class{constructor(e,t){_defineProperty(this,`manifest`,void 0),_defineProperty(this,`basePath`,void 0),_defineProperty(this,`documentCache`,new Map),_defineProperty(this,`chunkCache`,new Map),_defineProperty(this,`indexCache`,new Map),this.manifest=e,this.basePath=t}async getDocument(e){if(this.documentCache.has(e)){let t=this.documentCache.get(e);return await this.hydrateAttachments(t)}let t=await this.findChunkForDocument(e);if(!t)throw logger.error(`Document ${e} not found in any chunk. Available chunks:`,this.manifest.chunks.map(e=>`${e.id} (${e.docType}): ${e.startKey} - ${e.endKey}`)),Error(`Document ${e} not found in any chunk`);if(await this.loadChunk(t.id),this.documentCache.has(e)){let t=this.documentCache.get(e);return await this.hydrateAttachments(t)}throw logger.error(`Document ${e} not found in chunk ${t.id}`),Error(`Document ${e} not found in chunk ${t.id}`)}async getAllDocumentsByPrefix(e){let t=this.manifest.chunks.filter(t=>{let n=e+`￰`;return t.startKey<=n&&t.endKey>=e});if(t.length===0)return logger.debug(`[StaticDataUnpacker] No chunks found for prefix: ${e}`),[];await Promise.all(t.map(e=>this.loadChunk(e.id)));let n=[];for(let[t,r]of this.documentCache.entries())t.startsWith(e)&&n.push(await this.hydrateAttachments(r));return logger.debug(`[StaticDataUnpacker] Found ${n.length} documents with prefix: ${e}`),n}async queryByElo(e,t=25){let n=await this.loadIndex(`elo`);if(!n||!n.sorted)return logger.warn(`ELO index not found or malformed, returning empty results`),[];let r=n.sorted,a=0,o=0,s=r.length-1;for(;o<=s;){let t=Math.floor((o+s)/2);r[t].elo<e?o=t+1:s=t-1}a=o;let c=[],l=Math.floor(t/2);for(let e=Math.max(0,a-l);e<a&&c.length<t;e++)c.push(r[e].cardId);for(let e=a;e<r.length&&c.length<t;e++)c.push(r[e].cardId);return c}async getTagsIndex(){try{return await this.loadIndex(`tags`)}catch{return{byCard:{},byTag:{}}}}getDocTypeFromId(e){for(let t in DocTypePrefixes){let n=DocTypePrefixes[t];if(e.startsWith(`${n}-`))return t}}async findChunkForDocument(e){let t=this.getDocTypeFromId(e);if(t){let n=this.manifest.chunks.filter(e=>e.docType===t);for(let t of n)if(e>=t.startKey&&e<=t.endKey&&await this.verifyDocumentInChunk(e,t))return t}else{for(let t of this.manifest.chunks)if(e>=t.startKey&&e<=t.endKey&&await this.verifyDocumentInChunk(e,t))return t;let t=this.manifest.chunks.filter(e=>e.docType!==`CARD`&&e.docType!==`DISPLAYABLE_DATA`&&e.docType!==`TAG`);for(let n of t)if(e>=n.startKey&&e<=n.endKey&&await this.verifyDocumentInChunk(e,n))return n;return}}async verifyDocumentInChunk(e,t){try{return await this.loadChunk(t.id),this.documentCache.has(e)}catch{return!1}}async loadChunk(e){if(this.chunkCache.has(e))return;let t=this.manifest.chunks.find(t=>t.id===e);if(!t)throw Error(`Chunk ${e} not found in manifest`);try{let n=`${this.basePath}/${t.path}`;logger.debug(`Loading chunk from ${n}`);let r;if(this.isLocalPath(n)&&nodeFS){let e=await nodeFS.promises.readFile(n,`utf8`);r=JSON.parse(e)}else{let t=await fetch(n);if(!t.ok)throw Error(`Failed to fetch chunk ${e}: ${t.status} ${t.statusText}`);r=await t.json()}this.chunkCache.set(e,r);for(let e of r)e._id&&this.documentCache.set(e._id,e);logger.debug(`Loaded ${r.length} documents from chunk ${e}`)}catch(t){throw logger.error(`Failed to load chunk ${e}:`,t),t}}async loadIndex(e){if(this.indexCache.has(e))return this.indexCache.get(e);let t=this.manifest.indices.find(t=>t.name===e);if(!t)throw Error(`Index ${e} not found in manifest`);try{let n=`${this.basePath}/${t.path}`;logger.debug(`Loading index from ${n}`);let r;if(this.isLocalPath(n)&&nodeFS){let e=await nodeFS.promises.readFile(n,`utf8`);r=JSON.parse(e)}else{let t=await fetch(n);if(!t.ok)throw Error(`Failed to fetch index ${e}: ${t.status} ${t.statusText}`);r=await t.json()}return this.indexCache.set(e,r),logger.debug(`Loaded index ${e}`),r}catch(t){throw logger.error(`Failed to load index ${e}:`,t),t}}async getRawDocument(e){if(this.documentCache.has(e))return this.documentCache.get(e);let t=await this.findChunkForDocument(e);if(!t)throw logger.error(`Document ${e} not found in any chunk. Available chunks:`,this.manifest.chunks.map(e=>`${e.id} (${e.docType}): ${e.startKey} - ${e.endKey}`)),Error(`Document ${e} not found in any chunk`);if(await this.loadChunk(t.id),this.documentCache.has(e))return this.documentCache.get(e);throw logger.error(`Document ${e} not found in chunk ${t.id}`),Error(`Document ${e} not found in chunk ${t.id}`)}getAttachmentUrl(e,t){return`${this.basePath}/attachments/${e}/${t}`}async getAttachmentPath(e,t){try{let n=await this.getRawDocument(e);if(n._attachments&&n._attachments[t]){let e=n._attachments[t];if(e.path)return`${this.basePath}/${e.path}`}return null}catch{return null}}async getAttachmentBlob(e,t){let n=await this.getAttachmentPath(e,t);if(!n)return null;try{if(this.isLocalPath(n)&&nodeFS)return await nodeFS.promises.readFile(n);{let r=await fetch(n);if(!r.ok)throw Error(`Failed to fetch attachment ${e}/${t}: ${r.status} ${r.statusText}`);return await r.blob()}}catch(n){return logger.error(`Failed to load attachment ${e}/${t}:`,n),null}}async hydrateAttachments(e){let t=e;if(!t._attachments)return e;let n=JSON.parse(JSON.stringify(e));for(let[e,r]of Object.entries(t._attachments)){let a=r;if(a.path)try{let r=await this.getAttachmentBlob(t._id,e);r?typeof window<`u`&&window.URL?n._attachments[e]={...a,data:r,stub:!1}:n._attachments[e]={...a,buffer:r}:logger.warn(`[hydrateAttachments] getAttachmentBlob returned null for ${t._id}/${e}. Skipping hydration for this attachment.`)}catch(n){logger.warn(`[hydrateAttachments] Failed to hydrate attachment ${t._id}/${e}:`,n)}else logger.debug(`[hydrateAttachments] Attachment ${e} for doc ${t._id} has no path. Skipping blob conversion.`)}return n}clearCaches(){this.documentCache.clear(),this.chunkCache.clear(),this.indexCache.clear()}getCacheStats(){return{documents:this.documentCache.size,chunks:this.chunkCache.size,indices:this.indexCache.size}}isLocalPath(e){return!e.startsWith(`http://`)&&!e.startsWith(`https://`)&&(pathUtils.isAbsolute(e)||e.startsWith(`./`)||e.startsWith(`../`))}}}}),StaticCourseDB,init_courseDB2=__esm({"src/impl/static/courseDB.ts"(){init_types_legacy(),init_logger(),init_defaults(),init_PipelineAssembler(),StaticCourseDB=class{constructor(e,t,n,r){_defineProperty(this,`_pendingHints`,null),this.courseId=e,this.unpacker=t,this.userDB=n,this.manifest=r}getCourseID(){return this.courseId}async getCourseConfig(){if(this.manifest.courseConfig!=null)return this.manifest.courseConfig;throw Error(`Course config not found for course ${this.courseId}`)}async updateCourseConfig(e){throw Error(`Cannot update course config in static mode`)}async getCourseInfo(){return{cardCount:this.manifest.chunks.filter(e=>e.docType===`CARD`).reduce((e,t)=>e+t.documentCount,0),registeredUsers:0}}async getCourseDoc(e,t){return this.unpacker.getDocument(e)}async getCourseDocs(e,t){let n=await Promise.all(e.map(async e=>{try{return{id:e,key:e,value:{rev:`1-static`},doc:await this.unpacker.getDocument(e)}}catch{return{key:e,error:`not_found`}}}));return{total_rows:e.length,offset:0,rows:n}}async getCardsByELO(e,t){return(await this.unpacker.queryByElo(e,t||25)).map(e=>{let[t,n,r]=e.split(`-`);return{courseID:t,cardID:n,elo:r?parseInt(r):void 0}})}async getCardEloData(e){return await Promise.all(e.map(async e=>{try{return(await this.unpacker.getDocument(e)).elo||{global:{score:1e3,count:0},tags:{},misc:{}}}catch{return{global:{score:1e3,count:0},tags:{},misc:{}}}}))}async updateCardElo(e,t){return{ok:!0,id:e,rev:`1-static`}}async getCardsCenteredAtELO(e,t){let n=typeof e.elo==`number`?e.elo:1e3;if(e.elo===`user`)try{let e=(await this.userDB.getCourseRegistrationsDoc()).courses.find(e=>e.courseID===this.courseId);e&&typeof e.elo==`object`&&(n=e.elo.global.score)}catch{n=1e3}else e.elo===`random`&&(n=800+Math.random()*400);let r=(await this.unpacker.queryByElo(n,e.limit*2)).map(e=>({cardID:e,courseID:this.courseId}));return t&&(r=r.filter(t)),r.slice(0,e.limit).map(e=>({status:`new`,cardID:e.cardID,contentSourceType:`course`,contentSourceID:this.courseId,courseID:this.courseId}))}async getAppliedTags(e){try{let t=await this.unpacker.getTagsIndex(),n=t.byCard[e]||[],r=await Promise.all(n.map(async n=>{let r=`TAG-${n}`;try{let t=await this.unpacker.getDocument(r);return{id:r,key:e,value:{name:t.name,snippet:t.snippet,count:t.taggedCards?.length||0}}}catch(a){if(a&&a.status===404)logger.warn(`Tag document not found for ${n}, creating stub`);else throw logger.error(`Error getting tag document for ${n}:`,a),a;return{id:r,key:e,value:{name:n,snippet:`Tag: ${n}`,count:t.byTag[n]?.length||0}}}}));return{total_rows:r.length,offset:0,rows:r}}catch(t){return logger.error(`Error getting applied tags for card ${e}:`,t),{total_rows:0,offset:0,rows:[]}}}async getAppliedTagsBatch(e){let t=await this.unpacker.getTagsIndex(),n=new Map;for(let r of e)n.set(r,t.byCard[r]||[]);return n}async getAllCardIds(){let e=await this.unpacker.getTagsIndex();return Object.keys(e.byCard)}async addTagToCard(e,t){throw Error(`Cannot modify tags in static mode`)}async removeTagFromCard(e,t){throw Error(`Cannot modify tags in static mode`)}async createTag(e){throw Error(`Cannot create tags in static mode`)}async getTag(e){return this.unpacker.getDocument(`TAG-${e}`)}async updateTag(e){throw Error(`Cannot update tags in static mode`)}async getCourseTagStubs(){try{let e=await this.unpacker.getTagsIndex();if(!e||!e.byTag)return logger.warn(`Tags index not found or empty`),{total_rows:0,offset:0,rows:[]};let t=Object.keys(e.byTag),n=await Promise.all(t.map(async t=>{let n=e.byTag[t]||[],r=`TAG-${t}`;try{return{id:r,key:r,value:{rev:`1-static`},doc:await this.unpacker.getDocument(r)}}catch(e){if(e&&e.status===404)return logger.warn(`Tag document not found for ${t}, creating stub`),{id:r,key:r,value:{rev:`1-static`},doc:{_id:r,_rev:`1-static`,course:this.courseId,docType:`TAG`,name:t,snippet:`Tag: ${t}`,wiki:``,taggedCards:n,author:`system`}};throw logger.error(`Error getting tag document for ${t}:`,e),e}}));return{total_rows:n.length,offset:0,rows:n}}catch(e){return logger.error(`Failed to get course tag stubs:`,e),{total_rows:0,offset:0,rows:[]}}}async addNote(e,t,n,r,a,o,s){return{status:Status.error,message:`Cannot add notes in static mode`}}async removeCard(e){throw Error(`Cannot remove cards in static mode`)}async getInexperiencedCards(){return[]}async getNavigationStrategy(e){try{return await this.unpacker.getDocument(e)}catch(t){throw logger.error(`[static/courseDB] Strategy ${e} not found: ${t}`),t}}async getAllNavigationStrategies(){let e=DocTypePrefixes.NAVIGATION_STRATEGY;try{return await this.unpacker.getAllDocumentsByPrefix(e)}catch(e){return logger.warn(`[static/courseDB] Error loading navigation strategies: ${e}`),[]}}async addNavigationStrategy(e){throw Error(`Cannot add navigation strategies in static mode`)}async updateNavigationStrategy(e,t){throw Error(`Cannot update navigation strategies in static mode`)}async createNavigator(e){try{let t=await this.getAllNavigationStrategies();if(t.length===0)return logger.debug(`[static/courseDB] No strategy documents found, using default Pipeline(Composite(ELO, SRS), [eloDistanceFilter])`),createDefaultPipeline(e,this);let{pipeline:n,generatorStrategies:r,filterStrategies:a,warnings:o}=await new PipelineAssembler().assemble({strategies:t,user:e,course:this});for(let e of o)logger.warn(`[PipelineAssembler] ${e}`);return n?(logger.debug(`[static/courseDB] Using assembled pipeline with ${r.length} generator(s) and ${a.length} filter(s)`),n):(logger.debug(`[static/courseDB] Pipeline assembly failed, using default pipeline`),createDefaultPipeline(e,this))}catch(e){throw logger.error(`[static/courseDB] Error creating navigator: ${e}`),e}}setEphemeralHints(e){this._pendingHints=e}async getWeightedCards(e){try{let t=await this.createNavigator(this.userDB);return this._pendingHints&&(t.setEphemeralHints(this._pendingHints),this._pendingHints=null),t.getWeightedCards(e)}catch(e){throw logger.error(`[static/courseDB] Error getting weighted cards: ${e}`),e}}getAttachmentUrl(e,t){return this.unpacker.getAttachmentUrl(e,t)}async getAttachmentBlob(e,t){return this.unpacker.getAttachmentBlob(e,t)}async searchCards(e){return[]}async find(e){return{docs:[],warning:`Find operations not supported in static mode`}}}}}),StaticCoursesDB,init_coursesDB=__esm({"src/impl/static/coursesDB.ts"(){init_logger(),StaticCoursesDB=class{constructor(e,t){this.manifests=e,this.dependencyNameToCourseId=t}async getCourseConfig(e){let t=this.manifests[e];if(!t&&this.dependencyNameToCourseId){let n=this.dependencyNameToCourseId.get(e);n&&(t=this.manifests[n])}if(!t)throw logger.warn(`Course manifest for ${e} not found`),Error(`Course ${e} not found`);if(t.courseConfig)return t.courseConfig;throw logger.warn(`Course config not found in manifest for course ${e}`),Error(`Course config not found for course ${e}`)}async getCourseList(){return Object.keys(this.manifests).map(e=>({courseID:e,name:this.manifests[e].courseName}))}async disambiguateCourse(e,t){logger.warn(`Cannot disambiguate courses in static mode`)}}}}),NoOpSyncStrategy,init_NoOpSyncStrategy=__esm({"src/impl/static/NoOpSyncStrategy.ts"(){init_common(),NoOpSyncStrategy=class{constructor(){_defineProperty(this,`currentUsername`,accomodateGuest().username)}setupRemoteDB(e){return getLocalUserDB(e)}getWriteDB(e){return getLocalUserDB(e)}startSync(e,t){}stopSync(){}canCreateAccount(){return!1}canAuthenticate(){return!1}async createAccount(e,t){throw Error(`Account creation not supported in static mode. Use local account switching instead.`)}async authenticate(e,t){throw Error(`Remote authentication not supported in static mode. Use local account switching instead.`)}async logout(){return this.currentUsername=accomodateGuest().username,{ok:!0}}async getCurrentUsername(){return this.currentUsername}setCurrentUsername(e){this.currentUsername=e}}}}),StaticDataLayerProvider_exports={};__export(StaticDataLayerProvider_exports,{StaticDataLayerProvider:()=>StaticDataLayerProvider});var StaticDataLayerProvider,init_StaticDataLayerProvider=__esm({"src/impl/static/StaticDataLayerProvider.ts"(){init_logger(),init_StaticDataUnpacker(),init_courseDB2(),init_coursesDB(),init_common(),init_NoOpSyncStrategy(),StaticDataLayerProvider=class{constructor(e){_defineProperty(this,`config`,void 0),_defineProperty(this,`initialized`,!1),_defineProperty(this,`courseUnpackers`,new Map),_defineProperty(this,`manifests`,{}),_defineProperty(this,`dependencyNameToCourseId`,new Map),this.config={localStoragePrefix:e.localStoragePrefix||`skuilder-static`,rootManifest:e.rootManifest||{dependencies:{}},rootManifestUrl:e.rootManifestUrl||`/`}}async resolveCourseDependencies(){logger.info(`[StaticDataLayerProvider] Starting course dependency resolution...`);let e=this.config.rootManifest;for(let[t,n]of Object.entries(e.dependencies||{}))try{logger.debug(`[StaticDataLayerProvider] Resolving dependency: ${t} from ${n}`);let e=new URL(n,this.config.rootManifestUrl).href,r=await fetch(e);if(!r.ok)throw Error(`Failed to fetch course manifest for ${t}`);let a=await r.json();if(a.content&&a.content.manifest){let n=new URL(`.`,e).href,r=new URL(a.content.manifest,e).href,o=await fetch(r);if(!o.ok)throw Error(`Failed to fetch final content manifest for ${t} at ${r}`);let s=await o.json(),c=s.courseId||s.courseConfig?.courseID;if(!c)throw Error(`Course manifest for ${t} missing courseId`);this.manifests[c]=s;let l=new StaticDataUnpacker(s,n);this.courseUnpackers.set(c,l),this.dependencyNameToCourseId.set(t,c),logger.info(`[StaticDataLayerProvider] Successfully resolved and prepared course: ${t} (courseId: ${c})`)}}catch(e){logger.error(`[StaticDataLayerProvider] Failed to resolve dependency ${t}:`,e)}logger.info(`[StaticDataLayerProvider] Course dependency resolution complete.`)}async initialize(){this.initialized||(logger.info(`Initializing static data layer provider`),await this.resolveCourseDependencies(),this.initialized=!0)}async teardown(){this.courseUnpackers.clear(),this.initialized=!1}getUserDB(){let e=new NoOpSyncStrategy;return BaseUser.Dummy(e)}getCourseDB(e){let t=this.courseUnpackers.get(e),n=e;if(!t){let r=this.dependencyNameToCourseId.get(e);r&&(t=this.courseUnpackers.get(r),n=r)}if(!t)throw Error(`Course ${e} not found or failed to initialize in static data layer.`);let r=this.manifests[n];return new StaticCourseDB(n,t,this.getUserDB(),r)}getCoursesDB(){return new StaticCoursesDB(this.manifests,this.dependencyNameToCourseId)}async getClassroomDB(e,t){throw Error(`Classrooms not supported in static mode`)}getAdminDB(){throw Error(`Admin functions not supported in static mode`)}async createUserReaderForUser(e){return logger.warn(`StaticDataLayerProvider: Multi-user access not supported in static mode`),logger.warn(`Request: trying to access data for ${e}`),logger.warn(`Returning current user's data instead`),this.getUserDB()}isReadOnly(){return!0}}}});async function initializeDataLayer(e){if(dataLayerInstance)return logger.warn(`Data layer already initialized. Returning existing instance.`),dataLayerInstance;if(await initializeNavigatorRegistry(),e.options.localStoragePrefix&&(ENV.LOCAL_STORAGE_PREFIX=e.options.localStoragePrefix),e.type===`couch`){if(!e.options.COUCHDB_SERVER_URL||!e.options.COUCHDB_SERVER_PROTOCOL)throw Error(`Missing CouchDB server URL or protocol`);if(ENV.COUCHDB_SERVER_PROTOCOL=e.options.COUCHDB_SERVER_PROTOCOL,ENV.COUCHDB_SERVER_URL=e.options.COUCHDB_SERVER_URL,ENV.COUCHDB_USERNAME=e.options.COUCHDB_USERNAME,ENV.COUCHDB_PASSWORD=e.options.COUCHDB_PASSWORD,e.options.courseSync){let{CourseSyncService:t}=await Promise.resolve().then(()=>(init_CourseSyncService(),CourseSyncService_exports));t.getInstance().configure(e.options.courseSync)}if(e.options.COUCHDB_PASSWORD&&e.options.COUCHDB_USERNAME&&typeof window<`u`){let{CouchDBSyncStrategy:t}=await Promise.resolve().then(()=>(init_CouchDBSyncStrategy(),CouchDBSyncStrategy_exports)),n=new t,r=await(await BaseUser.instance(n,e.options.COUCHDB_USERNAME)).login(e.options.COUCHDB_USERNAME,e.options.COUCHDB_PASSWORD);r.ok?logger.info(`Successfully authenticated as ${e.options.COUCHDB_USERNAME}`):logger.warn(`Authentication failed: ${r.error}`)}let{CouchDataLayerProvider:t}=await Promise.resolve().then(()=>(init_PouchDataLayerProvider(),PouchDataLayerProvider_exports));dataLayerInstance=new t(e.options.COURSE_IDS)}else if(e.type===`static`){let{StaticDataLayerProvider:t}=await Promise.resolve().then(()=>(init_StaticDataLayerProvider(),StaticDataLayerProvider_exports));dataLayerInstance=new t(e.options)}else throw Error(`Unknown data layer type: ${e.type}`);return await dataLayerInstance.initialize(),dataLayerInstance}function getDataLayer(){if(!dataLayerInstance)throw Error(`Data layer not initialized. Call initializeDataLayer first.`);return dataLayerInstance}async function _resetDataLayer(){dataLayerInstance&&await dataLayerInstance.teardown(),dataLayerInstance=null}var NOT_SET,ENV,dataLayerInstance,init_factory=__esm({"src/factory.ts"(){init_common(),init_logger(),init_navigators(),NOT_SET=`NOT_SET`,ENV={COUCHDB_SERVER_PROTOCOL:NOT_SET,COUCHDB_SERVER_URL:NOT_SET,LOCAL_STORAGE_PREFIX:``},dataLayerInstance=null}}),TagFilteredContentSource,init_TagFilteredContentSource=__esm({"src/study/TagFilteredContentSource.ts"(){init_courseDB(),init_logger(),TagFilteredContentSource=class{constructor(e,t,n){_defineProperty(this,`courseId`,void 0),_defineProperty(this,`filter`,void 0),_defineProperty(this,`user`,void 0),_defineProperty(this,`resolvedCardIds`,null),this.courseId=e,this.filter=t,this.user=n,logger.info(`[TagFilteredContentSource] Created for course "${e}" with filter:`,JSON.stringify(t))}async resolveFilteredCardIds(){if(this.resolvedCardIds!==null)return this.resolvedCardIds;let e=new Set;if(this.filter.include.length>0)for(let t of this.filter.include)try{(await getTag(this.courseId,t)).taggedCards.forEach(t=>e.add(t))}catch(e){logger.warn(`[TagFilteredContentSource] Could not resolve tag "${t}" for inclusion:`,e)}if(e.size===0&&this.filter.include.length>0)return logger.warn(`[TagFilteredContentSource] No cards found for include tags: ${this.filter.include.join(`, `)}`),this.resolvedCardIds=new Set,this.resolvedCardIds;let t=new Set;if(this.filter.exclude.length>0)for(let e of this.filter.exclude)try{(await getTag(this.courseId,e)).taggedCards.forEach(e=>t.add(e))}catch(t){logger.warn(`[TagFilteredContentSource] Could not resolve tag "${e}" for exclusion:`,t)}let n=new Set;for(let r of e)t.has(r)||n.add(r);return logger.info(`[TagFilteredContentSource] Resolved ${n.size} cards (included: ${e.size}, excluded: ${t.size})`),this.resolvedCardIds=n,n}async getWeightedCards(e){if(!hasActiveFilter(this.filter))return logger.warn(`[TagFilteredContentSource] getWeightedCards called with no active filter`),{cards:[]};let t=await this.resolveFilteredCardIds(),n=await this.user.getActiveCards(),r=new Set(n.map(e=>e.cardID)),a=[];for(let n of t)if(r.has(n)||a.push({cardId:n,courseId:this.courseId,score:1,provenance:[{strategy:`tagFilter`,strategyName:`Tag Filter`,strategyId:`TAG_FILTER`,action:`generated`,score:1,reason:`Tag-filtered new card (tags: ${this.filter.include.join(`, `)})`}]}),a.length>=e)break;logger.info(`[TagFilteredContentSource] Found ${a.length} new cards matching filter`);let o=await this.user.getPendingReviews(this.courseId),s=o.filter(e=>t.has(e.cardId));return logger.info(`[TagFilteredContentSource] Found ${s.length} pending reviews matching filter (of ${o.length} total)`),{cards:[...s.map(e=>({cardId:e.cardId,courseId:e.courseId,score:1,reviewID:e._id,provenance:[{strategy:`tagFilter`,strategyName:`Tag Filter`,strategyId:`TAG_FILTER`,action:`generated`,score:1,reason:`Tag-filtered review (tags: ${this.filter.include.join(`, `)})`}]})),...a].slice(0,e)}}clearCache(){this.resolvedCardIds=null}getCourseId(){return this.courseId}getFilter(){return this.filter}}}});function isReview(e){return e.status===`review`||e.status===`failed-review`||`reviewID`in e}async function getStudySource(e,t){return e.type===`classroom`?await StudentClassroomDB.factory(e.id,t):hasActiveFilter(e.tagFilter)?new TagFilteredContentSource(e.id,e.tagFilter,t):getDataLayer().getCourseDB(e.id)}var init_contentSource=__esm({"src/core/interfaces/contentSource.ts"(){init_factory(),init_classroomDB2(),init_TagFilteredContentSource()}}),init_courseDB3=__esm({"src/core/interfaces/courseDB.ts"(){}}),init_dataLayerProvider=__esm({"src/core/interfaces/dataLayerProvider.ts"(){}}),init_userDB=__esm({"src/core/interfaces/userDB.ts"(){}}),init_interfaces=__esm({"src/core/interfaces/index.ts"(){init_adminDB(),init_classroomDB(),init_contentSource(),init_courseDB3(),init_dataLayerProvider(),init_userDB()}}),init_user=__esm({"src/core/types/user.ts"(){}});function buildStrategyStateId(e,t){return`STRATEGY_STATE::${e}::${t}`}var init_strategyState=__esm({"src/core/types/strategyState.ts"(){}}),init_userOutcome=__esm({"src/core/types/userOutcome.ts"(){}});async function importParsedCards(e,t,n){let r=[];for(let a of e)try{let e=await processCard(a,t,n);r.push(e)}catch(e){logger.error(`Error processing card:`,e);let t=a.markdown;a.tags&&a.tags.length>0&&(t+=`
282
282
  tags: ${a.tags.join(`, `)}`),a.elo!==void 0&&(t+=`
283
283
  elo: ${a.elo}`),r.push({originalText:t,status:`error`,message:`Error processing card: ${e instanceof Error?e.message:`Unknown error`}`})}return r}async function processCard(e,t,n){let{markdown:r,tags:a,elo:o}=e,s=r;a.length>0&&(s+=`
284
284
  tags: ${a.join(`, `)}`),o!==void 0&&(s+=`
@@ -363,14 +363,14 @@ Example:
363
363
  window.skuilder.session.showQueue()
364
364
  `)}};function mountSessionDebugger(){if(typeof window>`u`)return;let e=window;e.skuilder=e.skuilder||{},e.skuilder.session=sessionDebugAPI}mountSessionDebugger(),init_logger();var SessionController=(_SessionController2=class _SessionController extends Loggable{set sessionRecord(e){this._sessionRecord=e}get secondsRemaining(){return this._secondsRemaining}get hasCardGuarantee(){return this._minCardsGuarantee>0}get report(){let e=this.reviewQ.dequeueCount,t=this.newQ.dequeueCount;return`${e} ${e===1?`review`:`reviews`}, ${t} ${t===1?`new card`:`new cards`}`}get detailedReport(){return this.newQ.toString+`
365
365
  `+this.reviewQ.toString+`
366
- `+this.failedQ.toString}constructor(e,t,n,r,a,o){super(),_defineProperty(this,`_className`,`SessionController`),_defineProperty(this,`services`,void 0),_defineProperty(this,`srsService`,void 0),_defineProperty(this,`eloService`,void 0),_defineProperty(this,`hydrationService`,void 0),_defineProperty(this,`mixer`,void 0),_defineProperty(this,`dataLayer`,void 0),_defineProperty(this,`courseNameCache`,new Map),_defineProperty(this,`_defaultBatchLimit`,20),_defineProperty(this,`_initialReviewCap`,200),_defineProperty(this,`sources`,void 0),_defineProperty(this,`_sessionRecord`,[]),_defineProperty(this,`_currentCard`,null),_defineProperty(this,`reviewQ`,new ItemQueue),_defineProperty(this,`newQ`,new ItemQueue),_defineProperty(this,`failedQ`,new ItemQueue),_defineProperty(this,`_replanPromise`,null),_defineProperty(this,`_wellIndicatedRemaining`,0),_defineProperty(this,`_suppressQualityReplan`,!1),_defineProperty(this,`_depletionReplanAttempted`,!1),_defineProperty(this,`_minCardsGuarantee`,0),_defineProperty(this,`startTime`,void 0),_defineProperty(this,`endTime`,void 0),_defineProperty(this,`_secondsRemaining`,void 0),_defineProperty(this,`_intervalHandle`,void 0),this.dataLayer=n,this.mixer=a||new QuotaRoundRobinMixer,this.srsService=new SrsService(n.getUserDB()),this.eloService=new EloService(n,n.getUserDB()),this.hydrationService=new CardHydrationService(r,e=>n.getCourseDB(e),()=>this._getItemsToHydrate()),this.services={response:new ResponseProcessor(this.srsService,this.eloService)},this.sources=e,this.startTime=new Date,this._secondsRemaining=t,this.endTime=new Date(this.startTime.valueOf()+1e3*this._secondsRemaining),o?.defaultBatchLimit!==void 0&&(this._defaultBatchLimit=o.defaultBatchLimit),o?.initialReviewCap!==void 0&&(this._initialReviewCap=o.initialReviewCap),this.log(`Session constructed:
366
+ `+this.failedQ.toString}constructor(e,t,n,r,a,o){super(),_defineProperty(this,`_className`,`SessionController`),_defineProperty(this,`services`,void 0),_defineProperty(this,`srsService`,void 0),_defineProperty(this,`eloService`,void 0),_defineProperty(this,`hydrationService`,void 0),_defineProperty(this,`mixer`,void 0),_defineProperty(this,`dataLayer`,void 0),_defineProperty(this,`courseNameCache`,new Map),_defineProperty(this,`_defaultBatchLimit`,20),_defineProperty(this,`_initialReviewCap`,200),_defineProperty(this,`sources`,void 0),_defineProperty(this,`_sessionRecord`,[]),_defineProperty(this,`_currentCard`,null),_defineProperty(this,`reviewQ`,new ItemQueue),_defineProperty(this,`newQ`,new ItemQueue),_defineProperty(this,`failedQ`,new ItemQueue),_defineProperty(this,`_replanPromise`,null),_defineProperty(this,`_wellIndicatedRemaining`,0),_defineProperty(this,`_suppressQualityReplan`,!1),_defineProperty(this,`_minCardsGuarantee`,0),_defineProperty(this,`startTime`,void 0),_defineProperty(this,`endTime`,void 0),_defineProperty(this,`_secondsRemaining`,void 0),_defineProperty(this,`_intervalHandle`,void 0),this.dataLayer=n,this.mixer=a||new QuotaRoundRobinMixer,this.srsService=new SrsService(n.getUserDB()),this.eloService=new EloService(n,n.getUserDB()),this.hydrationService=new CardHydrationService(r,e=>n.getCourseDB(e),()=>this._getItemsToHydrate()),this.services={response:new ResponseProcessor(this.srsService,this.eloService)},this.sources=e,this.startTime=new Date,this._secondsRemaining=t,this.endTime=new Date(this.startTime.valueOf()+1e3*this._secondsRemaining),o?.defaultBatchLimit!==void 0&&(this._defaultBatchLimit=o.defaultBatchLimit),o?.initialReviewCap!==void 0&&(this._initialReviewCap=o.initialReviewCap),this.log(`Session constructed:
367
367
  startTime: ${this.startTime}
368
368
  endTime: ${this.endTime}
369
369
  defaultBatchLimit: ${this._defaultBatchLimit}
370
- initialReviewCap: ${this._initialReviewCap}`)}tick(){this._secondsRemaining=Math.floor((this.endTime.valueOf()-Date.now())/1e3),this._secondsRemaining<=0&&clearInterval(this._intervalHandle)}estimateCleanupTime(){let e=0;for(let t=0;t<this.failedQ.length;t++){let n=this.failedQ.peek(t),r=this._sessionRecord.find(e=>e.item.cardID===n.cardID),a=0;if(r){for(let e=0;e<r.records.length;e++)a+=r.records[e].timeSpent;a/=r.records.length,e+=a}}let t=e/1e3;return this.log(`Failed card cleanup estimate: ${Math.round(t)}`),t}estimateReviewTime(){let e=5*this.reviewQ.length;return this.log(`Review card time estimate: ${e}`),e}async prepareSession(){if(this.sources.some(e=>typeof e.getWeightedCards!=`function`))throw Error(`[SessionController] All content sources must implement getWeightedCards().`);let e=await this.getWeightedContent();this._wellIndicatedRemaining=e,e>=0&&e<_SessionController.MIN_WELL_INDICATED&&this.log(`[Init] Only ${e}/${_SessionController.MIN_WELL_INDICATED} well-indicated cards in initial load`),await this.hydrationService.ensureHydratedCards(),startSessionTracking(this.reviewQ.length,this.newQ.length,this.failedQ.length),this._intervalHandle=setInterval(()=>{this.tick()},1e3)}async requestReplan(e){let t=this.normalizeReplanOptions(e);(t.hints||t.label||t.limit)&&(this._depletionReplanAttempted=!1);let n=this._replanHasIntent(t);if(this._replanPromise){if(!n)return this.log(`Replan already in progress, coalescing unhinted auto-replan`),this._replanPromise;let e=t.label?` [${t.label}]`:``;this.log(`Replan in progress; queueing hint-bearing replan${e} behind in-flight run`);let r=this._replanPromise.catch(()=>void 0).then(()=>this._runReplan(t));return this._replanPromise=r.finally(()=>{this._replanPromise===r&&(this._replanPromise=null)}),r}let r=this._runReplan(t);this._replanPromise=r.finally(()=>{this._replanPromise===r&&(this._replanPromise=null)}),await r}_replanHasIntent(e){return!!(e.label||e.limit!==void 0||e.minFollowUpCards!==void 0||e.mode&&e.mode!==`replace`||e.hints&&Object.keys(e.hints).length>0)}async _runReplan(e){e.hints||(e.hints={});let t=e.hints,n=new Set(t.excludeCards??[]);this._currentCard?.item.cardID&&n.add(this._currentCard.item.cardID);for(let e of this._sessionRecord)n.add(e.card.card_id);if(this.newQ.length>0&&n.add(this.newQ.peek(0).cardID),t.excludeCards=[...n],e.hints){let t=e.label?{...e.hints,_label:e.label}:e.hints;for(let e of this.sources)e.setEphemeralHints?.(t)}let r=e.label?` [${e.label}]`:``;this.log(`Mid-session replan requested${r} (limit: ${e.limit??`default`}, mode: ${e.mode??`replace`}${e.hints?`, with hints`:``})`),e.minFollowUpCards!==void 0&&e.minFollowUpCards>0&&(this._minCardsGuarantee=Math.max(this._minCardsGuarantee,e.minFollowUpCards),this.log(`[Replan] Card guarantee set to ${this._minCardsGuarantee}`)),await this._executeReplan(e)}normalizeReplanOptions(e){if(!e)return{};let t=[`hints`,`limit`,`mode`,`label`,`minFollowUpCards`];return Object.keys(e).some(e=>t.includes(e))?e:{hints:e}}async _executeReplan(e={}){let t=e.limit,n=e.mode??`replace`,r=await this.getWeightedContent({replan:!0,additive:n===`merge`,limit:t});this._wellIndicatedRemaining=r,t!==void 0&&t<this._defaultBatchLimit?(this._suppressQualityReplan=!0,this.log(`[Replan] Burst mode (limit=${t}): suppressing quality-based auto-replan`)):this._suppressQualityReplan=!1,r>=0&&r<_SessionController.MIN_WELL_INDICATED&&this.log(`[Replan] Only ${r}/${_SessionController.MIN_WELL_INDICATED} well-indicated cards after replan`),this.newQ.length>0&&(this._depletionReplanAttempted=!1),await this.hydrationService.ensureHydratedCards();let a=e.label?` [${e.label}]`:``;this.log(`Replan complete${a}: newQ now has ${this.newQ.length} cards (mode=${n})`),snapshotQueues(this.reviewQ.length,this.newQ.length,this.failedQ.length)}addTime(e){this.endTime=new Date(this.endTime.valueOf()+1e3*e)}get failedCount(){return this.failedQ.length}toString(){return`Session: ${this.reviewQ.length} Reviews, ${this.newQ.length} New, ${this.failedQ.length} failed`}reportString(){return`${this.reviewQ.dequeueCount} Reviews, ${this.newQ.dequeueCount} New, ${this.failedQ.dequeueCount} failed`}getDebugInfo(){let e=this.sources.some(e=>typeof e.getWeightedCards==`function`),extractQueueItems=(e,t=10)=>{let n=[];for(let r=0;r<Math.min(e.length,t);r++){let t=e.peek(r);n.push({courseID:t.courseID||`unknown`,cardID:t.cardID||`unknown`,status:t.status||`unknown`})}return n};return{api:{mode:e?`weighted`:`legacy`,description:e?`Using getWeightedCards() API with scored candidates`:`ERROR: getWeightedCards() not a function.`},reviewQueue:{length:this.reviewQ.length,dequeueCount:this.reviewQ.dequeueCount,items:extractQueueItems(this.reviewQ)},newQueue:{length:this.newQ.length,dequeueCount:this.newQ.dequeueCount,items:extractQueueItems(this.newQ)},failedQueue:{length:this.failedQ.length,dequeueCount:this.failedQ.dequeueCount,items:extractQueueItems(this.failedQ)},hydratedCache:{count:this.hydrationService.hydratedCount,cardIds:this.hydrationService.getHydratedCardIds()},replan:{inProgress:this._replanPromise!==null,suppressQualityReplan:this._suppressQualityReplan,defaultBatchLimit:this._defaultBatchLimit,minCardsGuarantee:this._minCardsGuarantee}}}async getWeightedContent(e){let t=e?.replan??!1,n=e?.additive??!1,r=e?.limit??this._defaultBatchLimit,a=t?r:r+this._initialReviewCap,o=[];for(let e=0;e<this.sources.length;e++){let t=this.sources[e];try{let n=(await t.getWeightedCards(a)).cards;o.push({sourceIndex:e,weighted:n})}catch(t){if(this.error(`Failed to get content from source ${e}:`,t),this.sources.length===1)throw Error(`Cannot start session: failed to load content from source ${e}`)}}if(o.length===0){if(t)return this.log(`Replan: no content from any source, keeping existing newQ`),-1;throw Error(`Cannot start session: failed to load content from all ${this.sources.length} source(s). Check logs for details.`)}let s=this.mixer.mix(o,a*this.sources.length),c=o.map(e=>e.weighted[0]?.courseId||`source-${e.sourceIndex}`);await Promise.all(c.map(async e=>{if(!this.courseNameCache.has(e))try{let t=await this.dataLayer.getCoursesDB().getCourseConfig(e);this.courseNameCache.set(e,t.name)}catch{}}));let l=c.map(e=>this.courseNameCache.get(e)),u=this.mixer instanceof QuotaRoundRobinMixer?Math.ceil(a*this.sources.length/o.length):void 0;captureMixerRun(this.mixer.constructor.name,o,c,l,a*this.sources.length,u,s);let d=s.filter(e=>getCardOrigin(e)===`review`).slice(0,this._initialReviewCap),p=s.filter(e=>getCardOrigin(e)===`new`).slice(0,r);logger.debug(`[reviews] got ${d.length} reviews from mixer`);let m=t?`Replan content:
370
+ initialReviewCap: ${this._initialReviewCap}`)}tick(){this._secondsRemaining=Math.floor((this.endTime.valueOf()-Date.now())/1e3),this._secondsRemaining<=0&&clearInterval(this._intervalHandle)}estimateCleanupTime(){let e=0;for(let t=0;t<this.failedQ.length;t++){let n=this.failedQ.peek(t),r=this._sessionRecord.find(e=>e.item.cardID===n.cardID),a=0;if(r){for(let e=0;e<r.records.length;e++)a+=r.records[e].timeSpent;a/=r.records.length,e+=a}}let t=e/1e3;return this.log(`Failed card cleanup estimate: ${Math.round(t)}`),t}estimateReviewTime(){let e=5*this.reviewQ.length;return this.log(`Review card time estimate: ${e}`),e}async prepareSession(){if(this.sources.some(e=>typeof e.getWeightedCards!=`function`))throw Error(`[SessionController] All content sources must implement getWeightedCards().`);let e=await this.getWeightedContent();this._wellIndicatedRemaining=e,e>=0&&e<_SessionController.MIN_WELL_INDICATED&&this.log(`[Init] Only ${e}/${_SessionController.MIN_WELL_INDICATED} well-indicated cards in initial load`),await this.hydrationService.ensureHydratedCards(),startSessionTracking(this.reviewQ.length,this.newQ.length,this.failedQ.length),this._intervalHandle=setInterval(()=>{this.tick()},1e3)}async requestReplan(e){let t=this.normalizeReplanOptions(e),n=this._replanHasIntent(t);if(this._replanPromise){if(!n)return this.log(`Replan already in progress, coalescing unhinted auto-replan`),this._replanPromise;let e=t.label?` [${t.label}]`:``;this.log(`Replan in progress; queueing hint-bearing replan${e} behind in-flight run`);let r=this._replanPromise.catch(()=>void 0).then(()=>this._runReplan(t));return this._replanPromise=r.finally(()=>{this._replanPromise===r&&(this._replanPromise=null)}),r}let r=this._runReplan(t);this._replanPromise=r.finally(()=>{this._replanPromise===r&&(this._replanPromise=null)}),await r}_replanHasIntent(e){return!!(e.label||e.limit!==void 0||e.minFollowUpCards!==void 0||e.mode&&e.mode!==`replace`||e.hints&&Object.keys(e.hints).length>0)}async _runReplan(e){e.hints||(e.hints={});let t=e.hints,n=new Set(t.excludeCards??[]);this._currentCard?.item.cardID&&n.add(this._currentCard.item.cardID);for(let e of this._sessionRecord)n.add(e.card.card_id);if(this.newQ.length>0&&n.add(this.newQ.peek(0).cardID),t.excludeCards=[...n],e.hints){let t=e.label?{...e.hints,_label:e.label}:e.hints;for(let e of this.sources)e.setEphemeralHints?.(t)}let r=e.label?` [${e.label}]`:``;this.log(`Mid-session replan requested${r} (limit: ${e.limit??`default`}, mode: ${e.mode??`replace`}${e.hints?`, with hints`:``})`),e.minFollowUpCards!==void 0&&e.minFollowUpCards>0&&(this._minCardsGuarantee=Math.max(this._minCardsGuarantee,e.minFollowUpCards),this.log(`[Replan] Card guarantee set to ${this._minCardsGuarantee}`)),await this._executeReplan(e)}async _replanUncoalesced(e){let t=this._runReplan(e);this._replanPromise=t.finally(()=>{this._replanPromise===t&&(this._replanPromise=null)}),await t}normalizeReplanOptions(e){if(!e)return{};let t=[`hints`,`limit`,`mode`,`label`,`minFollowUpCards`];return Object.keys(e).some(e=>t.includes(e))?e:{hints:e}}async _executeReplan(e={}){let t=e.limit,n=e.mode??`replace`,r=await this.getWeightedContent({replan:!0,additive:n===`merge`,limit:t});this._wellIndicatedRemaining=r,t!==void 0&&t<this._defaultBatchLimit?(this._suppressQualityReplan=!0,this.log(`[Replan] Burst mode (limit=${t}): suppressing quality-based auto-replan`)):this._suppressQualityReplan=!1,r>=0&&r<_SessionController.MIN_WELL_INDICATED&&this.log(`[Replan] Only ${r}/${_SessionController.MIN_WELL_INDICATED} well-indicated cards after replan`),await this.hydrationService.ensureHydratedCards();let a=e.label?` [${e.label}]`:``;this.log(`Replan complete${a}: newQ now has ${this.newQ.length} cards (mode=${n})`),snapshotQueues(this.reviewQ.length,this.newQ.length,this.failedQ.length)}addTime(e){this.endTime=new Date(this.endTime.valueOf()+1e3*e)}get failedCount(){return this.failedQ.length}toString(){return`Session: ${this.reviewQ.length} Reviews, ${this.newQ.length} New, ${this.failedQ.length} failed`}reportString(){return`${this.reviewQ.dequeueCount} Reviews, ${this.newQ.dequeueCount} New, ${this.failedQ.dequeueCount} failed`}getDebugInfo(){let e=this.sources.some(e=>typeof e.getWeightedCards==`function`),extractQueueItems=(e,t=10)=>{let n=[];for(let r=0;r<Math.min(e.length,t);r++){let t=e.peek(r);n.push({courseID:t.courseID||`unknown`,cardID:t.cardID||`unknown`,status:t.status||`unknown`})}return n};return{api:{mode:e?`weighted`:`legacy`,description:e?`Using getWeightedCards() API with scored candidates`:`ERROR: getWeightedCards() not a function.`},reviewQueue:{length:this.reviewQ.length,dequeueCount:this.reviewQ.dequeueCount,items:extractQueueItems(this.reviewQ)},newQueue:{length:this.newQ.length,dequeueCount:this.newQ.dequeueCount,items:extractQueueItems(this.newQ)},failedQueue:{length:this.failedQ.length,dequeueCount:this.failedQ.dequeueCount,items:extractQueueItems(this.failedQ)},hydratedCache:{count:this.hydrationService.hydratedCount,cardIds:this.hydrationService.getHydratedCardIds()},replan:{inProgress:this._replanPromise!==null,suppressQualityReplan:this._suppressQualityReplan,defaultBatchLimit:this._defaultBatchLimit,minCardsGuarantee:this._minCardsGuarantee}}}async getWeightedContent(e){let t=e?.replan??!1,n=e?.additive??!1,r=e?.limit??this._defaultBatchLimit,a=t?r:r+this._initialReviewCap,o=[];for(let e=0;e<this.sources.length;e++){let t=this.sources[e];try{let n=(await t.getWeightedCards(a)).cards;o.push({sourceIndex:e,weighted:n})}catch(t){if(this.error(`Failed to get content from source ${e}:`,t),this.sources.length===1)throw Error(`Cannot start session: failed to load content from source ${e}`)}}if(o.length===0){if(t)return this.log(`Replan: no content from any source, keeping existing newQ`),-1;throw Error(`Cannot start session: failed to load content from all ${this.sources.length} source(s). Check logs for details.`)}let s=this.mixer.mix(o,a*this.sources.length),c=o.map(e=>e.weighted[0]?.courseId||`source-${e.sourceIndex}`);await Promise.all(c.map(async e=>{if(!this.courseNameCache.has(e))try{let t=await this.dataLayer.getCoursesDB().getCourseConfig(e);this.courseNameCache.set(e,t.name)}catch{}}));let l=c.map(e=>this.courseNameCache.get(e)),u=this.mixer instanceof QuotaRoundRobinMixer?Math.ceil(a*this.sources.length/o.length):void 0;captureMixerRun(this.mixer.constructor.name,o,c,l,a*this.sources.length,u,s);let d=s.filter(e=>getCardOrigin(e)===`review`).slice(0,this._initialReviewCap),p=s.filter(e=>getCardOrigin(e)===`new`).slice(0,r);logger.debug(`[reviews] got ${d.length} reviews from mixer`);let m=t?`Replan content:
371
371
  `:`Mixed content session created with:
372
372
  `;if(!t)for(let e of d){let t={cardID:e.cardId,courseID:e.courseId,contentSourceType:`course`,contentSourceID:e.courseId,reviewID:e.reviewID,status:`review`};this.reviewQ.add(t,t.cardID),m+=`Review: ${e.courseId}::${e.cardId} (score: ${e.score.toFixed(2)})
373
373
  `}let g=p.filter(e=>e.score>=_SessionController.WELL_INDICATED_SCORE).length,_=[];for(let e of p){let t={cardID:e.cardId,courseID:e.courseId,contentSourceType:`course`,contentSourceID:e.courseId,status:`new`};_.push(t),m+=`New: ${e.courseId}::${e.cardId} (score: ${e.score.toFixed(2)})
374
374
  `}if(n){let e=this.newQ.mergeToFront(_,e=>e.cardID);m+=`Additive merge: ${e} new cards added to front of newQ
375
- `}else if(t)this.newQ.replaceAll(_,e=>e.cardID);else for(let e of _)this.newQ.add(e,e.cardID);return this.log(m),g}_getItemsToHydrate(){let e=[],t=2;for(let t=0;t<Math.min(2,this.reviewQ.length);t++)e.push(this.reviewQ.peek(t));for(let t=0;t<Math.min(2,this.newQ.length);t++)e.push(this.newQ.peek(t));for(let t=0;t<Math.min(2,this.failedQ.length);t++)e.push(this.failedQ.peek(t));return e}_selectNextItemToHydrate(){let e=Math.random(),t=.1,n=.75;if(this.reviewQ.length===0&&this.failedQ.length===0&&this.newQ.length===0||this._secondsRemaining<2&&this.failedQ.length===0&&this._minCardsGuarantee<=0)return null;if(this._secondsRemaining<=0&&this._minCardsGuarantee<=0)return this.failedQ.length>0?this.failedQ.peek(0):null;if(this.newQ.dequeueCount<this.sources.length&&this.newQ.length)return this.newQ.peek(0);let r=this.estimateCleanupTime(),a=this.estimateReviewTime();return this._secondsRemaining-(r+a)>20?(t=.5,n=.9):this._secondsRemaining-r>20?(t=.05,n=.9):(t=.01,n=.1),this.failedQ.length===0&&(n=1),this.reviewQ.length===0&&(t=n),e<t&&this.newQ.length?this.newQ.peek(0):e<n&&this.reviewQ.length?this.reviewQ.peek(0):this.failedQ.length?this.failedQ.peek(0):(this.log(`No more cards available for the session!`),null)}async nextCard(e=`dismiss-success`){if(this.dismissCurrentCard(e),this._minCardsGuarantee>0&&(this._minCardsGuarantee--,this.log(`[CardGuarantee] ${this._minCardsGuarantee} guaranteed cards remaining`)),this._replanPromise&&(this.log(`nextCard: awaiting in-flight replan before drawing`),await this._replanPromise),this.newQ.length<=1&&this._secondsRemaining>0&&!this._replanPromise&&!this._depletionReplanAttempted){this._suppressQualityReplan=!1,this._depletionReplanAttempted=!0;let e=this.reviewQ.length+this.failedQ.length;this.newQ.length===0&&e===0?(this.log(`[AutoReplan:depletion] All queues empty with ${this._secondsRemaining}s remaining. Awaiting replan.`),await this.requestReplan()):(this.log(`[AutoReplan:depletion] newQ has ${this.newQ.length} card(s) (${e} in other queues) with ${this._secondsRemaining}s remaining. Triggering background replan.`),this.requestReplan())}if(!this._suppressQualityReplan&&this._wellIndicatedRemaining<=3&&this.newQ.length>0&&!this._replanPromise&&(this.log(`[AutoReplan:quality] ${this._wellIndicatedRemaining} well-indicated cards remaining (newQ: ${this.newQ.length}). Triggering background replan.`),this.requestReplan()),this._secondsRemaining<=0&&this.failedQ.length===0&&this._minCardsGuarantee<=0)return this._currentCard=null,endSessionTracking(),null;let t=20;for(let e=0;e<20;e++){let e=this._selectNextItemToHydrate();if(!e)return this._currentCard=null,endSessionTracking(),null;let t=this.hydrationService.getHydratedCard(e.cardID);if(t||(t=await this.hydrationService.waitForCard(e.cardID)),this.removeItemFromQueue(e),t){await this.hydrationService.ensureHydratedCards(),this._currentCard=t;let n=e.status===`review`||e.status===`failed-review`?`review`:e.status===`new`||e.status===`failed-new`?`new`:`failed`,r=e.status.startsWith(`failed`)?`failedQ`:e.status===`review`?`reviewQ`:`newQ`;return recordCardPresentation(e.cardID,e.courseID,this.courseNameCache.get(e.courseID),n,r),snapshotQueues(this.reviewQ.length,this.newQ.length,this.failedQ.length),t}this.log(`Skipping card ${e.cardID}: hydration failed, trying next`),isReview(e)&&this.srsService.removeReview(e.reviewID)}return this.log(`Exhausted 20 skip attempts finding a hydratable card`),this._currentCard=null,endSessionTracking(),null}async submitResponse(e,t,n,r,a,o,s,c,l){let u={...r.item};return await this.services.response.processResponse(e,t,u,n,r,a,o,s,c,l)}dismissCurrentCard(e=`dismiss-success`){if(this._currentCard)if(e===`dismiss-success`)this.hydrationService.removeCard(this._currentCard.item.cardID);else if(e===`marked-failed`){let e;e=isReview(this._currentCard.item)?{cardID:this._currentCard.item.cardID,courseID:this._currentCard.item.courseID,contentSourceID:this._currentCard.item.contentSourceID,contentSourceType:this._currentCard.item.contentSourceType,status:`failed-review`,reviewID:this._currentCard.item.reviewID}:{cardID:this._currentCard.item.cardID,courseID:this._currentCard.item.courseID,contentSourceID:this._currentCard.item.contentSourceID,contentSourceType:this._currentCard.item.contentSourceType,status:`failed-new`},this.failedQ.add(e,e.cardID)}else (e===`dismiss-error`||e===`dismiss-failed`)&&this.hydrationService.removeCard(this._currentCard.item.cardID)}removeItemFromQueue(e){this.reviewQ.peek(0)?.cardID===e.cardID?this.reviewQ.dequeue(e=>e.cardID):this.newQ.peek(0)?.cardID===e.cardID?(this.newQ.dequeue(e=>e.cardID),this._wellIndicatedRemaining>0&&this._wellIndicatedRemaining--):this.failedQ.peek(0)?.cardID===e.cardID&&this.failedQ.dequeue(e=>e.cardID)}async endSession(){if(!this._sessionRecord||this._sessionRecord.length===0)return;let e=this._sessionRecord.flatMap(e=>e.records).filter(e=>e.userAnswer!==void 0);if(e.length===0)return;let t=null,n=[];for(let e of this.sources)if(e.getOrchestrationContext){try{t=await e.getOrchestrationContext(),e.getStrategyIds&&n.push(...e.getStrategyIds())}catch(e){logger.warn(`[SessionController] Failed to get orchestration context: ${e}`)}if(t)break}if(!t){logger.debug(`[SessionController] No orchestration context available, skipping outcome recording`);return}let r=new Date().toISOString(),a=new Date(this.startTime).toISOString();await recordUserOutcome(t,a,r,e,n)}},_defineProperty(_SessionController2,`MIN_WELL_INDICATED`,5),_defineProperty(_SessionController2,`WELL_INDICATED_SCORE`,.1),_SessionController2);init_TagFilteredContentSource(),init_factory();export{processCustomQuestionsData as $,getDbPath as A,isFilter as B,createOrchestrationContext as C,getCardHistoryID as D,getAppDataDirectory as E,hasRegisteredNavigator as F,log as G,isQuestionRecord as H,importParsedCards as I,mountPipelineDebugger as J,mixerDebugAPI as K,initializeDataDirectory as L,getRegisteredNavigatorNames as M,getRegisteredNavigatorRole as N,getCardOrigin as O,getStudySource as P,pipelineDebugAPI as Q,initializeDataLayer as R,computeSpread as S,ensureAppDataDirectory as T,isQuestionTypeRegistered as U,isGenerator as V,isReview as W,mountUserDBDebugger as X,mountSessionDebugger as Y,newInterval as Z,buildStrategyStateId as _,validateStaticCourse as _t,ENV as a,registerSeedData as at,computeEffectiveWeight as b,Loggable as c,scoreAccuracyInZone as ct,NavigatorRoles as d,startSessionTracking as dt,recordCardPresentation as et,Navigators as f,updateLearningState as ft,areQuestionRecords as g,validateProcessorConfig as gt,TagFilteredContentSource as h,validateMigration as ht,DocTypePrefixes as i,registerQuestionType as it,getRegisteredNavigator as j,getDataLayer as k,NOT_SET as l,sessionDebugAPI as lt,SessionController as m,userDBDebugAPI as mt,CourseLookup as n,registerDataShape as nt,FileSystemError as o,removeDataShape as ot,QuotaRoundRobinMixer as p,updateStrategyWeight as pt,mountMixerDebugger as q,DocType as r,registerNavigator as rt,GuestUsername as s,removeQuestionType as st,ContentNavigator as t,recordUserOutcome as tt,NavigatorRole as u,snapshotQueues as ut,captureMixerRun as v,endSessionTracking as w,computeOutcomeSignal as x,computeDeviation as y,initializeNavigatorRegistry as z};
376
- //# sourceMappingURL=dist-DXR8aZcS.js.map
375
+ `}else if(t)this.newQ.replaceAll(_,e=>e.cardID);else for(let e of _)this.newQ.add(e,e.cardID);return this.log(m),g}_getItemsToHydrate(){let e=[],t=2;for(let t=0;t<Math.min(2,this.reviewQ.length);t++)e.push(this.reviewQ.peek(t));for(let t=0;t<Math.min(2,this.newQ.length);t++)e.push(this.newQ.peek(t));for(let t=0;t<Math.min(2,this.failedQ.length);t++)e.push(this.failedQ.peek(t));return e}_selectNextItemToHydrate(){let e=Math.random(),t=.1,n=.75;if(this.reviewQ.length===0&&this.failedQ.length===0&&this.newQ.length===0||this._secondsRemaining<2&&this.failedQ.length===0&&this._minCardsGuarantee<=0)return null;if(this._secondsRemaining<=0&&this._minCardsGuarantee<=0)return this.failedQ.length>0?this.failedQ.peek(0):null;if(this.newQ.dequeueCount<this.sources.length&&this.newQ.length)return this.newQ.peek(0);let r=this.estimateCleanupTime(),a=this.estimateReviewTime();return this._secondsRemaining-(r+a)>20?(t=.5,n=.9):this._secondsRemaining-r>20?(t=.05,n=.9):(t=.01,n=.1),this.failedQ.length===0&&(n=1),this.reviewQ.length===0&&(t=n),e<t&&this.newQ.length?this.newQ.peek(0):e<n&&this.reviewQ.length?this.reviewQ.peek(0):this.failedQ.length?this.failedQ.peek(0):(this.log(`No more cards available for the session!`),null)}async nextCard(e=`dismiss-success`){if(this.dismissCurrentCard(e),this._minCardsGuarantee>0&&(this._minCardsGuarantee--,this.log(`[CardGuarantee] ${this._minCardsGuarantee} guaranteed cards remaining`)),this._replanPromise&&this.newQ.length===0&&this.reviewQ.length===0&&this.failedQ.length===0&&(this.log(`nextCard: queues empty, awaiting in-flight replan before drawing`),await this._replanPromise),this.newQ.length<=1&&this._secondsRemaining>0&&!this._replanPromise){this._suppressQualityReplan=!1;let e=this.reviewQ.length+this.failedQ.length;this.log(`[AutoReplan:depletion] newQ has ${this.newQ.length} card(s) (${e} in other queues) with ${this._secondsRemaining}s remaining. Triggering background replan.`),this.requestReplan()}if(!this._suppressQualityReplan&&this._wellIndicatedRemaining<=3&&this.newQ.length>0&&!this._replanPromise&&(this.log(`[AutoReplan:quality] ${this._wellIndicatedRemaining} well-indicated cards remaining (newQ: ${this.newQ.length}). Triggering background replan.`),this.requestReplan()),this._secondsRemaining<=0&&this.failedQ.length===0&&this._minCardsGuarantee<=0)return this._currentCard=null,endSessionTracking(),null;let t=3,n=250,r=0;for(;this._secondsRemaining>0&&this.newQ.length===0&&this.reviewQ.length===0&&this.failedQ.length===0;)if(this.log(`[WedgeBreaker] All queues empty with ${this._secondsRemaining}s remaining. Running pipeline (attempt ${r+1}/3).`),await this._replanUncoalesced({label:`wedge-breaker`}),this.newQ.length===0&&this.reviewQ.length===0&&this.failedQ.length===0){if(r++,r>=3){this.log(`[WedgeBreaker] Pipeline returned no content 3 consecutive times. Giving up; session will end.`);break}await new Promise(e=>setTimeout(e,250))}else r=0;let a=20;for(let e=0;e<20;e++){let e=this._selectNextItemToHydrate();if(!e)return this._currentCard=null,endSessionTracking(),null;let t=this.hydrationService.getHydratedCard(e.cardID);if(t||(t=await this.hydrationService.waitForCard(e.cardID)),this.removeItemFromQueue(e),t){await this.hydrationService.ensureHydratedCards(),this._currentCard=t;let n=e.status===`review`||e.status===`failed-review`?`review`:e.status===`new`||e.status===`failed-new`?`new`:`failed`,r=e.status.startsWith(`failed`)?`failedQ`:e.status===`review`?`reviewQ`:`newQ`;return recordCardPresentation(e.cardID,e.courseID,this.courseNameCache.get(e.courseID),n,r),snapshotQueues(this.reviewQ.length,this.newQ.length,this.failedQ.length),t}this.log(`Skipping card ${e.cardID}: hydration failed, trying next`),isReview(e)&&this.srsService.removeReview(e.reviewID)}return this.log(`Exhausted 20 skip attempts finding a hydratable card`),this._currentCard=null,endSessionTracking(),null}async submitResponse(e,t,n,r,a,o,s,c,l){let u={...r.item};return await this.services.response.processResponse(e,t,u,n,r,a,o,s,c,l)}dismissCurrentCard(e=`dismiss-success`){if(this._currentCard)if(e===`dismiss-success`)this.hydrationService.removeCard(this._currentCard.item.cardID);else if(e===`marked-failed`){let e;e=isReview(this._currentCard.item)?{cardID:this._currentCard.item.cardID,courseID:this._currentCard.item.courseID,contentSourceID:this._currentCard.item.contentSourceID,contentSourceType:this._currentCard.item.contentSourceType,status:`failed-review`,reviewID:this._currentCard.item.reviewID}:{cardID:this._currentCard.item.cardID,courseID:this._currentCard.item.courseID,contentSourceID:this._currentCard.item.contentSourceID,contentSourceType:this._currentCard.item.contentSourceType,status:`failed-new`},this.failedQ.add(e,e.cardID)}else (e===`dismiss-error`||e===`dismiss-failed`)&&this.hydrationService.removeCard(this._currentCard.item.cardID)}removeItemFromQueue(e){this.reviewQ.peek(0)?.cardID===e.cardID?this.reviewQ.dequeue(e=>e.cardID):this.newQ.peek(0)?.cardID===e.cardID?(this.newQ.dequeue(e=>e.cardID),this._wellIndicatedRemaining>0&&this._wellIndicatedRemaining--):this.failedQ.peek(0)?.cardID===e.cardID&&this.failedQ.dequeue(e=>e.cardID)}async endSession(){if(!this._sessionRecord||this._sessionRecord.length===0)return;let e=this._sessionRecord.flatMap(e=>e.records).filter(e=>e.userAnswer!==void 0);if(e.length===0)return;let t=null,n=[];for(let e of this.sources)if(e.getOrchestrationContext){try{t=await e.getOrchestrationContext(),e.getStrategyIds&&n.push(...e.getStrategyIds())}catch(e){logger.warn(`[SessionController] Failed to get orchestration context: ${e}`)}if(t)break}if(!t){logger.debug(`[SessionController] No orchestration context available, skipping outcome recording`);return}let r=new Date().toISOString(),a=new Date(this.startTime).toISOString();await recordUserOutcome(t,a,r,e,n)}},_defineProperty(_SessionController2,`MIN_WELL_INDICATED`,5),_defineProperty(_SessionController2,`WELL_INDICATED_SCORE`,.1),_SessionController2);init_TagFilteredContentSource(),init_factory();export{processCustomQuestionsData as $,getDbPath as A,isFilter as B,createOrchestrationContext as C,getCardHistoryID as D,getAppDataDirectory as E,hasRegisteredNavigator as F,log as G,isQuestionRecord as H,importParsedCards as I,mountPipelineDebugger as J,mixerDebugAPI as K,initializeDataDirectory as L,getRegisteredNavigatorNames as M,getRegisteredNavigatorRole as N,getCardOrigin as O,getStudySource as P,pipelineDebugAPI as Q,initializeDataLayer as R,computeSpread as S,ensureAppDataDirectory as T,isQuestionTypeRegistered as U,isGenerator as V,isReview as W,mountUserDBDebugger as X,mountSessionDebugger as Y,newInterval as Z,buildStrategyStateId as _,validateStaticCourse as _t,ENV as a,registerSeedData as at,computeEffectiveWeight as b,Loggable as c,scoreAccuracyInZone as ct,NavigatorRoles as d,startSessionTracking as dt,recordCardPresentation as et,Navigators as f,updateLearningState as ft,areQuestionRecords as g,validateProcessorConfig as gt,TagFilteredContentSource as h,validateMigration as ht,DocTypePrefixes as i,registerQuestionType as it,getRegisteredNavigator as j,getDataLayer as k,NOT_SET as l,sessionDebugAPI as lt,SessionController as m,userDBDebugAPI as mt,CourseLookup as n,registerDataShape as nt,FileSystemError as o,removeDataShape as ot,QuotaRoundRobinMixer as p,updateStrategyWeight as pt,mountMixerDebugger as q,DocType as r,registerNavigator as rt,GuestUsername as s,removeQuestionType as st,ContentNavigator as t,recordUserOutcome as tt,NavigatorRole as u,snapshotQueues as ut,captureMixerRun as v,endSessionTracking as w,computeOutcomeSignal as x,computeDeviation as y,initializeNavigatorRegistry as z};
376
+ //# sourceMappingURL=dist-B8xeftM2.js.map