"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react/jsx-runtime"),a=require("react"),g=require("react-i18next"),T=require("@lexical/react/LexicalComposerContext"),P=require("lexical"),d=require("./layers.BgetbX1j.js"),u=require("@mantine/core");function w({onAdd:e,label:n,disabled:c,tooltip:r}){const o=s.jsx(u.ActionIcon,{variant:"subtle",color:"gray",size:"sm",radius:"sm",className:"tabs-block__add","data-testid":"tabs-add","aria-label":n,"aria-disabled":c?"true":void 0,"data-disabled":c||void 0,onMouseDown:t=>t.preventDefault(),onClick:t=>{if(c){t.preventDefault();return}e()},children:s.jsx(d.HugeiconsIcon,{icon:d.T6,size:14})});return r?s.jsx(u.Tooltip,{label:r,position:"top",withArrow:!0,openDelay:300,children:o}):o}function E({tab:e,isActive:n,containerKey:c,onActivate:r,readOnly:o}){const[t]=T.useLexicalComposerContext(),{t:m}=g.useTranslation(),[f,p]=a.useState(!1),[b,l]=a.useState(e.title),[_,h]=a.useState(!1);a.useEffect(()=>{l(e.title)},[e.title]);const x=a.useCallback(()=>{const i=b.trim();i&&i!==e.title?d.dispatchRenameTab(t,c,e.id,i):l(e.title),p(!1)},[t,c,e.id,e.title,b]),C=a.useCallback(()=>{l(e.title),p(!1)},[e.title]),j=a.useCallback(i=>{i.key==="Enter"?(i.preventDefault(),x()):i.key==="Escape"&&(i.preventDefault(),C())},[x,C]),y=a.useCallback(()=>{f||r()},[f,r]),A=a.useCallback(i=>{o||(i.stopPropagation(),p(!0))},[o]),S=a.useCallback(i=>{o||(i.preventDefault(),i.stopPropagation(),h(!0))},[o]),N=a.useCallback(()=>{h(!1),p(!0)},[]),D=a.useCallback(()=>{h(!1),d.dispatchRemoveTab(t,c,e.id)},[t,c,e.id]);return s.jsxs(u.Popover,{opened:_,onChange:h,position:"bottom-start",shadow:"md",withinPortal:!0,trapFocus:!1,closeOnClickOutside:!0,closeOnEscape:!0,children:[s.jsx(u.Popover.Target,{children:s.jsx("span",{className:"tabs-block__tab","data-active":n,"data-testid":"tabs-tab","data-tab-id":e.id,onClick:y,onDoubleClick:A,onContextMenu:S,children:f?s.jsx(u.TextInput,{size:"xs",variant:"unstyled",value:b,onChange:i=>l(i.currentTarget.value),onBlur:x,onKeyDown:j,autoFocus:!0,classNames:{input:"tabs-block__rename-input"},styles:{input:{width:`${Math.max(b.length,3)}ch`}},"data-testid":"tabs-rename-input"}):s.jsx("span",{className:"tabs-block__tab-label",children:e.title})})}),s.jsx(u.Popover.Dropdown,{p:4,children:s.jsxs("div",{className:"tabs-block__menu",children:[s.jsx(u.UnstyledButton,{className:"tabs-block__menu-item",onClick:N,"data-testid":"tabs-menu-rename",children:m("editor.menu.other.tabs.actions.rename")}),s.jsx(u.UnstyledButton,{className:"tabs-block__menu-item tabs-block__menu-item--danger",onClick:D,"data-testid":"tabs-menu-delete",children:m("editor.menu.other.tabs.actions.delete")})]})})]})}function v(e,n){let c={tabs:[],activeTabId:"",containerKey:""};return e.getEditorState().read(()=>{const r=P.$getNodeByKey(n);if(!r||!d.$isTabsHeaderNode(r))return;const o=r.getParent();d.$isTabsContainerNode(o)&&(c={tabs:o.getTabs(),activeTabId:o.getActiveTabId(),containerKey:o.getKey()})}),c}function k(e){e.stopPropagation()}function I({nodeKey:e}){const[n]=T.useLexicalComposerContext(),{t:c}=g.useTranslation(),{disable:r}=a.useContext(d.Context),o=!!r,[t,m]=a.useState(()=>v(n,e));a.useEffect(()=>(m(v(n,e)),n.registerUpdateListener(()=>{m(v(n,e))})),[n,e]);const f=a.useCallback(()=>{t.containerKey&&d.dispatchAddTab(n,t.containerKey)},[n,t.containerKey]),p=a.useCallback(l=>{t.containerKey&&d.dispatchSetActiveTab(n,t.containerKey,l)},[n,t.containerKey]);if(t.tabs.length===0||!t.containerKey)return null;const b=t.tabs.length<d.MAX_TABS;return s.jsxs("div",{className:"tabs-block__header",contentEditable:!1,onMouseDown:k,onClick:k,children:[s.jsx("div",{className:"tabs-block__tabs","data-testid":"tabs-header",children:t.tabs.map(l=>s.jsx(E,{tab:l,isActive:l.id===t.activeTabId,containerKey:t.containerKey,onActivate:()=>p(l.id),readOnly:o},l.id))}),!o&&s.jsx(w,{onAdd:f,label:c("editor.menu.other.tabs.actions.add"),disabled:!b,tooltip:b?void 0:c("editor.menu.other.tabs.actions.maxReached",{max:d.MAX_TABS})})]})}exports.default=I;
