import{r as e,z as n}from"./vendor-s2rfM_Vj.js";import{D as t}from"./t-r-vendor-gn7FMrPE.js";const l={"*":"×","/":"÷",Enter:"=",Backspace:"←",Delete:"AC"};function s({item:e,onClick:t}){return n.jsx("div",{className:"button "+("="===e?"confirm":"number"==typeof e?"num":"op"),onClick:t,children:e})}function r(e){const n=[];let t=0;for(let l=0;l<e.length;l++)/^[\d.]$/.test(e[l])||("-"!==e[l]||0!==l&&"("!==e[l-1])&&(t<l&&n.push(e.slice(t,l)),n.push(e[l]),t=l+1);return t<e.length&&n.push(e.slice(t)),n}function i({visible:i,handleVisible:o}){const[u,c]=e.useState("0"),[a,f]=e.useState(!1),d=r(u),h=function(e){const n=[],t=[];function l(e){return"("===e?0:"+"===e||"-"===e?1:2}function s(e){if(n.length<2)return null;const t=n.pop(),l=n.pop();switch(e){case"+":return l+t;case"-":return l-t;case"×":return l*t;case"÷":return l/t;default:return null}}const r=e.filter((e=>"("===e)).length,i=e.filter((e=>")"===e)).length;if(i<r)for(let o=0;o<r-i;o++)e.push(")");for(const o of e)if("+-×÷".includes(o)){let e=t.length-1;for(;e>=0&&"("!==t[e]&&l(t[e])>=l(o);){const l=s(t.pop());if(null===l)return null;n.push(l),e--}t.push(o)}else if("("===o)t.push(o);else if(")"===o){let e=t.length-1;for(;e>=0&&"("!==t[e];){const l=s(t.pop());if(null===l)return null;n.push(l),e--}if(e<0)return null;t.pop()}else{const e=parseFloat(o);if(isNaN(e))return null;n.push(e)}for(;t.length;){const e=s(t.pop());if(null===e)return null;n.push(e)}return 1!==n.length?null:n[0]}([...d]),p=e.useCallback((e=>{let n=u;if(a){if("←"!==e)if("+-×÷".includes(""+e)){if(Math.abs(h)>999999999999999)return;n=h}else n="0";f(!1)}const t=r(n),l=t.at(-1);if("number"==typeof e){if(")"===l||l.length>=15)return;c("0"===l?n.slice(0,-1)+e:n+e)}else if("."===e){if(")"===l||l.length>=15||l.includes("."))return;c(n+".")}else if("+-×÷".includes(e)){if("("===l&&"-"!==e)return;if("-"===l&&t.length>=2&&"("===t.at(-2))return;c("0"===n&&"-"===e?e:"+-×÷".includes(l)?n.slice(0,-1)+e:n+e)}else if("("===e)"0"===n?c(e):"+-×÷(".includes(n.at(-1)??"")&&c(n+e);else if(")"===e){if("+-×÷(".includes(n.at(-1)??""))return;const t=n.split(/[0-9.]*/);t.filter((e=>"("===e)).length>t.filter((e=>")"===e)).length&&c(n+e)}else"="===e?f(!0):"←"===e?c(n.length>1?n.slice(0,-1):"0"):"AC"===e&&c("0")}),[u,h,a]);return e.useEffect((()=>{function e(e){"Escape"===e.key?f(!1):"0123456789".includes(e.key)?p(parseInt(e.key)):l[e.key]?p(l[e.key]):"+-().=".includes(e.key)&&p(e.key)}return window.addEventListener("keydown",e),()=>window.removeEventListener("keydown",e)}),[p]),n.jsx(t,{className:"calculator-dialog",style:{padding:0,overflow:"auto",backgroundColor:"var(--theme-black7)",backdropFilter:"blur(5px)",height:"fit-content"},width:"fit-content",visible:i,mode:"modal",onClose:()=>o(!1),preventScrollThrough:!0,showOverlay:!0,theme:"default",footer:!1,header:!1,closeBtn:!1,closeOnEscKeydown:!0,closeOnOverlayClick:!0,confirmOnEnter:!1,destroyOnClose:!0,children:n.jsxs("div",{className:"calculator",children:[n.jsx("div",{className:"line "+(a?"extra":"main"),onClick:()=>f(!1),children:d.join(" ")}),n.jsx("div",{className:"res "+(a?"main":"extra"),children:null!==h?Math.abs(h)>999999999999999?h.toExponential():h:a?"错误":""}),n.jsx("div",{className:"buttons",children:["(",")","←","÷",7,8,9,"×",4,5,6,"-",1,2,3,"+","AC",0,".","="].map((e=>n.jsx(s,{item:e,onClick:()=>p(e)},e)))})]})})}export{i as default};
