import{j as e,u as d,l as T}from"./fields-wnm5Jtc6.js";import{a as b,R as y}from"./vendor-D21mNUGX.js";import{P as i,cG as R}from"./index-pmrLY__j.js";import{g as L}from"./index.esm-DCQn2FO4.js";const $=({placeholder:p,id:n,disabled:f=!1,defaultValue:g,description:C,values:N,validate:V,register:w,errors:A,setValue:m,clearErrors:h,maxItems:k})=>{const o=L(A,n),[t,x]=b.useState(g&&R(g)?g:[]),[v,u]=b.useState(!1),[j,D]=b.useState(""),O=()=>{try{return t&&R(t)&&t.length>0?e.jsx(y.Fragment,{children:t&&R(t)&&t.map(s=>{const r=()=>{const l=N.filter(a=>a.options?a.options.filter(S=>S.value.toString()===s.toString()).length===1:a.value===s);if((l==null?void 0:l.length)===1){if(l[0].options){const a=l[0].options.filter(c=>c.value.toString()===s.toString());if((a==null?void 0:a.length)===1)return a[0].label}return l[0].label}return s};return e.jsxs("span",{className:"item",children:[e.jsx("span",{className:"label",children:r()}),e.jsx("a",{href:"#",className:"close-item",onClick:l=>{l.preventDefault(),l.stopPropagation(),f||x(a=>{const c=a.filter(S=>S!==s);return m(n,c),u(!1),o&&h(n),c})},children:"×"})]})})}):e.jsx("span",{children:p||d("Select items")})}catch{return e.jsx("span",{children:p||d("Select items")})}},F=()=>e.jsx("a",{href:"#",className:"close",onClick:s=>{s.preventDefault(),s.stopPropagation(),f||x(r=>(m(n,[]),h(n),u(!1),[]))},children:"×"}),I=()=>{x(()=>{let s=[];return N.map(r=>{s.push(r.value)}),m(n,s),h(n),u(!1),s})},P=()=>{if(k&&(t==null?void 0:t.length)>=k)return null;const s=q(N);return(s==null?void 0:s.length)===0?e.jsx("div",{className:"select-items",children:e.jsx("div",{className:"p-24",children:d("No items")})}):e.jsxs("div",{className:"select-items",children:[e.jsx("div",{className:"search-box",children:e.jsx("input",{type:"text",className:"form-control",placeholder:d("Filter items"),defaultValue:j,onChange:r=>{D(r.target.value)}})}),e.jsx("ul",{tabIndex:0,className:"select-items-list",children:s}),e.jsx("div",{className:"select-all",children:e.jsx("a",{href:"#",onClick:r=>{r.preventDefault(),I()},children:d("Select all")})})]})},q=s=>s.filter(r=>j!==""&&r.value&&r.label?T(r.value,j)||T(r.label,j):!0).filter(r=>r.value?t.findIndex(l=>l===r.value)===-1:!0).map((r,l)=>r.options?e.jsxs(y.Fragment,{children:[e.jsx("li",{className:"optgroup",children:r.label}),q(r.options)]}):e.jsx("li",{tabIndex:l+1,className:"select-item",onClick:()=>{x(a=>{const c=[...a,r.value];return m(n,c),u(!v),o&&h(n),c})},children:r.label},l));return e.jsxs(y.Fragment,{children:[e.jsxs("div",{className:"acpt-select-multi",children:[e.jsx("input",{id:n,name:n,type:"hidden",defaultValue:t,...w(n,V)}),e.jsxs("div",{className:`placeholder ${o?"has-errors":""} ${f?"disabled":""}`,onClick:()=>{f||u(!v)},children:[e.jsx("div",{className:"items",children:O()}),!v&&(t==null?void 0:t.length)>0&&F(),e.jsx("div",{className:"divider"}),e.jsx("div",{className:"caret"})]}),v&&P()]}),o&&e.jsx("div",{className:"invalid-feedback",children:o.message}),C&&e.jsx("div",{className:"form-description",children:C})]})};$.propTypes={id:i.string.isRequired,placeholder:i.string,defaultValue:i.array,description:i.string,disabled:i.bool,values:i.array.isRequired,validate:i.func,register:i.func.isRequired,setValue:i.func.isRequired,clearErrors:i.func.isRequired,errors:i.array.isRequired,maxItems:i.number};export{$ as S};
