(function(e,t){"function"==typeof define&&define.amd?define([],t):"object"==typeof module&&module.exports?module.exports=t():e.balanceText=t()})(this,()=>{function e(){}function t(e,t){Array.prototype.forEach.call(e,t)}function n(e){"loading"!==document.readyState?e():document.addEventListener?document.addEventListener("DOMContentLoaded",e):document.attachEvent("onreadystatechange",()=>{"loading"!==document.readyState&&e()})}function o(e,t,n,...o){let i;return function(){function l(){n||e.apply(a,o),i=null}const a=this;i?clearTimeout(i):n&&e.apply(a,o),i=setTimeout(l,t||100)}}function i(){if("undefined"==typeof window)return!1;const{style:e}=document.documentElement;return e.textWrap||e.WebkitTextWrap||e.MozTextWrap||e.MsTextWrap}function l(){this.reset()}function a(e){return E.some(t=>t.start<e&&e<t.end)}function r(e,n){if(e.nodeType===e.ELEMENT_NODE){const o=window.getComputedStyle(e);if("nowrap"===o.whiteSpace){const t=e.outerHTML.length;E.push({start:L,end:L+t}),L+=t}else t(e.childNodes,e=>{r(e,!0)}),n&&(L+=e.outerHTML.length-e.innerHTML.length)}else e.nodeType===e.COMMENT_NODE?L+=e.length+7:e.nodeType===e.PROCESSING_INSTRUCTION_NODE?L+=e.length+2:L+=e.length}function s(e,t,n){if(0===n)e.style.whiteSpace=t,L=0,E=[],r(e,!1),e.style.whiteSpace="nowrap";else{const e=[];E.forEach(t=>{t.start>n&&e.push({start:t.start-n,end:t.end-n})}),E=e}}function d(e){let n=e.querySelectorAll('br[data-owner="balance-text-hyphen"]');t(n,e=>{e.outerHTML=""}),n=e.querySelectorAll('br[data-owner="balance-text"]'),t(n,e=>{e.outerHTML=" "});let o=e.querySelectorAll('span[data-owner="balance-text-softhyphen"]');if(o.length>0&&t(o,e=>{const t=document.createTextNode("­");e.parentNode.insertBefore(t,e),e.parentNode.removeChild(e)}),o=e.querySelectorAll('span[data-owner="balance-text-justify"]'),o.length>0){let n="";t(o,e=>{n+=e.textContent,e.parentNode.removeChild(e)}),e.innerHTML=n}}function c(e,t,n){t=t.trim();const o=t.split(" ").length;if(t=`${t} `,o<2)return t;const i=document.createElement("span");i.innerHTML=t,e.appendChild(i);const l=i.offsetWidth;i.parentNode.removeChild(i);const a=Math.floor((n-l)/(o-1));i.style.wordSpacing=`${a}px`,i.setAttribute("data-owner","balance-text-justify");const r=document.createElement("div");return r.appendChild(i),r.innerHTML}function u(e,t){const n=/([^\S\u00a0]|-|\u2014|\u2013|\u00ad)(?![^<]*>)/g;let o;if(!S)for(S=[],o=n.exec(e);null!==o;)a(o.index)||S.push(o.index),o=n.exec(e);return-1!==S.indexOf(t)}function f(e,t){return 0===t||t===e.length||u(e,t-1)&&!u(e,t)}function p(e,t,n,o,i,l,a){let r;if(t&&"string"==typeof t)for(;;){for(;!f(t,l);)l+=i;if(e.innerHTML=t.substr(0,l),r=e.offsetWidth,i<0){if(r<=o||r<=0||0===l)break}else if(o<=r||n<=r||l===t.length)break;l+=i}a.index=l,a.width=r}function h(e,t){const n=document.createElement("div");n.style.display="block",n.style.position="absolute",n.style.bottom=0,n.style.right=0,n.style.width=0,n.style.height=0,n.style.margin=0,n.style.padding=0,n.style.visibility="hidden",n.style.overflow="hidden";const o=document.createElement("span");o.style.fontSize="2000px",o.innerHTML="&nbsp;",n.appendChild(o),e.appendChild(n);const i=o.getBoundingClientRect();n.parentNode.removeChild(n);const l=i.height/i.width;return t/l}function y(e){return e?"string"==typeof e?document.querySelectorAll(e):e.tagName&&e.querySelectorAll?[e]:e:[]}function w(e){t(y(e),e=>{const t=5e3;d(e);const n=e.style.whiteSpace,o=e.style.float,i=e.style.display,a=e.style.position,r=e.style.lineHeight;e.style.lineHeight="normal";const u=e.offsetWidth,f=e.offsetHeight;e.style.whiteSpace="nowrap",e.style.float="none",e.style.display="inline",e.style.position="static";let y=e.offsetWidth;const w=e.offsetHeight,m="pre-wrap"===n?0:h(e,w);if(u>0&&y>u&&y<t){let t=e.innerHTML,o="",i="";const a=v(e),r=Math.round(f/w);let d,h,x,g,b,T,M,E=r,L=0;for(;E>1;)S=null,s(e,n,L),d=Math.round((y+m)/E-m),h=Math.round((t.length+1)/E)-1,x=new l,p(e,t,u,d,-1,h,x),g=new l,h=x.index,p(e,t,u,d,1,h,g),x.reset(),h=g.index,p(e,t,u,d,-1,h,x),b=0===x.index?g.index:u<g.width||x.index===g.index?x.index:Math.abs(d-x.width)<Math.abs(g.width-d)?x.index:g.index,i=t.substr(0,b).replace(/\s$/,""),M=Boolean(i.match(/\u00ad$/)),M&&(i=i.replace(/\u00ad$/,'<span data-owner="balance-text-softhyphen">-</span>')),a?o+=c(e,i,u):(o+=i,T=M||Boolean(i.match(/(-|\u2014|\u2013)$/)),o+=T?'<br data-owner="balance-text-hyphen" />':'<br data-owner="balance-text" />'),t=t.substr(b),L=b,E--,e.innerHTML=t,y=e.offsetWidth;e.innerHTML=a?o+c(e,t,u):o+t}e.style.whiteSpace=n,e.style.float=o,e.style.display=i,e.style.position=a,e.style.lineHeight=r})}function m(){const e=H.sel.join(","),t=y(e),n=Array.prototype.concat.apply(H.el,t);w(n)}function x(){C||(n(m),window.addEventListener("load",m),window.addEventListener("resize",o(m)),C=!0)}function g(e){"string"==typeof e?H.sel.push(e):t(y(e),e=>{H.el.push(e)}),x(),m()}function b(e){"string"==typeof e?H.sel=H.sel.filter(t=>t!==e):(e=y(e),H.el=H.el.filter(t=>-1===e.indexOf(t)))}function T(){N||(H.sel.push(".balance-text"),x(),N=!0)}function M(e,t){e?t&&!0===t.watch?g(e):t&&!1===t.watch?b(e):w(e):T()}let S,E,L;const H={sel:[],el:[]};let C=!1,N=!1;l.prototype.reset=function(){this.index=0,this.width=0};const v=function(e){const t=e.currentStyle||window.getComputedStyle(e,null);return"justify"===t.textAlign};return M.updateWatched=m,i()?(e.updateWatched=e,e):M});