import*as e from"react";import{useEventListeners as n}from"./use-event-listeners.js";import{isRightClick as t,dataAttr as o}from"../../../utils/dist/esm/dom.js";import{mergeRefs as a}from"../../../react-utils/dist/esm/refs.js";function r(){return(r=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e}).apply(this,arguments)}function u(e){var n=e.target,{tagName:t,isContentEditable:o}=n;return"INPUT"!==t&&"TEXTAREA"!==t&&!0!==o}function l(l){void 0===l&&(l={});var{ref:s,isDisabled:i,isFocusable:c,clickOnEnter:p=!0,clickOnSpace:v=!0,onMouseDown:f,onMouseUp:d,onClick:b,onKeyDown:k,onKeyUp:m,tabIndex:y,onMouseOver:D,onMouseLeave:M}=l,C=function(e,n){if(null==e)return{};var t,o,a={},r=Object.keys(e);for(o=0;o<r.length;o++)t=r[o],n.indexOf(t)>=0||(a[t]=e[t]);return a}(l,["ref","isDisabled","isFocusable","clickOnEnter","clickOnSpace","onMouseDown","onMouseUp","onClick","onKeyDown","onKeyUp","tabIndex","onMouseOver","onMouseLeave"]),[O,g]=e.useState(!0),[K,U]=e.useState(!1),w=n(),T=O?y:y||0,E=i&&!c,P=e.useCallback((e=>{if(i)return e.stopPropagation(),void e.preventDefault();e.currentTarget.focus(),null==b||b(e)}),[i,b]),j=e.useCallback((e=>{K&&u(e)&&(e.preventDefault(),e.stopPropagation(),U(!1),w.remove(document,"keyup",j,!1))}),[K,w]),x=e.useCallback((e=>{if(null==k||k(e),!(i||e.defaultPrevented||e.metaKey)&&u(e.nativeEvent)&&!O){var n=p&&"Enter"===e.key;if(v&&" "===e.key&&(e.preventDefault(),U(!0)),n)e.preventDefault(),e.currentTarget.click();w.add(document,"keyup",j,!1)}}),[i,O,k,p,v,w,j]),S=e.useCallback((e=>{(null==m||m(e),i||e.defaultPrevented||e.metaKey)||u(e.nativeEvent)&&!O&&v&&" "===e.key&&(e.preventDefault(),U(!1),e.currentTarget.click())}),[v,O,i,m]),h=e.useCallback((e=>{0===e.button&&(U(!1),w.remove(document,"mouseup",h,!1))}),[w]),I=e.useCallback((e=>{if(!t(e)){if(i)return e.stopPropagation(),void e.preventDefault();O||U(!0),e.currentTarget.focus({preventScroll:!0}),w.add(document,"mouseup",h,!1),null==f||f(e)}}),[i,O,f,w,h]),L=e.useCallback((e=>{t(e)||(O||U(!1),null==d||d(e))}),[d,O]),N=e.useCallback((e=>{i?e.preventDefault():null==D||D(e)}),[i,D]),A=e.useCallback((e=>{K&&(e.preventDefault(),U(!1)),null==M||M(e)}),[K,M]),F=a(s,(e=>{e&&"BUTTON"!==e.tagName&&g(!1)}));return r({},C,O?{ref:F,type:"button","aria-disabled":E?void 0:i,disabled:E,onClick:P,onMouseDown:f,onMouseUp:d,onKeyUp:m,onKeyDown:k,onMouseOver:D,onMouseLeave:M}:{ref:F,role:"button","data-active":o(K),"aria-disabled":i?"true":void 0,tabIndex:E?void 0:T,onClick:P,onMouseDown:I,onMouseUp:L,onKeyUp:S,onKeyDown:x,onMouseOver:N,onMouseLeave:A})}export{l as useClickable};
//# sourceMappingURL=use-clickable.js.map
