import { useLingui } from '../../../../../../node_modules/@lingui/react/dist/index.mjs';
import React__default, { useState } from 'react';
import { getTranslatedError } from '../../../utils/getTranslatedError.mjs';
import Button from '../atoms/Button.mjs';
import Column from '../atoms/Column.mjs';
import { useCountdown, useTimeRemaining } from '../atoms/Countdown.mjs';
import { errorToast } from '../atoms/Toast.mjs';
import Typography from '../atoms/Typography.mjs';
import VerticalTransition from '../atoms/VerticalTransition.mjs';
import ButtonColumn from './ButtonColumn.mjs';

/**
 * Handles common layouts for simple email actions
 */ const EmailActionLayout = ({ header, description, children, additionalActions, resend, sendMagicLink, goBack })=>{
    const { i18n: $__i18n, _: $__ } = useLingui();
    const [emailResending, setEmailResending] = useState(false);
    const [magicLinkSent, setMagicLinkSent] = useState(false);
    const countdown = useCountdown();
    const [timeRemaining, active] = useTimeRemaining(countdown.expiration);
    const resendButton = resend && /*#__PURE__*/ React__default.createElement(Button, {
        variant: "outline",
        onClick: async ()=>{
            setEmailResending(true);
            try {
                await resend();
                countdown.start(15);
            } catch (error) {
                errorToast({
                    message: getTranslatedError(error, $__)
                });
                countdown.clear();
            } finally{
                setEmailResending(false);
            }
        },
        loading: emailResending,
        disabled: emailResending || active
    }, /*#__PURE__*/ React__default.createElement(VerticalTransition, {
        className: (void 0),
        triggered: active,
        primary: $__i18n._({
            id: "button.resendEmail",
            message: "Resend email"
        }),
        secondary: $__i18n._({
            id: "button.emailResent",
            message: "Email resent! Try again in {timeRemaining}s",
            values: {
                timeRemaining: timeRemaining
            }
        })
    }));
    const goBackButton = goBack && /*#__PURE__*/ React__default.createElement(Button, {
        variant: "ghost",
        onClick: goBack
    }, $__i18n._({
        id: "button.goBack",
        message: "Go back"
    }));
    const actions = sendMagicLink ? /*#__PURE__*/ React__default.createElement(ButtonColumn, {
        top: /*#__PURE__*/ React__default.createElement(React__default.Fragment, null, additionalActions, resendButton),
        bottom: /*#__PURE__*/ React__default.createElement(React__default.Fragment, null, /*#__PURE__*/ React__default.createElement(Button, {
            variant: "outline",
            onClick: ()=>{
                setMagicLinkSent(true);
                sendMagicLink().catch(()=>setMagicLinkSent(false));
            },
            loading: magicLinkSent
        }, $__i18n._({
            id: "button.loginWithoutPassword",
            message: "Log in without a password"
        })), goBackButton)
    }) : /*#__PURE__*/ React__default.createElement(ButtonColumn, null, additionalActions, resendButton, goBackButton);
    return /*#__PURE__*/ React__default.createElement(Column, {
        gap: 6
    }, /*#__PURE__*/ React__default.createElement(Typography, {
        variant: "header"
    }, header), /*#__PURE__*/ React__default.createElement(Typography, null, description), children, actions);
};

export { EmailActionLayout as default };
//# sourceMappingURL=EmailActionLayout.mjs.map
