import{t as I,r as c,j as t}from"./vendor-react.js";import{I as K}from"./input.js";import{B as P}from"./button.js";import{B as V}from"./badge.js";import{C as Z}from"./checkbox.js";import{D as q,a as z,b as F,c as H,e as R,d as _}from"./dialog.js";import{Z as $,w as G,u as J}from"./vendor-icons.js";function C(d,h){return!d||!/^[a-zA-Z0-9._-]+$/.test(d)?!1:!h.includes(d)}function te({open:d,onOpenChange:h,version:a,allLabels:x,onSave:S}){const{t:o}=I(),[v,y]=c.useState(!1),[r,p]=c.useState(""),[w,u]=c.useState(""),[l,f]=c.useState(new Set),[b,N]=c.useState([]),k=e=>{if(e){const s=new Set;for(const[n,j]of Object.entries(x))j===a&&s.add(n);f(s),N([]),p(""),u("")}h(e)},i=c.useMemo(()=>{const e=new Set(Object.keys(x));for(const s of b)e.add(s);return Array.from(e).sort()},[x,b]),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()),g=r.trim()&&!D&&C(r.trim(),[]),B=(e,s)=>{const n=new Set(l);s?n.add(e):n.delete(e),f(n)},L=()=>{const e=r.trim();if(e){if(!C(e,i)){i.includes(e)?u(o("common.versionLabels.keyDuplicate")):u(o("common.versionLabels.keyInvalid"));return}N(s=>[...s,e]),f(s=>new Set([...s,e])),p(""),u("")}},T=e=>{e.key==="Enter"&&g&&(e.preventDefault(),L())},A=()=>{const e=new Set(l);for(const s of m)e.add(s);f(e)},O=()=>{const e=new Set(l);for(const s of m)e.delete(s);f(e)},E=async()=>{y(!0);try{const e={};for(const[s,n]of Object.entries(x))n!==a?l.has(s)?e[s]=a:e[s]=n:l.has(s)&&(e[s]=a);for(const s of b)l.has(s)&&!(s in e)&&(e[s]=a);await S(e),h(!1)}finally{y(!1)}};return t.jsx(q,{open:d,onOpenChange:k,children:t.jsxs(z,{className:"sm:max-w-md",children:[t.jsxs(F,{children:[t.jsxs(H,{className:"flex items-center gap-2",children:[t.jsx($,{className:"h-4 w-4"}),o("common.versionLabels.labelManagement")]}),t.jsx(R,{children:o("common.versionLabels.labelBindDesc",{version:a})})]}),t.jsxs("div",{className:"space-y-4",children:[t.jsxs("div",{className:"relative",children:[t.jsx(G,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-muted-foreground"}),t.jsx(K,{value:r,onChange:e=>{p(e.target.value),u("")},onKeyDown:T,placeholder:o("common.versionLabels.searchOrCreateLabel"),className:"pl-8 text-sm"})]}),w&&t.jsx("p",{className:"text-xs text-destructive",children:w}),g&&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:L,children:[t.jsx(J,{className:"h-3.5 w-3.5"}),o("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:o("common.versionLabels.selectAll",{count:m.length})}),t.jsx("span",{children:"·"}),t.jsx("button",{type:"button",className:"hover:text-foreground transition-colors",onClick:O,children:o("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),n=x[e],j=n&&n!==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(Z,{checked:s,onCheckedChange:M=>B(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]}):j?t.jsxs("span",{className:"text-[10px] text-muted-foreground font-mono",children:["→ ",n]}):null]},e)})})]}),m.length===0&&!g&&t.jsx("p",{className:"text-sm text-muted-foreground text-center py-4",children:o("common.versionLabels.noLabels")})]}),t.jsx(_,{children:t.jsx(P,{onClick:E,disabled:v,className:"w-full",children:o(v?"common.loading":"common.versionLabels.save")})})]})})}export{te as L};
