import e from"@babel/runtime/helpers/defineProperty";import r from"@babel/runtime/helpers/slicedToArray";import{createElement as t,useRef as n,useState as i,useLayoutEffect as o}from"react";import{jsx as c,jsxs as l,Fragment as a}from"react/jsx-runtime";import s from"classnames";import{debounce as u}from"lodash";import{Tooltip as d}from"../Tooltip/Tooltip.js";import{TextMiddleTruncatedTestIds as f}from"./TextMiddleTruncated.constants.js";import m from"./TextMiddleTruncated.scss.js";import{truncateInTheMiddle as p}from"../../utils/formatting.js";function v(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function h(r){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?v(Object(n),!0).forEach((function(t){e(r,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(n)):v(Object(n)).forEach((function(e){Object.defineProperty(r,e,Object.getOwnPropertyDescriptor(n,e))}))}return r}var b=function(t){var v=t.children,h=void 0===v?"":v,b=t.testId,O=t.className,j=t.disableTooltip,g=t.maxLines,y=void 0===g?1:g,T=String(h),x=n(null),P=n(null),w=n(null),D=i(T),M=r(D,2),N=M[0],E=M[1],I=i(),L=r(I,2),S=L[0],W=L[1],k=y>1;return o((function(){var e=function(){if(x.current&&P.current){var e=x.current.clientWidth,r=P.current,t=r.scrollWidth,n=r.textContent,i=null!=n?n:"";if(e&&t&&i.length)if(k){var o=t/i.length,c=Math.max(1,Math.floor(e/o));if(i.length<=c*y)return W(void 0),void E(i);for(var l=y-1,a=l-1,s=c*(l-1)+(c-2),u=c,d=i.slice(0,s),f=i.slice(i.length-u),m=[],v=0;v<l;v+=1){var h=v*c,b=v===a,O=b?c-3:c,j=d.slice(h,h+O);j&&m.push(b?"".concat(j," ..."):j)}m.push(f),W(m),E(m.join("\n"))}else{var g;if(e===(null===(g=w.current)||void 0===g?void 0:g.clientWidth))return;W(void 0),E(p(i,e,t))}}};e();var r=u((function(){return requestAnimationFrame(e)}),200,{leading:!0}),t=new ResizeObserver(r),n=new MutationObserver(r);return x.current&&P.current&&(t.observe(x.current),t.observe(P.current),n.observe(P.current,{subtree:!0,characterData:!0})),function(){t.disconnect(),n.disconnect(),r.cancel()}}),[k,y,T]),c(d,{title:T,disabled:j||!(!!S||N!==T),children:l("div",{className:s(m.root,O,e({},m.multiLine,k)),ref:x,"data-testid":b,children:[c("div",{className:m.inner,ref:w,"data-testid":f.TruncatedTextTestId,children:S?c(a,{children:S.map((function(e,r){return c("div",{className:m.line,children:e},r)}))}):N}),c("div",{className:m.hiddenElement,ref:P,"data-testid":f.FullTextTestId,children:T})]})})},O=function(e){return t(b,h(h({},e),{},{key:"".concat(e.children,"-").concat(e.maxLines)}))};export{O as TextMiddleTruncated};
//# sourceMappingURL=TextMiddleTruncated.js.map
