import{u as R,r as e,j as s,i as L}from"./index-RapqkS1n.js";const oe=()=>{R();const[h,p]=e.useState(""),[i,b]=e.useState(!1),[d,l]=e.useState(null),[f,I]=e.useState(null),[S,x]=e.useState(null),[v,m]=e.useState(null),[C,T]=e.useState(""),[B,D]=e.useState(""),[M,U]=e.useState(""),[F,P]=e.useState(""),[K,W]=e.useState(""),[_,$]=e.useState(""),[q,z]=e.useState(!1),[A,G]=e.useState([]),[H,J]=e.useState(!1),[O,Q]=e.useState(null),[V,X]=e.useState(!1),[Y,Z]=e.useState(0),[ee,te]=e.useState(""),[y,se]=e.useState(!1),[ne,ae]=e.useState(null),w=e.useRef(null),[o,g]=e.useState(!0),r=e.useRef(null);e.useRef(!0);const u=e.useCallback(()=>{r.current&&o&&r.current.focus()},[o]);e.useEffect(()=>{const n=r.current;let c,a,t;return n&&o&&(u(),c=setInterval(()=>{document.activeElement!==n&&o&&u()},100),a=k=>{if(!o)return;k.target.closest("button, a, input, select, textarea")||u()},t=()=>{document.activeElement!==n&&o&&u()},document.addEventListener("click",a,!0),document.addEventListener("focusin",t),window.addEventListener("focus",t)),f&&f.status&&(g(!1),c&&clearInterval(c),a&&document.removeEventListener("click",a,!0),t&&(document.removeEventListener("focusin",t),window.removeEventListener("focus",t))),()=>{c&&clearInterval(c),a&&document.removeEventListener("click",a,!0),t&&(document.removeEventListener("focusin",t),window.removeEventListener("focus",t))}},[u,o,f]),e.useEffect(()=>()=>{w.current&&clearTimeout(w.current)},[]);const N=async n=>{var c;b(!0),l(null),x(null),m(null);try{const t=(await L.get(`/scanner?barcode=${encodeURIComponent(h)}`)).data;t.success?x(t.message):(t.suggestions&&m(t.suggestions),l(t.error||"Unknown error occurred"),g(!0))}catch(a){console.error("Barcode processing failed:",a);const t=(c=a.response)==null?void 0:c.data;t!=null&&t.suggestions?(m(t.suggestions),l(t.error)):l(a.message||"Unknown error occurred"),g(!0)}finally{if(b(!1),r.current){r.current.focus();const a=r.current.value.length;r.current.setSelectionRange(a,a)}}},j=e.useCallback(n=>{n.preventDefault(),N()},[]),E=n=>{p(n),N()};return s.jsxs("div",{className:"scanner-container p-6 rounded-lg w-full mx-auto bg-white shadow-lg",children:[s.jsx("h1",{className:"text-2xl font-semibold text-center text-gray-900 mb-6",children:"Scan Barcode"}),s.jsxs("form",{onSubmit:j,className:"space-y-4",children:[s.jsxs("div",{className:"relative",children:[s.jsx("input",{ref:r,type:"text",value:h,onChange:n=>p(n.target.value),placeholder:"Enter or scan barcode",disabled:i&&!y,className:"block w-full p-3 border border-gray-300 rounded-md focus:ring-[#ff6b00] focus:border-secondary text-gray-700",autoFocus:!0}),i&&y&&s.jsx("div",{className:"absolute right-3 top-1/2 transform -translate-y-1/2",children:s.jsx("div",{className:"animate-spin rounded-full h-5 w-5 border-b-2 border-secondary"})})]}),s.jsx("button",{type:"submit",disabled:i,className:"w-full py-3 bg-primary text-white rounded-md focus:outline-none focus:ring-2 focus:ring-[#ff6b00]",children:i?"Searching...":"Search"})]}),d&&s.jsxs("div",{className:"mt-4 text-red-600 text-center",children:[s.jsx("p",{children:d}),v&&s.jsxs("div",{children:[s.jsx("h3",{className:"text-lg font-semibold mb-2",children:"Matching Packages:"}),s.jsx("div",{className:"space-y-2",children:v.map(n=>s.jsx("button",{type:"button",onClick:()=>E(n.tracking_number),className:"w-full py-2 bg-gray-800 text-white rounded-md hover:bg-gray-700",children:n.tracking_number},n.id))})]})]}),S&&!d&&s.jsx("div",{className:"mt-6 p-4 bg-gray-50 rounded-md",children:s.jsx("p",{className:"text-center text-green-600 text-xl",children:S})})]})};export{oe as default};
