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 u from"../../hooks/useMergeRef.js";import s from"./EditableTypography.module.scss.js";import{keyCodes as c}from"../../constants/keyCodes.js";import"../../constants/sizes.js";import{useKeyboardButtonPressedFunc as p}from"@vibe/shared";import d from"../../hooks/usePrevious/index.js";import f from"../../hooks/ssr/useIsomorphicLayoutEffect.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,C=n.onKeyDown,P=n.readOnly,x=void 0!==P&&P,w=n.ariaLabel,k=void 0===w?"":w,j=n.placeholder,D=n.clearable,N=n.typographyClassName,K=n.component,T=n.isEditMode,B=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=u(m,L),_=r(T||!1),z=e(_,2),q=z[0],F=z[1],G=r(g),J=e(G,2),Q=J[0],U=J[1],V=d(g),W=l(null),X=l(null);function Y(e){x||q||(e.preventDefault(),Z(!0))}function Z(e){null==M||M(e),F(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==C||C(e)}}function oe(e){U(e.target.value)}a((function(){q||g===V||g===Q||U(g)}),[V,q,g,Q]),a((function(){F(T)}),[T]);var ne=p(Y);return a((function(){var e,t;q&&(!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)}}(),B&&(null===(t=null===(e=W.current)||void 0===e?void 0:e.select)||void 0===t||t.call(e)))}),[B,q]),f((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,q]),o.createElement("div",{ref:O,id:v,"aria-label":k,"data-testid":y,className:i(s.editableTypography,h),role:q?null:"button",onClick:function(e){null==b||b(e),Y(e)},onKeyDown:ne},q&&(I?o.createElement("textarea",{ref:W,className:i(s.textarea,N),value:Q,onChange:oe,onKeyDown:te,onBlur:ee,"aria-label":k,placeholder:j,role:"textbox",rows:1}):o.createElement("input",{ref:W,className:i(s.input,N),value:Q,onChange:oe,onKeyDown:te,onBlur:ee,"aria-label":k,placeholder:j,role:"input"})),o.createElement(K,{ref:X,"aria-hidden":q,className:i(s.typography,N,t(t(t(t({},s.hidden,q),s.disabled,x),s.placeholder,!Q&&j),s.multiline,!q&&I)),tabIndex:0,tooltipProps:R,weight:A,type:S,ellipsis:!I},Q||j))}));export{m as default};
//# sourceMappingURL=EditableTypography.js.map
