"use strict";(self.webpackChunkurncjp=self.webpackChunkurncjp||[]).push([[675],{675:(e,t,o)=>{o.r(t),o.d(t,{default:()=>b});var n=o(249),a=o(285),r=o(931),s=o(247),c=o(69),i=o.n(c),d=o(526),u=o(125);async function l(e="",t={}){const o=u.ServerConnection.makeSettings(),n=d.URLExt.join(o.baseUrl,"urncjp",e);let a;try{a=await u.ServerConnection.makeRequest(n,t,o)}catch(e){throw new u.ServerConnection.NetworkError(e)}let r=await a.text();if(r.length>0)try{r=JSON.parse(r)}catch(e){console.log("Not a JSON response body.",a,e)}if(!a.ok)throw new u.ServerConnection.ResponseError(a,r.message||r);return r}const m={id:"urncjp:plugin",description:"A JupyterLab extension to collect exam files",autoStart:!0,requires:[a.ICommandPalette,r.INotebookTracker],optional:[n.ISettingRegistry],activate:(e,t,o,n)=>{console.log("JupyterLab extension urncjp is activated!");const{commands:a}=e,r={},s=p(e,"jp-UserWidget"),c=p(e,"jp-SubmitWidget");a.addCommand("urncjp:submit",{label:"Submit Exam",caption:"Submit the exam to the server",execute:()=>{var e;const t=o.currentWidget;if(!t)return void console.error("No notebook is active");const n=null===(e=t.content.model)||void 0===e?void 0:e.toJSON();if(!n)return void console.error("Failed to get notebook data");const a=n;if(!a.metadata||!a.metadata.exam)return void console.log("Not an exam notebook. Skipping submission.");const s=a.metadata.exam,d=JSON.stringify(n),u=i()(d,{size:128}).toString(16);console.log("hash:",u),r[s]!==u?(r[s]=u,async function(e){return l("submit",{body:e,method:"POST"})}(JSON.stringify(n)).then((e=>{const{submission:t}=e,{createdAt:o,valid:n}=t,a=new Date(o).toLocaleString("de-DE");n?(c.node.textContent=`Submitted at ${a}`,c.node.style.backgroundColor="#006045"):(c.node.textContent="Exam submissions are closed",c.node.style.backgroundColor="#1d293d")})).catch((e=>{console.error("Failed to submit notebook:",e)}))):console.log("Notebook is unchanged. Skipping submission.")}}),a.addCommand("urncjp:status",{label:"Check Status",caption:"Check the status of the exam",execute:()=>{(async function(){return l("user")})().then((e=>{const{sub:t}=e;s.node.textContent=`Jupyter Exam: ${t}`}))}}),t.addItem({command:"urncjp:status",category:"Notebook Operations"}),t.addItem({command:"urncjp:submit",category:"Notebook Operations"}),o.currentChanged.connect(((e,t)=>{t&&(a.execute("urncjp:status"),t.context.saveState.connect(((e,t)=>{"completed"===t&&a.execute("urncjp:submit")})))})),n&&n.load(m.id).then((e=>{console.log("urncjp settings loaded:",e.composite)})).catch((e=>{console.error("Failed to load settings for urncjp.",e)}))}};function p(e,t){const o=document.createElement("div");o.textContent="";const n=new s.Widget({node:o});return n.id=a.DOMUtils.createDomID(),n.addClass(t),e.shell.add(n,"top",{rank:1e3}),n}const b=m}}]);