import{defineComponent as $,computed as d,ref as s,watch as k,provide as j,createVNode as y}from"vue";import F from"../config-provider/hooks/useConfigInject.mjs";import{useInjectFormItemContext as P}from"../form/FormItemContext.mjs";import B from"./Checkbox.mjs";import{CheckboxGroupContextKey as D,checkboxGroupProps as E}from"./interface.mjs";import K from"./style/index.mjs";const L=$({compatConfig:{MODE:3},name:"ACheckboxGroup",inheritAttrs:!1,props:E(),setup(l,{slots:c,attrs:m,emit:g,expose:I}){const b=P(),{prefixCls:h,direction:p}=F("checkbox",l),r=d(()=>`${h.value}-group`),[S,V]=K(r),t=s((l.value===void 0?l.defaultValue:l.value)||[]);k(()=>l.value,()=>{t.value=l.value||[]});const n=d(()=>l.options.map(e=>typeof e=="string"||typeof e=="number"?{label:e,value:e}:e)),i=s(Symbol()),v=s(new Map),M=e=>{v.value.delete(e),i.value=Symbol()},w=(e,u)=>{v.value.set(e,u),i.value=Symbol()},x=s(new Map);return k(i,()=>{const e=new Map;for(const u of v.value.values())e.set(u,!0);x.value=e}),j(D,{cancelValue:M,registerValue:w,toggleOption:e=>{const u=t.value.indexOf(e.value),a=[...t.value];u===-1?a.push(e.value):a.splice(u,1),l.value===void 0&&(t.value=a);const C=a.filter(o=>x.value.has(o)).sort((o,G)=>{const O=n.value.findIndex(f=>f.value===o),A=n.value.findIndex(f=>f.value===G);return O-A});g("update:value",C),g("change",C),b.onFieldChange()},mergedValue:t,name:d(()=>l.name),disabled:d(()=>l.disabled)}),I({mergedValue:t}),()=>{const{id:e=b.id.value}=l;let u=null;return n.value&&n.value.length>0&&(u=n.value.map(a=>y(B,{prefixCls:h.value,key:a.value.toString(),disabled:"disabled"in a?a.disabled:l.disabled,indeterminate:a.indeterminate,value:a.value,checked:t.value.includes(a.value),onChange:a.onChange,class:`${r.value}-item`},{default:()=>[c.label!==void 0?c.label?.(a):a.label]}))),S(y("div",{...m,class:[r.value,{[`${r.value}-rtl`]:p.value==="rtl"},m.class,V.value],id:e},[u||c.default?.()]))}}});export{L as default};
