import{o as xe,r as t,w as he,j as e,L as z,v as J,q as me,X as fe}from"./index-oAPVDf-r.js";import{C as Ae}from"./circle-alert-C1Hs9zwh.js";import{A as Te}from"./index-B-MEeiYV.js";import{S as Ie}from"./shield-BWGzcrms.js";import{C as De}from"./check-CG8zylH4.js";/** * @license lucide-react v0.462.0 - ISC * * This source code is licensed under the ISC license. * See the LICENSE file in the root directory of this source tree. */const Pe=xe("Flashlight",[["path",{d:"M18 6c0 2-2 2-2 4v10a2 2 0 0 1-2 2h-4a2 2 0 0 1-2-2V10c0-2-2-2-2-4V2h12z",key:"1orkel"}],["line",{x1:"6",x2:"18",y1:"6",y2:"6",key:"1z11jq"}],["line",{x1:"12",x2:"12",y1:"12",y2:"12",key:"1f4yc1"}]]);/** * @license lucide-react v0.462.0 - ISC * * This source code is licensed under the ISC license. * See the LICENSE file in the root directory of this source tree. */const ze=xe("FlipHorizontal",[["path",{d:"M8 3H5a2 2 0 0 0-2 2v14c0 1.1.9 2 2 2h3",key:"1i73f7"}],["path",{d:"M16 3h3a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-3",key:"saxlbk"}],["path",{d:"M12 20v2",key:"1lh1kg"}],["path",{d:"M12 14v2",key:"8jcxud"}],["path",{d:"M12 8v2",key:"1woqiv"}],["path",{d:"M12 2v2",key:"tus03m"}]]),Ke=({mode:i,docTypeLabel:ge,hint:pe,onPhotoCapture:Q,onCancel:Z,onFrameCapture:ee,isUploading:be=!1})=>{const N=t.useRef(null),L=t.useRef(null),x=t.useRef(null),E=t.useRef(null),F=t.useRef(null),[A,je]=t.useState(i==="selfie"?"user":"environment"),[q,te]=t.useState(!1),[we,se]=t.useState(!1),B=t.useRef(null),[ke,j]=t.useState(!0),[g,H]=t.useState(!1),[T,w]=t.useState(null),[ae,re]=t.useState(!1),[u,W]=t.useState(null),[C,S]=t.useState("searching"),[ve,K]=t.useState(0),[U,ne]=t.useState(!1),I=t.useRef(!1),le=t.useRef(0),D=t.useRef(null),p=t.useRef(null),R=t.useCallback(()=>{x.current&&(x.current.getTracks().forEach(a=>a.stop()),x.current=null)},[]),V=t.useCallback(async a=>{var n,s;x.current&&(x.current.getTracks().forEach(r=>r.stop()),x.current=null),j(!0),H(!1),w(null);try{const r=await navigator.mediaDevices.getUserMedia({video:{facingMode:{ideal:a},width:{ideal:1920},height:{ideal:1080}},audio:!1});x.current=r;const c=r.getVideoTracks();B.current=c[0]??null,se(!!((s=(n=B.current)==null?void 0:n.getCapabilities)!=null&&s.call(n).torch));const l=N.current;if(!l){r.getTracks().forEach(m=>m.stop()),w("Internt fel — kameravyn saknas. Ladda om sidan."),j(!1);return}l.srcObject=r,l.onloadedmetadata=()=>{l.play().then(()=>{H(!0),j(!1)}).catch(m=>{console.error("[SecureCamera] play() failed:",m),w("Kunde inte visa kamerabilden. Prova att ladda om sidan."),j(!1)})}}catch(r){console.error("[SecureCamera] getUserMedia failed:",r);const c=(r==null?void 0:r.name)??"";w(c==="NotAllowedError"||c==="PermissionDeniedError"?"Kameratillåtelse nekades. Tillåt kameraåtkomst i webbläsarens inställningar och försök igen.":c==="NotFoundError"?"Ingen kamera hittades på enheten.":"Kunde inte starta kameran. Kontrollera kameratillståndet och försök igen."),j(!1)}},[]);t.useEffect(()=>{E.current=V},[V]),t.useEffect(()=>{F.current=ce}),t.useEffect(()=>(V(A),()=>{R()}),[]);const ye=t.useCallback(async()=>{var n;const a=B.current;if(a)try{const s=(n=a.getCapabilities)==null?void 0:n.call(a);if(!(s!=null&&s.torch))return;const r=!q;await a.applyConstraints({advanced:[{torch:r}]}),te(r)}catch(s){console.error("[SecureCamera] torch toggle failed",s),he.error("Ficklampan kunde inte slås på")}},[q]),Ne=t.useCallback(async()=>{var n;const a=A==="environment"?"user":"environment";je(a),W(null),H(!1),j(!0),te(!1),se(!1),await((n=E.current)==null?void 0:n.call(E,a))},[A]),X=t.useCallback((a,n,s,r,c)=>{const l=document.createElement("canvas");return l.width=Math.round(r),l.height=Math.round(c),l.getContext("2d").drawImage(a,n,s,r,c,0,0,l.width,l.height),l},[]),_=t.useRef(!1),ce=t.useCallback(async a=>{const n=a??N.current;if(!(!n||!L.current||_.current)){_.current=!0,re(!0);try{const s=L.current,r=s.getContext("2d");if(!r)throw new Error("Canvas context saknas");const c=n.videoWidth||1280,l=n.videoHeight||720,m=1400,b=c>m?m/c:1;s.width=Math.round(c*b),s.height=Math.round(l*b),r.drawImage(n,0,0,s.width,s.height);let k;if(i==="document"){const d=s.width*.86,h=d*(2/3),f=(s.width-d)/2,v=(s.height-h)/2;k=X(s,f,v,d,h)}else{const d=s.width*.7,h=d*(4/3),f=(s.width-d)/2,v=s.height*.45-h/2;k=X(s,f,v,d,h)}const O=await new Promise((d,h)=>{k.toBlob(f=>f?d(f):h(new Error("Blob misslyckades")),"image/jpeg",.92)}),$=k.toDataURL("image/jpeg",.92);W({blob:O,dataUrl:$,timestamp:Date.now(),deviceInfo:{userAgent:navigator.userAgent,platform:navigator.platform,timestamp:Date.now()}})}catch(s){console.error("[SecureCamera] capture failed:",s),he.error("Kunde inte ta fotot — försök igen")}finally{_.current=!1,re(!1)}}},[i,X]);t.useEffect(()=>{if(i!=="selfie"||!g||u)return;I.current=!1;let a=0;S("searching"),ne(!1),D.current=setTimeout(()=>{ne(!0)},12e3);const n=document.createElement("canvas");n.width=160,n.height=120;const s=n.getContext("2d"),r=600,c=5,l=m=>{if(I.current)return;const b=N.current;if(!b||b.readyState<2){p.current=requestAnimationFrame(l);return}if(m-le.current0?f/y:0,Me=y>0?v/y-P*P:0,Ee=Math.sqrt(Math.max(0,Me)),Fe=y>160*120*.03,oe=P>55&&P<230&&Ee>15&&Fe;if(ee&&oe)try{const o=document.createElement("canvas");o.width=320,o.height=240,o.getContext("2d").drawImage(N.current,0,0,320,240),ee(o.toDataURL("image/jpeg",.75).split(",")[1])}catch{}if(oe?(a++,K(a),S(a>=c?"capturing":a>=3?"detected":"searching")):(a=0,K(0),S("searching")),a>=c){I.current=!0,S("capturing"),setTimeout(()=>{var o;return(o=F.current)==null?void 0:o.call(F,b)},1500);return}p.current=requestAnimationFrame(l)};return p.current=requestAnimationFrame(l),()=>{I.current=!0,p.current!==null&&(cancelAnimationFrame(p.current),p.current=null),D.current!==null&&(clearTimeout(D.current),D.current=null)}},[i,g,u]);const Ce=t.useCallback(()=>{u&&(R(),Q(u))},[u,Q,R]),Se=t.useCallback(()=>{W(null),S("searching"),K(0)},[]),ie=t.useCallback(()=>{R(),Z()},[R,Z]),Re=C==="capturing"?"rgba(34,197,94,0.9)":C==="detected"?"rgba(234,179,8,0.9)":"rgba(255,255,255,0.5)";return e.jsxs("div",{className:"fixed inset-0 z-50 bg-black",children:[e.jsx("canvas",{ref:L,className:"hidden"}),(ke||!g)&&!T&&e.jsxs("div",{className:"absolute inset-0 flex flex-col items-center justify-center gap-4 text-white",children:[e.jsx(z,{className:"w-10 h-10 animate-spin"}),e.jsx("p",{className:"text-sm opacity-80",children:"Startar kameran..."})]}),T&&e.jsxs("div",{className:"absolute inset-0 flex flex-col items-center justify-center gap-5 p-8 text-white",children:[e.jsx(Ae,{className:"w-12 h-12 text-red-400 shrink-0"}),e.jsx("p",{className:"text-center text-sm leading-relaxed",children:T}),e.jsx(J,{onClick:ie,variant:"outline",className:"border-white/30 text-white hover:bg-white/10",children:"Stäng"})]}),!T&&e.jsxs(e.Fragment,{children:[e.jsx("video",{ref:N,autoPlay:!0,playsInline:!0,muted:!0,className:`absolute inset-0 w-full h-full object-cover transition-opacity duration-300 ${g&&!u?"opacity-100":"opacity-0"}`,style:i==="selfie"?{transform:"scaleX(-1)"}:void 0}),e.jsx(Te,{children:u&&e.jsx(me.img,{src:u.dataUrl,alt:"Förhandsgranskning",initial:{opacity:0},animate:{opacity:1},exit:{opacity:0},className:"absolute inset-0 w-full h-full object-contain bg-black"},"preview")}),be&&e.jsxs("div",{className:"absolute inset-0 bg-black/70 flex flex-col items-center justify-center gap-3 z-10",children:[e.jsx(z,{className:"w-10 h-10 text-white animate-spin"}),e.jsx("p",{className:"text-white text-sm",children:"Laddar upp bild..."})]}),g&&!u&&e.jsxs("div",{className:"absolute inset-0 pointer-events-none",children:[i==="document"?e.jsxs("svg",{className:"absolute inset-0 w-full h-full",viewBox:"0 0 100 100",preserveAspectRatio:"none",children:[e.jsx("defs",{children:e.jsxs("mask",{id:"docMask",children:[e.jsx("rect",{width:"100",height:"100",fill:"white"}),e.jsx("rect",{x:"7",y:"33",width:"86",height:"34",rx:"1",fill:"black"})]})}),e.jsx("rect",{width:"100",height:"100",fill:"rgba(0,0,0,0.55)",mask:"url(#docMask)"}),e.jsxs("g",{stroke:"white",strokeWidth:"0.8",fill:"none",opacity:"0.9",children:[e.jsx("polyline",{points:"7,38 7,33 12,33"}),e.jsx("polyline",{points:"88,38 88,33 83,33"}),e.jsx("polyline",{points:"7,62 7,67 12,67"}),e.jsx("polyline",{points:"88,62 88,67 83,67"})]})]}):e.jsxs("svg",{className:"absolute inset-0 w-full h-full",viewBox:"0 0 100 100",preserveAspectRatio:"none",children:[e.jsx("defs",{children:e.jsxs("mask",{id:"selfieMask",children:[e.jsx("rect",{width:"100",height:"100",fill:"white"}),e.jsx("ellipse",{cx:"50",cy:"45",rx:"32",ry:"40",fill:"black"})]})}),e.jsx("rect",{width:"100",height:"100",fill:"rgba(0,0,0,0.55)",mask:"url(#selfieMask)"}),e.jsx("ellipse",{cx:"50",cy:"45",rx:"32",ry:"40",stroke:Re,strokeWidth:"1.2",fill:"none"})]}),i==="selfie"&&e.jsx("div",{className:"absolute bottom-44 left-0 right-0 flex justify-center",children:e.jsxs("div",{className:"bg-black/70 backdrop-blur-sm px-4 py-2 rounded-full flex items-center gap-2",children:[C==="searching"&&e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"w-2.5 h-2.5 rounded-full bg-white/50 animate-pulse"}),e.jsx("p",{className:"text-white/80 text-xs",children:"Håll ansiktet inom ringen..."})]}),C==="detected"&&e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"w-2.5 h-2.5 rounded-full bg-yellow-400 animate-pulse"}),e.jsxs("p",{className:"text-yellow-300 text-xs",children:["Håll still... ",Math.max(0,Math.round((10-ve)*.8)),"s kvar"]})]}),C==="capturing"&&e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"w-2.5 h-2.5 rounded-full bg-green-400"}),e.jsx("p",{className:"text-green-300 text-xs",children:"📸"})]})]})}),e.jsx("div",{className:"absolute bottom-36 left-0 right-0 flex justify-center",children:e.jsx("div",{className:"bg-black/60 backdrop-blur-sm px-4 py-2 rounded-full",children:e.jsx("p",{className:"text-white/90 text-xs text-center",children:pe})})})]}),e.jsxs("div",{className:"absolute top-0 left-0 right-0 p-4 flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2 bg-green-500/20 backdrop-blur-sm border border-green-500/30 px-3 py-1.5 rounded-full",children:[e.jsx(Ie,{className:"w-3.5 h-3.5 text-green-400"}),e.jsx("span",{className:"text-xs text-green-400 font-medium",children:"Säker kamera"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[i==="document"&&we&&A==="environment"&&e.jsx("button",{onClick:ye,className:`w-9 h-9 rounded-full backdrop-blur-sm flex items-center justify-center transition-colors ${q?"bg-yellow-500/80":"bg-black/50"}`,title:"Ficklampa",children:e.jsx(Pe,{className:"w-4 h-4 text-white"})}),e.jsx("button",{onClick:Ne,className:"w-9 h-9 rounded-full bg-black/50 backdrop-blur-sm flex items-center justify-center",title:"Byt kamera",children:e.jsx(ze,{className:"w-4 h-4 text-white"})}),e.jsx("button",{onClick:ie,className:"w-9 h-9 rounded-full bg-black/50 backdrop-blur-sm flex items-center justify-center",children:e.jsx(fe,{className:"w-4 h-4 text-white"})})]})]}),g&&!u&&e.jsx("div",{className:"absolute top-16 left-0 right-0 flex justify-center pointer-events-none",children:e.jsx("div",{className:"bg-black/50 backdrop-blur-sm px-4 py-1.5 rounded-full",children:e.jsx("p",{className:"text-white text-sm font-medium",children:i==="selfie"?"Ta en selfie":`Fotografera ${ge}`})})}),e.jsx("div",{className:"absolute bottom-0 left-0 right-0 p-6",children:u?e.jsxs("div",{className:"space-y-3",children:[e.jsx("p",{className:"text-white/80 text-sm text-center",children:"Ser fotot bra ut? Kontrollera att allt är tydligt och välbelyst."}),e.jsxs("div",{className:"flex items-center justify-center gap-4",children:[e.jsxs(J,{onClick:Se,variant:"outline",size:"lg",className:"px-8 rounded-full bg-white/10 border-white/30 text-white hover:bg-white/20",children:[e.jsx(fe,{className:"w-4 h-4 mr-2"}),"Ta om"]}),e.jsxs(J,{onClick:Ce,size:"lg",className:"px-8 rounded-full bg-green-500 hover:bg-green-600 text-white",children:[e.jsx(De,{className:"w-4 h-4 mr-2"}),"Godkänn"]})]})]}):e.jsxs("div",{className:"flex flex-col items-center gap-4",children:[(i==="document"||U)&&e.jsx(me.button,{whileTap:{scale:.88},onClick:()=>ce(),disabled:ae||!g,className:"w-20 h-20 rounded-full bg-white flex items-center justify-center shadow-xl disabled:opacity-40 active:scale-90 transition-transform",children:e.jsx("div",{className:"w-16 h-16 rounded-full border-4 border-black/20 flex items-center justify-center",children:ae?e.jsx(z,{className:"w-7 h-7 animate-spin text-gray-600"}):e.jsx("div",{className:"w-10 h-10 rounded-full bg-gray-800"})})}),i==="selfie"&&U&&e.jsx("p",{className:"text-white/60 text-xs text-center",children:"Ta manuellt"}),i==="selfie"&&!U&&e.jsx("div",{className:"w-20 h-20 rounded-full border-2 border-white/20 flex items-center justify-center",children:e.jsx(z,{className:"w-6 h-6 text-white/40 animate-spin"})})]})})]})]})};export{Ke as S};