"use client";
import { __awaiter } from '../node_modules/.pnpm/@rollup_plugin-typescript@12.1.4_rollup@4.46.2_tslib@2.8.1_typescript@5.9.3/node_modules/tslib/tslib.es6.mjs';
import { jsx } from 'react/jsx-runtime';
import { SignUpButton, useClerk } from '@clerk/nextjs';
import { useEffect } from 'react';
import { useFingerprintContextSafe } from './fingerprint/fingerprint-provider.mjs';

function SignUpButtonWithFingerprint({ mode, signUp, }) {
    if (mode === 'redirect') {
        return (
        // 重定向模式则直接跳转到自定义注册页面
        jsx(SignUpButton, { children: jsx("button", { className: "w-16 sm:w-20 h-8 sm:h-9 px-1.5 sm:px-2 border border-gray-300 rounded-full hover:bg-gray-100 dark:border-gray-600 dark:hover:bg-gray-800 text-center text-xs sm:text-sm whitespace-nowrap", children: signUp }) }));
    }
    // 弹框模式则需要自定义注册按钮
    const fingerprintContext = useFingerprintContextSafe();
    // 如果没有fingerprint context，使用默认值
    const { fingerprintId = null, xUser = null, isInitialized = false, initializeAnonymousUser = () => __awaiter(this, void 0, void 0, function* () { }) } = fingerprintContext || {};
    const userId = (xUser === null || xUser === void 0 ? void 0 : xUser.userId) || null;
    const unsafeMetadata = {
        user_id: userId,
        fingerprint_id: fingerprintId || null,
    };
    // 确保匿名用户已初始化
    useEffect(() => {
        if (!isInitialized && fingerprintId) {
            initializeAnonymousUser();
        }
    }, [fingerprintId, isInitialized, initializeAnonymousUser]);
    // 调试日志和处理注册逻辑
    useEffect(() => {
        console.log('SignUpWithFingerprint on [modal] DEBUG:', {
            fingerprintProvider: fingerprintContext ? 'Available' : 'Not found',
            fingerprintId: fingerprintId || 'Not generated',
            xUser: xUser ? 'Initialized' : 'Not initialized',
            clerkMetadata: unsafeMetadata
        });
    }, [xUser, fingerprintId, fingerprintContext, unsafeMetadata]);
    const { openSignUp } = useClerk();
    const handleClick = () => {
        openSignUp({
            unsafeMetadata,
        });
        // 记录日志
        console.log('SignUpButton on [modal] clicked', {
            timestamp: new Date().toISOString(),
            mode,
            userId,
            fingerprintId
        });
    };
    return (jsx("button", { className: "w-16 sm:w-20 h-8 sm:h-9 px-1.5 sm:px-2 border border-gray-300 rounded-full hover:bg-gray-100 dark:border-gray-600 dark:hover:bg-gray-800 text-center text-xs sm:text-sm whitespace-nowrap", onClick: handleClick, children: signUp }));
}

export { SignUpButtonWithFingerprint };
