import{o as e}from"./rolldown-runtime.C0FnF6B9.js";import{r as t}from"./emotion-is-prop-valid.esm.CuYlOKOk.js";import{a as n,i as r}from"./emotion-styled.browser.esm.Dj5rZcu_.js";import{S as i,o as a}from"./protobuf.D2FOsaUI.js";import{ot as o}from"./utils.Bid3C9ZE.js";import{a as s}from"./utils.C3-QKZw0.js";import{t as c}from"./Tooltip.bbzhtE2V.js";import{C as l,E as u,a as d,g as f}from"./styled-components.CMq9oZJa.js";import{t as p}from"./WidgetLabel.BB5s2NOk.js";import{t as m}from"./WidgetLabelHelpIconInline.6Cy34Tnv.js";import{G as h,H as g,K as _}from"./index.ByR4Z2EF.js";import{n as v}from"./useBasicWidgetState.CmWXqZwl.js";var y=e(t(),1);function b(e){let t=e.contentIcon,n=e.content??``;return t?`${t} ${n}`.trim():n}function x(e,t){if(!(t instanceof HTMLElement))return;let n=e.getBoundingClientRect(),r=t.getBoundingClientRect(),i=getComputedStyle(e),a=Number.parseFloat(i.scrollPaddingInlineStart)||0,o=Number.parseFloat(i.scrollPaddingInlineEnd)||0,s=r.left-(n.left+a),c=r.right-(n.right-o);s>=0&&c<=0||(e.scrollLeft+=s<0?s:c)}function S(e,t){for(let n=e.length-1;n>=0;n--)if(b(e[n])===t)return n;return-1}function C(e,t){let n=[];for(let r of t){let t=S(e,r);t>=0&&n.push(t)}return n}function w(e,t){return e.getStringArrayValue(t)}function T(e){return(e.default??[]).map(t=>{let n=e.options[t];return n?b(n):``}).filter(e=>e!==``)}function E(e){return e.rawValues??[]}function D(e,t,n,r){t.setStringArrayValue(e.id,n.value,{formId:e.formId,fragmentId:r,fromUser:n.fromUser})}function O(e){let{disabled:t,element:S,fragmentId:O,widgetMgr:k,widthConfig:A}=e,{clickMode:j,options:M,style:N,label:P,labelVisibility:F,help:I,required:L}=S,R=S.queryParamKey?{paramKey:S.queryParamKey,valueType:`string_array_value`,clearable:!0,urlFormat:`repeated`}:void 0,[z,B]=v({getStateFromWidgetMgr:w,getDefaultStateFromProto:T,getCurrStateFromProto:E,updateWidgetMgrState:D,element:S,widgetMgr:k,fragmentId:O,formClearBehavior:`resetValueOnly`,queryParamBinding:R}),V=s(A),H=h(S.wrap),U=(0,y.useRef)(null),W=(0,y.useMemo)(()=>M.map(b).join(`\0`),[M]),{canScrollLeft:G,canScrollRight:K}=g({elementRef:U,enabled:!H,layoutKey:W});(0,y.useEffect)(()=>{U.current&&(L?U.current.setAttribute(`aria-required`,`true`):U.current.removeAttribute(`aria-required`))},[L]),(0,y.useEffect)(()=>{if(H||!U.current||z.length===0)return;let e=U.current,t=document.activeElement,n=t instanceof Element&&e.contains(t);if(n&&!t.hasAttribute(`data-selected`))return;let r=n?t:e.querySelector(`[data-selected]`);r&&x(e,r)},[H,z,W]),(0,y.useEffect)(()=>{if(z.length===0||C(M,z).length>0)return;let e=E(S);B({value:e.length>0?e:T(S),fromUser:!1})},[M,z,B,S]);let q=j===a.ClickMode.MULTI_SELECT?`multiple`:`single`,J=(0,y.useCallback)(e=>`${S.id}-${e}`,[S.id]),Y=(0,y.useMemo)(()=>new Set(C(M,z).map(e=>J(e))),[M,z,J]),X=(0,y.useCallback)(e=>{if(e===`all`)return;let t=`${S.id}-`,n=[...e].map(e=>b(M[Number(String(e).slice(t.length))])),r=new Set(z);n.length===z.length&&n.every(e=>r.has(e))||B({value:n,fromUser:!0})},[M,z,B,S.id]),Z=N===a.Style.PILLS,Q=(0,y.useMemo)(()=>{let e=Z?f:l,t=Z?`pills`:`segmented_control`;return M.map((n,i)=>r(e,{id:J(i),"data-variant":t,$containerWidth:V,$wrap:H,children:r(_,{icon:n.contentIcon??void 0,label:n.content??``,iconSize:`base`})},`${b(n)}-${i}`))},[M,Z,V,H,J]);return n(d,{className:`stButtonGroup`,"data-testid":`stButtonGroup`,containerWidth:V,children:[r(p,{label:P,disabled:t,labelVisibility:o(F?.value??i.LabelVisibilityOptions.COLLAPSED),children:I&&r(m,{content:I,placement:c.TOP,label:P})}),r(u,{ref:U,selectionMode:q,selectedKeys:Y,onSelectionChange:X,isDisabled:t,disallowEmptySelection:L&&q===`single`,"aria-label":S.label,$isPills:Z,$containerWidth:V,$wrap:H,"data-can-scroll-start":G?``:void 0,"data-can-scroll-end":K?``:void 0,children:Q})]})}var k=(0,y.memo)(O);export{k as default};