import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__CalculatorDisplay_mjs__ from "./CalculatorDisplay.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__ from "./CalculatorKey.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_mjs__ from "./style.mjs";
const CalculatorOperations = {
    '/': (prevValue, nextValue)=>prevValue / nextValue,
    '*': (prevValue, nextValue)=>prevValue * nextValue,
    '+': (prevValue, nextValue)=>prevValue + nextValue,
    '-': (prevValue, nextValue)=>prevValue - nextValue,
    '=': (prevValue, nextValue)=>nextValue
};
const Calculator = /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].forwardRef((param, ref)=>{
    let { className } = param;
    const [value, setValue] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(null);
    const [displayValue, setDisplayValue] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)('0');
    const [operator, setOperator] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(null);
    const [waitingForOperand, setWaitingForOperand] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const clearAll = ()=>{
        setValue(null);
        setDisplayValue('0');
        setOperator(null);
        setWaitingForOperand(false);
    };
    const clearDisplay = ()=>{
        setDisplayValue('0');
    };
    const clearLastChar = ()=>{
        setDisplayValue(displayValue.substring(0, displayValue.length - 1) || '0');
    };
    const toggleSign = ()=>{
        const newValue = -1 * parseFloat(displayValue);
        setDisplayValue(String(newValue));
    };
    const inputPercent = ()=>{
        const currentValue = parseFloat(displayValue);
        if (0 === currentValue) return;
        const fixedDigits = displayValue.replace(/^-?\d*\.?/, '');
        const newValue = parseFloat(displayValue) / 100;
        setDisplayValue(String(newValue.toFixed(fixedDigits.length + 2)));
    };
    const inputDot = ()=>{
        if (!/\./.test(displayValue)) {
            setDisplayValue(displayValue + '.');
            setWaitingForOperand(false);
        }
    };
    const inputDigit = (digit)=>{
        if (waitingForOperand) {
            setDisplayValue(String(digit));
            setWaitingForOperand(false);
        } else setDisplayValue('0' === displayValue ? String(digit) : displayValue + digit);
    };
    const performOperation = (nextOperator)=>{
        const inputValue = parseFloat(displayValue);
        if (null == value) setValue(inputValue);
        else if (operator) {
            const currentValue = value || 0;
            const newValue = CalculatorOperations[operator](currentValue, inputValue);
            setValue(newValue);
            setDisplayValue(String(newValue));
        }
        setWaitingForOperand(true);
        setOperator(nextOperator);
    };
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        const handleKeyDown = (event)=>{
            let { key } = event;
            if ('Enter' === key) key = '=';
            if (/\d/.test(key)) {
                event.preventDefault();
                inputDigit(parseInt(key, 10));
            } else if (key in CalculatorOperations) {
                event.preventDefault();
                performOperation(key);
            } else if ('.' === key) {
                event.preventDefault();
                inputDot();
            } else if ('%' === key) {
                event.preventDefault();
                inputPercent();
            } else if ('Backspace' === key) {
                event.preventDefault();
                clearLastChar();
            } else if ('Clear' === key) {
                event.preventDefault();
                if ('0' !== displayValue) clearDisplay();
                else clearAll();
            }
        };
        document.addEventListener('keydown', handleKeyDown);
        return ()=>{
            document.removeEventListener('keydown', handleKeyDown);
        };
    });
    const clearKey = '0' !== displayValue;
    const clearText = clearKey ? 'C' : 'AC';
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
        className: "calculator",
        ref: ref,
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorDisplay_mjs__.CalculatorDisplay, {
                value: displayValue
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                className: "calculator-keypad",
                children: [
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                        className: "input-keys",
                        children: [
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                                className: "function-keys",
                                children: [
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                        className: "key-clear",
                                        onPress: ()=>clearKey ? clearDisplay() : clearAll(),
                                        children: clearText
                                    }),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                        className: "key-sign",
                                        onPress: ()=>toggleSign(),
                                        children: "\xb1"
                                    }),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                        className: "key-percent",
                                        onPress: ()=>inputPercent(),
                                        children: "%"
                                    })
                                ]
                            }),
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                                className: "digit-keys",
                                children: [
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                        className: "key-0",
                                        onPress: ()=>inputDigit(0),
                                        children: "0"
                                    }),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                        className: "key-dot",
                                        onPress: ()=>inputDot(),
                                        children: "●"
                                    }),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                        className: "key-1",
                                        onPress: ()=>inputDigit(1),
                                        children: "1"
                                    }),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                        className: "key-2",
                                        onPress: ()=>inputDigit(2),
                                        children: "2"
                                    }),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                        className: "key-3",
                                        onPress: ()=>inputDigit(3),
                                        children: "3"
                                    }),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                        className: "key-4",
                                        onPress: ()=>inputDigit(4),
                                        children: "4"
                                    }),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                        className: "key-5",
                                        onPress: ()=>inputDigit(5),
                                        children: "5"
                                    }),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                        className: "key-6",
                                        onPress: ()=>inputDigit(6),
                                        children: "6"
                                    }),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                        className: "key-7",
                                        onPress: ()=>inputDigit(7),
                                        children: "7"
                                    }),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                        className: "key-8",
                                        onPress: ()=>inputDigit(8),
                                        children: "8"
                                    }),
                                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                        className: "key-9",
                                        onPress: ()=>inputDigit(9),
                                        children: "9"
                                    })
                                ]
                            })
                        ]
                    }),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                        className: "operator-keys",
                        children: [
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                className: "key-divide",
                                onPress: ()=>performOperation('/'),
                                children: "\xf7"
                            }),
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                className: "key-multiply",
                                onPress: ()=>performOperation('*'),
                                children: "\xd7"
                            }),
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                className: "key-subtract",
                                onPress: ()=>performOperation('-'),
                                children: "−"
                            }),
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                className: "key-add",
                                onPress: ()=>performOperation('+'),
                                children: "+"
                            }),
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__CalculatorKey_mjs__.CalculatorKey, {
                                className: "key-equals",
                                onPress: ()=>performOperation('='),
                                children: "="
                            })
                        ]
                    })
                ]
            })
        ]
    });
});
const CalculatorWrapper = /*#__PURE__*/ __WEBPACK_EXTERNAL_MODULE_react__["default"].forwardRef((props, ref)=>{
    const { styles } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Calculator, {
        ref: ref,
        className: styles.container
    });
});
export { CalculatorWrapper };
