import{slicedToArray as e,defineProperty as t}from"../../../_virtual/_rollupPluginBabelHelpers.js";import o,{forwardRef as n,useRef as l,useState as r,useEffect as a}from"react";import i from"classnames";import s from"../../hooks/useMergeRef.js";import u from"./EditableTypography.module.scss.js";import{keyCodes as c}from"../../constants/keyCodes.js";import"../../constants/sizes.js";import p from"../../hooks/usePrevious/index.js";import d from"../../hooks/ssr/useIsomorphicLayoutEffect.js";import{useKeyboardButtonPressedFunc as f}from"../../../shared/dist/hooks/useKeyboardButtonPressedFunc.js";var m=n((function(n,m){var v=n.id,h=n.className,y=n["data-testid"],g=n.value,E=n.onChange,b=n.onClick,P=n.onKeyDown,C=n.readOnly,x=void 0!==C&&C,k=n.ariaLabel,w=void 0===k?"":k,j=n.placeholder,D=n.clearable,N=n.typographyClassName,K=n.component,B=n.isEditMode,T=n.autoSelectTextOnEditMode,M=n.onEditModeChange,R=n.tooltipProps,S=n.type,A=n.weight,H=n.multiline,I=void 0!==H&&H,L=l(null),O=s(m,L),_=r(B||!1),z=e(_,2),F=z[0],q=z[1],G=r(g),J=e(G,2),Q=J[0],U=J[1],V=p(g),W=l(null),X=l(null);function Y(e){x||F||(e.preventDefault(),Z(!0))}function Z(e){null==M||M(e),q(e)}function $(){(Z(!1),g!==Q)&&(Q||D&&j?(U(Q),null==E||E(Q)):U(g))}function ee(){$()}function te(e){if(!e.nativeEvent.isComposing){if(e.key===c.ENTER){if(I&&e.shiftKey)return;e.preventDefault(),e.stopPropagation(),$()}e.key===c.ESCAPE&&(e.preventDefault(),e.stopPropagation(),Z(!1),U(g)),null==P||P(e)}}function oe(e){U(e.target.value)}a((function(){F||g===V||g===Q||U(g)}),[V,F,g,Q]),a((function(){q(B)}),[B]);var ne=f(Y);return a((function(){var e,t;F&&(!function(){var e,t;if(null===(t=null===(e=W.current)||void 0===e?void 0:e.focus)||void 0===t||t.call(e),W.current){var o=W.current,n=o.value.length;o.setSelectionRange(n,n)}}(),T&&(null===(t=null===(e=W.current)||void 0===e?void 0:e.select)||void 0===t||t.call(e)))}),[T,F]),d((function(){var e;if(X.current){var t=X.current.getBoundingClientRect();if(null===(e=null==W?void 0:W.current)||void 0===e||e.style.setProperty("--input-width","".concat(t.width,"px")),I){var o=null==W?void 0:W.current;null==o||o.style.setProperty("--input-height","auto"),null==o||o.style.setProperty("--input-height","".concat(o.scrollHeight+2,"px"))}}}),[Q,F]),o.createElement("div",{ref:O,id:v,"aria-label":w,"data-testid":y,className:i(u.editableTypography,h),role:F?null:"button",onClick:function(e){null==b||b(e),Y(e)},onKeyDown:ne},F&&(I?o.createElement("textarea",{ref:W,className:i(u.textarea,N),value:Q,onChange:oe,onKeyDown:te,onBlur:ee,"aria-label":w,placeholder:j,role:"textbox",rows:1}):o.createElement("input",{ref:W,className:i(u.input,N),value:Q,onChange:oe,onKeyDown:te,onBlur:ee,"aria-label":w,placeholder:j,role:"input"})),o.createElement(K,{ref:X,"aria-hidden":F,className:i(u.typography,N,t(t(t(t({},u.hidden,F),u.disabled,x),u.placeholder,!Q&&j),u.multiline,!F&&I)),tabIndex:0,tooltipProps:R,weight:A,type:S,ellipsis:!I},Q||j))}));export{m as default};
//# sourceMappingURL=EditableTypography.js.map
