import{u as k,r as e,j as s,i as C}from"./index-D0o5GN6H.js";const re=()=>{k();const[u,m]=e.useState(""),[l,b]=e.useState(!1),[f,i]=e.useState(null),[S,T]=e.useState(null),[p,h]=e.useState(null),[I,N]=e.useState(""),[L,B]=e.useState(""),[D,M]=e.useState(""),[U,F]=e.useState(""),[P,K]=e.useState(""),[W,_]=e.useState(""),[$,q]=e.useState(!1),[z,A]=e.useState([]),[G,H]=e.useState(!1),[J,O]=e.useState(null),[Q,V]=e.useState(!1),[X,Y]=e.useState(0),[Z,ee]=e.useState(""),[j,te]=e.useState(!1),[se,ne]=e.useState(null),x=e.useRef(null),[v,y]=e.useState(null),[o,d]=e.useState(!0),a=e.useRef(null),g=e.useCallback(()=>{if(a.current&&o){a.current.focus();const t=a.current.value.length;a.current.setSelectionRange(t,t)}},[o]),w=async t=>{var c;b(!0),i(null),y(null),h(null);try{const n=(await C.get(`/scanner?barcode=${encodeURIComponent(u)}`)).data;n.success?y(n.message):(n.suggestions&&h(n.suggestions),i(n.error||"Unknown error occurred"),d(!0))}catch(r){console.error("Barcode processing failed:",r);const n=(c=r.response)==null?void 0:c.data;n!=null&&n.suggestions?(h(n.suggestions),i(n.error)):i(r.message||"Unknown error occurred"),d(!0)}finally{if(b(!1),a.current){a.current.focus();const r=a.current.value.length;a.current.setSelectionRange(r,r)}}},E=e.useCallback(t=>{t.preventDefault(),w()},[]);e.useEffect(()=>{let t;const c=()=>{var r;if(o&&document.activeElement!==a.current){const n=(r=a.current)==null?void 0:r.closest(".scanner-container");n&&!n.contains(document.activeElement)&&g()}};return o?(g(),t=setInterval(g,100),document.addEventListener("focusin",c),window.addEventListener("focus",c)):S&&m(""),()=>{t&&clearInterval(t),document.removeEventListener("focusin",c),window.removeEventListener("focus",c)}},[g,o,S]),e.useEffect(()=>{const t=setTimeout(()=>{N(u)},500);return()=>clearTimeout(t)},[u]),e.useEffect(()=>()=>{x.current&&clearTimeout(x.current)},[]);const R=t=>{m(t),w()};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:E,className:"space-y-4",children:[s.jsxs("div",{className:"relative",children:[s.jsx("input",{ref:a,type:"text",value:u,onChange:t=>m(t.target.value),placeholder:"Enter or scan barcode",disabled:l,className:"block w-full p-3 border border-gray-300 rounded-md focus:ring-[#ff6b00] focus:border-[#61CE70] text-gray-700",autoFocus:!0,onFocus:()=>d(!0)}),l&&j&&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-[#61CE70]"})})]}),s.jsx("button",{type:"submit",disabled:l,className:"w-full py-3 bg-[#f5b900] text-white rounded-md focus:outline-none focus:ring-2 focus:ring-[#ff6b00]",onClick:()=>d(!1),children:l?"Searching...":"Search"})]}),f&&s.jsxs("div",{className:"mt-4 text-red-600 text-center",children:[s.jsx("p",{children:f}),p&&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:p.map(t=>s.jsx("button",{type:"button",onClick:()=>R(t.tracking_number),className:"w-full py-2 bg-gray-800 text-white rounded-md hover:bg-gray-700",children:t.tracking_number},t.id))})]})]}),v&&!f&&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:v})})]})};export{re as default};
