import{__awaiter as e,__generator as t,__assign as n}from"tslib";import{jsxs as r,jsx as a}from"react/jsx-runtime";import{useState as c,useRef as i,useEffect as o}from"react";import{Loader2 as s,Check as u}from"lucide-react";import{fetchStationsByKeyword as l}from"../../apis/scopeApis.js";import d from"../../styles/StationAutocomplete.module.css.js";var m=function(m){var p=m.value,f=m.onChange,h=m.onStationSelect,v=m.className,N=m.style,g=c([]),b=g[0],k=g[1],w=c(!1),y=w[0],C=w[1],S=c(!1),W=S[0],_=S[1],j=i(null),x=i(null),E=i(!1);return o(function(){var e=function(e){x.current&&!x.current.contains(e.target)&&W&&(_(!1),E.current||(f(""),h(null)))};return document.addEventListener("mousedown",e),function(){document.removeEventListener("mousedown",e)}},[W,f,h]),o(function(){return p.trim()?(j.current&&clearTimeout(j.current),C(!0),j.current=setTimeout(function(){return e(void 0,void 0,void 0,function(){var e,n;return t(this,function(t){switch(t.label){case 0:return t.trys.push([0,2,3,4]),[4,l(p,!0)];case 1:return(e=t.sent())&&e.data?k(e.data):k([]),[3,4];case 2:return n=t.sent(),console.error("Error fetching stations:",n),k([]),[3,4];case 3:return C(!1),[7];case 4:return[2]}})})},500)):(k([]),C(!1)),function(){j.current&&clearTimeout(j.current)}},[p]),r("div",n({className:"".concat(d.wrapper," ").concat(v||""),style:N,ref:x},{children:[r("div",n({className:d.inputWrapper},{children:[a("input",{type:"text",value:p,onChange:function(e){f(e.target.value),_(!0),h(null),E.current=!1},onFocus:function(){(b.length>0||p.length>0)&&_(!0)},placeholder:"Search for a station...",className:d.input}),y&&a("div",n({className:d.spinnerWrapper},{children:a(s,{className:d.spinner})}))]})),W&&p&&a("div",n({className:d.dropdown},{children:b.length>0?b.map(function(e){var t=!!e.ruleset_id;return r("div",n({className:"".concat(d.item," ").concat(t?d.itemOnboarded:d.itemClickable),onClick:function(){t||(f(e.station_name),h(e),_(!1),E.current=!0)}},{children:[r("div",n({className:d.itemLeft},{children:[t&&a("div",n({className:d.checkWrapper},{children:a(u,{size:12,strokeWidth:3})})),r("div",{children:[a("div",n({className:"".concat(d.stationName," ").concat(t?d.stationNameOnboarded:d.stationNameUnonboarded)},{children:e.station_name})),a("div",n({className:d.market},{children:e.market}))]})]})),t&&a("span",n({className:d.badge},{children:"Onboarded"}))]}),e.station_guid)}):!y&&a("div",n({className:d.noResults},{children:"No stations found"}))}))]}))};export{m as default};
//# sourceMappingURL=StationAutocomplete.js.map
