"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResendCodeButton = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const Button_1 = require("../../Button");
const Typography_1 = require("../../Typography");
const useSecondsCountDown_1 = require("../../useSecondsCountDown");
const styles_1 = require("./styles");
const ResendCodeButton = ({ loading, disabled, resendTimeout, onResendCode, clearCodeValue, }) => {
    const { isActive, textTime: time, restart, } = (0, useSecondsCountDown_1.useSecondsCountdown)({
        seconds: resendTimeout,
    });
    const [resendCodeLoading, setResendCodeLoading] = (0, react_1.useState)(false);
    const isTimerActive = isActive && time !== '00:00';
    const showTimer = !disabled && !loading && !resendCodeLoading && isTimerActive;
    const disableButton = disabled || loading || resendCodeLoading || isTimerActive;
    (0, react_1.useEffect)(() => {
        restart(resendTimeout);
    }, [resendTimeout, resendCodeLoading]);
    const onClick = () => {
        setResendCodeLoading(true);
        if (onResendCode) {
            onResendCode()
                .then(() => {
                clearCodeValue();
            })
                .catch((error) => {
                // biome-ignore lint/suspicious/noConsole: Ошибка для интеграторов
                console.error(error);
            })
                .finally(() => setResendCodeLoading(false));
        }
    };
    return ((0, jsx_runtime_1.jsxs)(styles_1.Wrapper, { children: [(0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "link", disabled: disableButton, onClick: onClick, children: "\u041E\u0442\u043F\u0440\u0430\u0432\u0438\u0442\u044C \u043A\u043E\u0434 \u043F\u043E\u0432\u0442\u043E\u0440\u043D\u043E" }), showTimer && ((0, jsx_runtime_1.jsx)(Typography_1.Typography, { color: "grey", colorIntensity: "700", children: time }))] }));
};
exports.ResendCodeButton = ResendCodeButton;
