import{jsx as _jsx,jsxs as _jsxs}from"react/jsx-runtime";import{useLayoutEffect,useRef,useState,useCallback,memo}from"react";import{useAtomValue,useSetAtom}from"jotai";import{Button,OverflowMenuVerticalIcon}from"@redocly/theme";import{LanguageDropdown}from"./LanguageDropdown";import{languageAtom,layoutAtom}from"../../jotai/app";import{LanguageListContainer,LanguageListItem,Container}from"./styled";import{LanguageItem}from"./LanguageItem";import{ITEM_WIDTH}from"./constants";import{LanguageListSkeleton}from"../common/Skeleton/LanguageListSkeleton";function LanguageListComponent({languages:e,activeLanguage:t}){const[n,o]=useState([]),[a,s]=useState([]),i=useRef(null),r=useAtomValue(layoutAtom),u=useSetAtom(languageAtom),m=useCallback((()=>{var n;const a=null===(n=null==i?void 0:i.current)||void 0===n?void 0:n.offsetWidth;if(!a)return;const{newVisibleItems:r,newHiddenItems:u}=calculateVisibleAndHiddenItems(a,[...e]),m=u.findIndex((e=>e.key===t));-1!==m&&swapItems(r,u,m),o(r),s(u)}),[t,e]);return useLayoutEffect((()=>(m(),window.addEventListener("resize",m),()=>{window.removeEventListener("resize",m)})),[r,t]),_jsxs(Container,{ref:i,children:[_jsxs(LanguageListContainer,{children:[!n.length&&_jsx(LanguageListSkeleton,{}),n.map((e=>_jsx(LanguageListItem,{onClick:()=>u(e.key),active:t===e.key,children:_jsx(LanguageItem,{item:e,withIcon:!0})},e.key)))]}),a.length?_jsx(LanguageDropdown,{activeTab:t,samples:a,onChange:e=>{const t=a.findIndex((t=>t.key===e));if(-1!==t){const i=[...a],r=[...n];swapItems(r,i,t),o(r),s(i),u(e)}},trigger:_jsx(Button,{icon:_jsx(OverflowMenuVerticalIcon,{}),variant:"text",style:{height:"100%"}}),withArrow:!1,withCheckmark:!1,width:156}):null]})}export const LanguageList=memo(LanguageListComponent);function calculateVisibleAndHiddenItems(e,t){let n=0;const o=[],a=[];return t.forEach((t=>{e&&n+ITEM_WIDTH<=e?(o.push(t),n+=ITEM_WIDTH):a.push(t)})),{newVisibleItems:o,newHiddenItems:a}}function swapItems(e,t,n){const o=e[e.length-1];o&&(e[e.length-1]=t[n],t[n]=o)}
//# sourceMappingURL=LanguageList.js.map