import{n as T,u as E,j as e,B as g,k as j,l as v,L as D,I}from"./index-O0BQoyzo.js";import{r as w}from"./react-Cb2sDjhD.js";import{u as L,s as _,a as z,t as F}from"./use-settings-DMdaoWsB.js";import{L as K}from"./loader-circle-9VUMGitw.js";import{R as U}from"./refresh-ccw-D2CWiyU_.js";import{X as M}from"./x-DG-JKVw_.js";import{S as V}from"./save-DB0BDYTs.js";import{A as q}from"./arrow-up-63xELY5Q.js";import{A as J}from"./arrow-down-BXvC8Al2.js";import{C as W}from"./circle-check-CewnjFgv.js";import{C as G}from"./circle-x-Ccg1HyV-.js";import"./useQuery-PdiC7-sY.js";/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Q=T("FlaskConical",[["path",{d:"M14 2v6a2 2 0 0 0 .245.96l5.51 10.08A2 2 0 0 1 18 22H6a2 2 0 0 1-1.755-2.96l5.51-10.08A2 2 0 0 0 10 8V2",key:"18mbvz"}],["path",{d:"M6.453 15h11.094",key:"3shlmq"}],["path",{d:"M8.5 2h7",key:"csnxdl"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const X=T("Globe",[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["path",{d:"M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20",key:"13o1zl"}],["path",{d:"M2 12h20",key:"9i4pu4"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Y=T("Key",[["path",{d:"m15.5 7.5 2.3 2.3a1 1 0 0 0 1.4 0l2.1-2.1a1 1 0 0 0 0-1.4L19 4",key:"g0fldk"}],["path",{d:"m21 2-9.6 9.6",key:"1j0ho8"}],["circle",{cx:"7.5",cy:"15.5",r:"5.5",key:"yqb3hr"}]]);function Z(r,a){const o=(r??"").split(",").map(i=>i.trim()).filter(Boolean),h=new Set(a.map(i=>i.name)),m=new Set,x=[];for(const i of o)!h.has(i)||m.has(i)||(x.push({name:i,enabled:!0}),m.add(i));for(const i of a)m.has(i.name)||x.push({name:i.name,enabled:!1});return x}function B(r){return r.filter(a=>a.enabled).map(a=>a.name).join(",")}function ue(){const{t:r}=E(["settings","common"]),a=L({reveal:!1}),o=_(),h=z(),m=F(),x=a.data?.env??{},i=o.data?.providers??[],y=o.data?.chain??[],d=w.useMemo(()=>({chain:Z(x.IMHUB_WEB_SEARCH_PROVIDERS,i),keys:{},keysTouched:{}}),[x.IMHUB_WEB_SEARCH_PROVIDERS,i]),[l,u]=w.useState(d),[b,C]=w.useState("");w.useEffect(()=>{const s=JSON.stringify(d);s!==b&&(u(d),C(s))},[d,b]);const k=JSON.stringify(l.chain)!==JSON.stringify(d.chain)||Object.keys(l.keysTouched).length>0;function N(s,t){u(c=>{const n=[...c.chain],f=s+t;return f<0||f>=n.length?c:([n[s],n[f]]=[n[f],n[s]],{...c,chain:n})})}function S(s,t){u(c=>{const n=[...c.chain];return n[s]={...n[s],enabled:t},{...c,chain:n}})}function p(s,t){u(c=>({...c,keys:{...c.keys,[s]:t},keysTouched:{...c.keysTouched,[s]:!0}}))}function P(s){u(t=>({...t,keys:{...t.keys,[s]:""},keysTouched:{...t.keysTouched,[s]:!0}}))}async function A(){const s={},t=B(l.chain),c=B(d.chain);t!==c&&(s.IMHUB_WEB_SEARCH_PROVIDERS=t||null);for(const[n,f]of Object.entries(l.keysTouched)){if(!f)continue;const R=l.keys[n]??"";s[n]=R.trim()===""?null:R}if(Object.keys(s).length!==0)try{await h.mutateAsync({updates:s}),o.refetch(),v.success(r("search.savedToast")),u(n=>({...n,keys:{},keysTouched:{}}))}catch(n){v.error(n?.message??String(n))}}function H(){u(d)}async function O(s){try{const t=await m.mutateAsync({provider:s,query:"agim ping"});t.ok?v.success(r("search.testOk",{provider:s,hits:t.hits,ms:t.latencyMs})):v.error(r("search.testFail",{provider:s,error:t.error??"unknown error"}))}catch(t){v.error(t?.message??String(t))}}return e.jsxs("div",{className:"mx-auto flex max-w-4xl flex-col gap-6",children:[e.jsxs("header",{className:"flex items-center gap-3",children:[e.jsx("h1",{className:"text-xl font-semibold",children:r("search.title")}),e.jsxs(g,{variant:"ghost",size:"sm",className:"ml-auto",onClick:()=>{a.refetch(),o.refetch()},disabled:a.isFetching||o.isFetching,"aria-label":r("actions.refresh",{ns:"common"}),children:[a.isFetching||o.isFetching?e.jsx(K,{className:"h-4 w-4 animate-spin"}):e.jsx(U,{className:"h-4 w-4"}),e.jsx("span",{className:"hidden sm:inline",children:r("search.refresh")})]})]}),e.jsx("p",{className:"text-sm text-text-dim",children:r("search.subtitle")}),e.jsx($,{live:y}),a.isLoading||o.isLoading?e.jsx("div",{className:"h-64 rounded-md bg-surface-2 animate-pulse"}):e.jsx("section",{className:"flex flex-col gap-2",children:l.chain.map((s,t)=>{const c=i.find(n=>n.name===s.name);return e.jsx(ee,{info:c,entry:s,idx:t,count:l.chain.length,keyValue:c?.envKey?l.keys[c.envKey]:void 0,keyTouched:c?.envKey?!!l.keysTouched[c.envKey]:!1,onMove:n=>N(t,n),onToggle:n=>S(t,n),onSetKey:n=>c?.envKey&&p(c.envKey,n),onClearKey:()=>c?.envKey&&P(c.envKey),onTest:()=>O(s.name),testing:m.isPending},s.name)})}),k&&e.jsxs("div",{className:"sticky bottom-2 flex items-center gap-2 rounded-md border border-border bg-surface px-4 py-3 shadow",children:[e.jsx(j,{variant:"info",children:r("search.unsaved")}),e.jsxs(g,{variant:"ghost",size:"sm",className:"ml-auto",onClick:H,disabled:h.isPending,children:[e.jsx(M,{className:"h-4 w-4"})," ",r("search.discard")]}),e.jsxs(g,{size:"sm",onClick:()=>void A(),disabled:h.isPending,children:[h.isPending?e.jsx(K,{className:"h-4 w-4 animate-spin"}):e.jsx(V,{className:"h-4 w-4"}),r("search.save")]})]})]})}function $({live:r}){const{t:a}=E("settings");return e.jsxs("section",{className:"rounded-md border border-border bg-surface px-4 py-3",children:[e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(X,{className:"h-4 w-4 text-text-dim"}),e.jsx("span",{className:"text-text-dim",children:a("search.chainLabel")}),r.length===0?e.jsx(j,{variant:"outline",children:a("search.chainEmpty")}):e.jsx("span",{className:"font-mono text-xs",children:r.join(" → ")})]}),e.jsx("p",{className:"mt-1 text-[11px] text-text-dim",children:a("search.chainHint")})]})}function ee(r){const{t:a}=E("settings"),{info:o,entry:h,idx:m,count:x,keyValue:i,keyTouched:y,onMove:d,onToggle:l,onSetKey:u,onClearKey:b,onTest:C,testing:k}=r,N=!!o?.envKey,S=!!o?.available||y&&!!i;return e.jsxs("div",{className:"rounded-md border border-border bg-surface p-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("div",{className:"flex flex-col",children:[e.jsx("button",{type:"button",className:"h-5 w-5 rounded text-text-dim hover:bg-surface-hover disabled:opacity-30",onClick:()=>d(-1),disabled:m===0,"aria-label":a("search.moveUp"),children:e.jsx(q,{className:"h-4 w-4"})}),e.jsx("button",{type:"button",className:"h-5 w-5 rounded text-text-dim hover:bg-surface-hover disabled:opacity-30",onClick:()=>d(1),disabled:m===x-1,"aria-label":a("search.moveDown"),children:e.jsx(J,{className:"h-4 w-4"})})]}),e.jsxs("label",{className:"flex items-center gap-2 text-sm",children:[e.jsx("input",{type:"checkbox",checked:h.enabled,onChange:p=>l(p.target.checked),className:"h-4 w-4 accent-accent cursor-pointer"}),e.jsx("span",{className:"font-mono text-sm",children:h.name})]}),N?S?e.jsxs(j,{variant:"success",children:[e.jsx(W,{className:"mr-1 h-3 w-3"})," ",a("search.configured")]}):e.jsxs(j,{variant:"warning",children:[e.jsx(G,{className:"mr-1 h-3 w-3"})," ",a("search.missingKey")]}):e.jsx(j,{variant:"outline",children:a("search.noKey")}),e.jsx("div",{className:"ml-auto flex items-center gap-1",children:e.jsxs(g,{size:"sm",variant:"ghost",onClick:C,disabled:k||!h.enabled,"aria-label":a("search.testBtn"),children:[k?e.jsx(K,{className:"h-4 w-4 animate-spin"}):e.jsx(Q,{className:"h-4 w-4"}),e.jsx("span",{className:"hidden sm:inline",children:a("search.testBtn")})]})})]}),N&&e.jsxs("div",{className:"mt-3 flex flex-col gap-1",children:[e.jsxs(D,{className:"text-xs font-medium",children:[e.jsx(Y,{className:"mr-1 inline h-3 w-3"}),o?.envKey]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(I,{type:"password",value:i??"",onChange:p=>u(p.target.value),placeholder:o?.available&&!y?a("search.placeholderConfigured"):a("search.placeholderEnter"),className:"font-mono text-xs"}),o?.available&&!y&&e.jsx(g,{size:"sm",variant:"ghost",onClick:b,"aria-label":a("search.clearKey"),children:e.jsx(M,{className:"h-4 w-4"})})]}),e.jsx("p",{className:"text-[11px] text-text-dim",children:a("search.keyHint",{envKey:o?.envKey})})]})]})}export{ue as default};
//# sourceMappingURL=search-Con69NhG.js.map
