import React from"react";import{CssClasses,FormElementTypes}from"./constants.js";import FormElementOuter from"./FormElementOuter.js";import{getKeyString}from"./keyboard.js";import{isNumberRef,isString,makeSafeClassName}from"./utils.js";const NumberRangeSlider=React.lazy(()=>import("./components/NumberRangeSlider"));function renderNumberRangeSlider(e,t){return React.createElement(React.Suspense,{fallback:null},React.createElement(NumberRangeSlider,{component:e,element:t}))}function renderRangeFormat(e,t){return t=null==(t=isNumberRef(e)?e.format:t)?{}:t}function renderNumber(n,o){const t=n.props.name,a=n.props.host;let u=renderRangeFormat(o.value,o.format),l=u.precision;"number"==typeof l&&0*l==0||(l=3);const i=Math.pow(10,-1*l);let s=Math.round(u.step/i)*i;"number"==typeof s&&0*s<s||(s=void 0);let m=Math.floor(u.lowerBound/i)*i;"number"==typeof m&&0*m==0||(m=void 0);let c=Math.ceil(u.upperBound/i)*i;"number"==typeof c&&0*c==0||(c=void 0),void 0!==m&&void 0!==c&&c<=m&&(m=void 0,c=void 0);const e=[CssClasses.NUMERIC,makeSafeClassName(t)];let r=!1;o.type===FormElementTypes.NUMBERSLIDER&&(e.push(CssClasses.SLIDER),r=!0,void 0===s&&(s=Math.max(1,i)),void 0===m&&(m=0),void 0===c&&(c=100)),void 0!==s&&e.push(CssClasses.STEPPER),u={precision:l,step:s,lowerBound:m,upperBound:c};const p={useGrouping:!0,minimumFractionDigits:0<l?l:0,maximumFractionDigits:0<l?l:0};function d(t){var e,n,r;if(t=function(e){if("string"==typeof e){if(e.length<1)return;e=Number(e)}if("number"==typeof e&&0*e==0)return void 0!==c&&e>c&&(e=c),void 0!==m&&e<m&&(e=m),Math.round(e/i)*i}(t),o.type===FormElementTypes.NUMBERSLIDER&&void 0===t&&void 0!==m&&void 0!==c&&(e=(m+c)/2,r=s||i,(n=(e-m)/r)%1==0?t=e:(n=Math.ceil(n),(r=m+n*r)<=c&&(t=r))),void 0===t)return o.value=void 0,"";{o.value={refValueType:"number",format:u,display:a.formatNumber(t,p),numeric:t};let e=l;return e=20<e?20:e,e=e<0?0:e,t.toFixed(e)}}let f=null,v=null,b=(R=o.value,isNumberRef(R)&&(R=R.numeric),d("number"==typeof(R=isString(R)?a.coerceNumber(R):R)?R:void 0));var R;function g(e){return e=d(e),f.value=v.value=e,y=f.value,b!==e&&(b=e,a.post("changed",{name:t,value:o.value}),1)}let y=void 0;function E(){var e=f.value;y!==e&&g(e)}var N=a.renderText(o.prompt),h=a.renderText(o.tooltip);return React.createElement(FormElementOuter,{className:e.join(" "),component:n,element:o,errorClassName:CssClasses.ERROR,htmlElementType:"label",ref:function(e){null!==e&&null===e.querySelector("*:focus")&&(f.value=v.value=b,y=f.value,a.renderFocus(r?f:v,t))}},React.createElement("input",Object.assign({placeholder:N,title:h},{type:"range",disabled:!n.enabled,min:m,max:c,step:s,onMouseMove:E,onMouseDown:E,onChange:E,onInput:E,ref(e){f=e}})),React.createElement("input",Object.assign({placeholder:N,title:h},{type:"number",disabled:!n.enabled,min:m,max:c,step:s,onKeyDown:function(e){var t=getKeyString(e);if("enter"===t&&g(v.value))return v.select(),e.preventDefault(),e.stopPropagation(),!1;"escape"===t&&(f.value=v.value=b,v.select()),n.autoInput=!1},onFocus:function(){v.select()},onMouseDown:function(){n.autoInput=!0},onChange:function(){n.autoInput&&(g(v.value),v.select())},onBlur:function(){g(v.value)},ref:function(e){v=e}})))}export{renderNumberRangeSlider,renderNumber};