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