import{r as c,j as t}from"./main.js";import{I}from"./input-CbL6Bfs6.js";import{d as K,B as P}from"./button-D4VgS8ri.js";import{B as V}from"./badge-C9rqaanl.js";import{C as q}from"./checkbox-CuMDeUdJ.js";import{D as F,a as H,b as R,c as z,e as G,d as J}from"./dialog-B3hEL_A6.js";import{i as L}from"./label-utils-B422rjYv.js";import{T as Q}from"./tag-5uYH3Yvy.js";import{S as U}from"./search-DZkcyG-F.js";import{P as W}from"./plus-nLo-Myt8.js";function ae({open:C,onOpenChange:g,version:a,allLabels:d,onSave:S}){const{t:n}=K(),[j,y]=c.useState(!1),[r,f]=c.useState(""),[v,x]=c.useState(""),[l,h]=c.useState(new Set),[u,w]=c.useState([]),k=e=>{if(e){const s=new Set;for(const[o,b]of Object.entries(d))b===a&&s.add(o);h(s),w([]),f(""),x("")}g(e)},i=c.useMemo(()=>{const e=new Set(Object.keys(d));for(const s of u)e.add(s);return Array.from(e).sort()},[d,u]),m=c.useMemo(()=>{const e=r.trim().toLowerCase();return e?i.filter(s=>s.toLowerCase().includes(e)):i},[i,r]),D=i.some(e=>e.toLowerCase()===r.trim().toLowerCase()),p=r.trim()&&!D&&L(r.trim(),[]),T=(e,s)=>{const o=new Set(l);s?o.add(e):o.delete(e),h(o)},N=()=>{const e=r.trim();if(e){if(!L(e,i)){i.includes(e)?x(n("common.versionLabels.keyDuplicate")):x(n("common.versionLabels.keyInvalid"));return}w(s=>[...s,e]),h(s=>new Set([...s,e])),f(""),x("")}},B=e=>{e.key==="Enter"&&p&&(e.preventDefault(),N())},A=()=>{const e=new Set(l);for(const s of m)e.add(s);h(e)},O=()=>{const e=new Set(l);for(const s of m)e.delete(s);h(e)},E=async()=>{y(!0);try{const e={};for(const[s,o]of Object.entries(d))o!==a?l.has(s)?e[s]=a:e[s]=o:l.has(s)&&(e[s]=a);for(const s of u)l.has(s)&&!(s in e)&&(e[s]=a);await S(e),g(!1)}finally{y(!1)}};return t.jsx(F,{open:C,onOpenChange:k,children:t.jsxs(H,{className:"sm:max-w-md",children:[t.jsxs(R,{children:[t.jsxs(z,{className:"flex items-center gap-2",children:[t.jsx(Q,{className:"h-4 w-4"}),n("common.versionLabels.labelManagement")]}),t.jsx(G,{children:n("common.versionLabels.labelBindDesc",{version:a})})]}),t.jsxs("div",{className:"space-y-4",children:[t.jsxs("div",{className:"relative",children:[t.jsx(U,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-muted-foreground"}),t.jsx(I,{value:r,onChange:e=>{f(e.target.value),x("")},onKeyDown:B,placeholder:n("common.versionLabels.searchOrCreateLabel"),className:"pl-8 text-sm"})]}),v&&t.jsx("p",{className:"text-xs text-destructive",children:v}),p&&t.jsxs("button",{type:"button",className:"flex items-center gap-2 w-full px-3 py-2 text-sm rounded-md hover:bg-muted/60 transition-colors text-primary",onClick:N,children:[t.jsx(W,{className:"h-3.5 w-3.5"}),n("common.versionLabels.createLabel",{name:r.trim()})]}),m.length>0&&t.jsxs("div",{className:"space-y-1",children:[t.jsxs("div",{className:"flex items-center gap-2 px-1 pb-1 text-xs text-muted-foreground",children:[t.jsx("button",{type:"button",className:"hover:text-foreground transition-colors",onClick:A,children:n("common.versionLabels.selectAll",{count:m.length})}),t.jsx("span",{children:"·"}),t.jsx("button",{type:"button",className:"hover:text-foreground transition-colors",onClick:O,children:n("common.versionLabels.clearAll")})]}),t.jsx("div",{className:"max-h-[240px] overflow-y-auto space-y-0.5",children:m.map(e=>{const s=l.has(e),o=d[e],b=o&&o!==a;return t.jsxs("label",{className:"flex items-center gap-3 px-3 py-2 rounded-md hover:bg-muted/60 transition-colors cursor-pointer",children:[t.jsx(q,{checked:s,onCheckedChange:M=>T(e,M===!0)}),t.jsx("span",{className:"flex-1 text-sm font-mono",children:e}),s?t.jsxs(V,{variant:"secondary",className:"text-[10px] px-1.5 py-0 h-4 font-mono",children:["→ ",a]}):b?t.jsxs("span",{className:"text-[10px] text-muted-foreground font-mono",children:["→ ",o]}):null]},e)})})]}),m.length===0&&!p&&t.jsx("p",{className:"text-sm text-muted-foreground text-center py-4",children:n("common.versionLabels.noLabels")})]}),t.jsx(J,{children:t.jsx(P,{onClick:E,disabled:j,className:"w-full",children:n(j?"common.loading":"common.versionLabels.save")})})]})})}export{ae as L};
