"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    CalculatorWrapper: ()=>CalculatorWrapper
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const external_CalculatorDisplay_js_namespaceObject = require("./CalculatorDisplay.js");
const external_CalculatorKey_js_namespaceObject = require("./CalculatorKey.js");
const external_style_js_namespaceObject = require("./style.js");
const CalculatorOperations = {
    '/': (prevValue, nextValue)=>prevValue / nextValue,
    '*': (prevValue, nextValue)=>prevValue * nextValue,
    '+': (prevValue, nextValue)=>prevValue + nextValue,
    '-': (prevValue, nextValue)=>prevValue - nextValue,
    '=': (prevValue, nextValue)=>nextValue
};
const Calculator = /*#__PURE__*/ external_react_default().forwardRef((param, ref)=>{
    let { className } = param;
    const [value, setValue] = (0, external_react_namespaceObject.useState)(null);
    const [displayValue, setDisplayValue] = (0, external_react_namespaceObject.useState)('0');
    const [operator, setOperator] = (0, external_react_namespaceObject.useState)(null);
    const [waitingForOperand, setWaitingForOperand] = (0, external_react_namespaceObject.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, external_react_namespaceObject.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, jsx_runtime_namespaceObject.jsxs)("div", {
        className: "calculator",
        ref: ref,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorDisplay_js_namespaceObject.CalculatorDisplay, {
                value: displayValue
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                className: "calculator-keypad",
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                        className: "input-keys",
                        children: [
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                                className: "function-keys",
                                children: [
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                        className: "key-clear",
                                        onPress: ()=>clearKey ? clearDisplay() : clearAll(),
                                        children: clearText
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                        className: "key-sign",
                                        onPress: ()=>toggleSign(),
                                        children: "\xb1"
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                        className: "key-percent",
                                        onPress: ()=>inputPercent(),
                                        children: "%"
                                    })
                                ]
                            }),
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                                className: "digit-keys",
                                children: [
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                        className: "key-0",
                                        onPress: ()=>inputDigit(0),
                                        children: "0"
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                        className: "key-dot",
                                        onPress: ()=>inputDot(),
                                        children: "●"
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                        className: "key-1",
                                        onPress: ()=>inputDigit(1),
                                        children: "1"
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                        className: "key-2",
                                        onPress: ()=>inputDigit(2),
                                        children: "2"
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                        className: "key-3",
                                        onPress: ()=>inputDigit(3),
                                        children: "3"
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                        className: "key-4",
                                        onPress: ()=>inputDigit(4),
                                        children: "4"
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                        className: "key-5",
                                        onPress: ()=>inputDigit(5),
                                        children: "5"
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                        className: "key-6",
                                        onPress: ()=>inputDigit(6),
                                        children: "6"
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                        className: "key-7",
                                        onPress: ()=>inputDigit(7),
                                        children: "7"
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                        className: "key-8",
                                        onPress: ()=>inputDigit(8),
                                        children: "8"
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                        className: "key-9",
                                        onPress: ()=>inputDigit(9),
                                        children: "9"
                                    })
                                ]
                            })
                        ]
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                        className: "operator-keys",
                        children: [
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                className: "key-divide",
                                onPress: ()=>performOperation('/'),
                                children: "\xf7"
                            }),
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                className: "key-multiply",
                                onPress: ()=>performOperation('*'),
                                children: "\xd7"
                            }),
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                className: "key-subtract",
                                onPress: ()=>performOperation('-'),
                                children: "−"
                            }),
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                className: "key-add",
                                onPress: ()=>performOperation('+'),
                                children: "+"
                            }),
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CalculatorKey_js_namespaceObject.CalculatorKey, {
                                className: "key-equals",
                                onPress: ()=>performOperation('='),
                                children: "="
                            })
                        ]
                    })
                ]
            })
        ]
    });
});
const CalculatorWrapper = /*#__PURE__*/ external_react_default().forwardRef((props, ref)=>{
    const { styles } = (0, external_style_js_namespaceObject.useStyles)();
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Calculator, {
        ref: ref,
        className: styles.container
    });
});
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
