var e=e=>new Promise(t=>setTimeout(t,e));async function t(e){let t=await crypto.subtle.digest(`SHA-256`,e);return Array.from(new Uint8Array(t),e=>e.toString(16).padStart(2,`0`)).join(``)}function n(){return{open:!1,busy:!1,progress:``,error:``,controller:null,activeId:null,requests:new Map,emailRemovalOffers:[],contextVersion:0,resetContext(){this.contextVersion+=1,this.emailRemovalOffers=[],this.error=``},pick(){this.$refs.input&&!this.busy&&(this.open=!0,this.$refs.input.click())},async request(t,n,r){let i=document.querySelector(`meta[name="csrf-token"]`).content;for(let a=0;;a++)try{let e=await fetch(this.$root.dataset.api+t,{...n,signal:r,headers:{Accept:`application/json`,"X-CSRF-TOKEN":i,...n.headers}});if(e.redirected||e.status===401||e.status===419)throw Error(`Innloggingen må fornyes. Logg inn på nytt.`);if(e.ok)return e.status===204?null:await e.json();let a=await e.json().catch(()=>({})),o=Error(a.message||`Filen kunne ikke lastes opp.`);throw o.retryable=e.status>=500,o}catch(t){if(r?.aborted||a>=2||!(t instanceof TypeError||t.retryable))throw t;await e(250*2**a)}},async upload(e,t=null){let n=Array.from(e);if(n.length===0||this.busy)return;this.busy=!0,this.error=``,t===null&&(this.emailRemovalOffers=[]),this.open=!0,this.controller=new AbortController;let r=this.controller.signal,i=this.contextVersion;try{let e=Number(this.$root.dataset.projectId)||null;if(t!==null&&e!==t.projectId)throw Error(`Bytt tilbake til prosjektet og velg PDF-filen på nytt.`);let a=[];for(let o of n){r.throwIfAborted();try{e=await this.uploadFile(o,e,r,t!==null,i),this.emailRemovalOffers=this.emailRemovalOffers.filter(e=>e!==t)}catch(e){if(r.aborted)throw e;a.push(o.name+` – `+e.message)}}i===this.contextVersion&&(this.error=a.join(` `))}catch(e){this.error=r.aborted?`Opplastingen er avbrutt.`:e.message}finally{this.busy=!1,this.activeId=null,this.controller=null,this.$refs.input&&(this.$refs.input.value=``)}},async uploadFile(n,r,i,a,o){let s=/\.(?:png|jpe?g|webp)$/i.test(n.name);if(n.size<1||n.size>(s?35e5:25e6))throw Error(`Velg en fil på inntil 25 MB. Bilder kan være inntil 3,5 MB.`);this.progress=`Klargjør `+n.name+` …`;let c=await t(await n.arrayBuffer()),l=document.getElementById(`ai-classification`)?.value??`offentlig`,u=JSON.stringify([r,n.name,c,l,a]);this.requests.has(u)||this.requests.set(u,crypto.randomUUID());let d=await this.request(``,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({id:this.requests.get(u),project_id:r,name:n.name,bytes:n.size,sha256:c,classification:l,remove_email_addresses:a})},i);if(this.activeId=d.id,this.requests.set(u,d.id),d.state===`uploading`){let e=new Set(d.received);for(let r=0;r=f)throw Error(`Filen behandles fortsatt. Velg samme fil for å fortsette kontrollen.`);await e(1200),d=await this.request(`/`+d.id,{method:`GET`},i)}if(d.state!==`ready`)throw this.requests.delete(u),o===this.contextVersion&&!a&&d.error_code===`email_removal_available`&&this.emailRemovalOffers.push({id:d.id,file:n,projectId:r}),Error(d.error||`Filen kunne ikke leses.`);return await this.$wire.finishedUpload(d.id),this.requests.delete(u),d.project_id},async cancel(){let e=this.activeId;if(this.controller?.abort(),e!==null){for(let[t,n]of this.requests)n===e&&this.requests.delete(t);await this.request(`/`+e,{method:`DELETE`}).catch(e=>{this.error=e.message})}}}}var r=()=>Alpine.data(`aiAttachmentUpload`,n);window.Alpine?r():document.addEventListener(`alpine:init`,r,{once:!0});function i(){return{dirty:!1,saving:!1,uncertain:!1,expanded:!1,picking:!1,composing:!1,waitingForRevision:null,narrow:window.matchMedia(`(max-width: 1050px)`).matches,pendingNavigation:null,bypass:null,resetDraft(){this.dirty=!1,this.uncertain=!1,this.picking=!1,this.composing=!1,this.waitingForRevision=null},pickRevision(e){this.picking=!1,this.composing=this.narrow||this.expanded,this.$dispatch(`ai-word-revision-selected`,{target:e})},openRevision({requestId:e,id:t,number:n}){e===this.waitingForRevision&&(this.waitingForRevision=null,this.dirty||this.saving||this.uncertain||this.$dispatch(`ai-open-word`,{id:t,number:n}))},confirmNavigation(e){this.saving||(this.pendingNavigation=e,this.$refs.discard.showModal())},discardChanges(){let e=this.pendingNavigation;this.pendingNavigation=null,this.$refs.discard.close(),e&&e()},guardNavigation(e){let t=e.target.closest?.(`[data-ai-word-navigation]`),n=t instanceof HTMLFormElement;!t||t===this.bypass||e.type===`click`&&n||!this.saving&&!this.dirty&&!this.uncertain||(e.preventDefault(),e.stopImmediatePropagation(),this.confirmNavigation(()=>{this.bypass=t;try{n?(this.resetDraft(),t.requestSubmit()):t.click()}finally{this.bypass=null}}))},guardUnload(e){(this.dirty||this.saving||this.uncertain)&&(e.preventDefault(),e.returnValue=``)},chooseVersion(e,t){let n=e.target,r=String(t.version),i=Number(n.value),a=async()=>{try{await t.chooseVersion(i)||(n.value=r)}catch{n.value=r}};this.dirty||this.uncertain||this.saving?(n.value=r,this.confirmNavigation(a)):a()},async saveDraft(e){if(!this.saving){this.saving=!0;try{let t=await e.save();this.uncertain=typeof t!=`boolean`,t===!0&&(this.dirty=!1)}catch{this.uncertain=!0}finally{this.saving=!1}}}}}var a=()=>Alpine.data(`aiWordWorkbench`,i);window.Alpine?a():document.addEventListener(`alpine:init`,a,{once:!0});var o=document.querySelector(`form[data-portal-ticket]`);if(o instanceof HTMLFormElement){let e=new URLSearchParams(window.location.hash.slice(1)),t=o.elements.namedItem(`code`);window.history.replaceState(window.history.state,``,window.location.pathname+window.location.search),t instanceof HTMLInputElement&&(t.value=e.get(`id_code`)??``,o.requestSubmit())}