"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 { jsxs, Fragment, jsx } from 'react/jsx-runtime';
import { useClerk } from '@clerk/nextjs';
import { cn } from '@windrun-huaiin/lib/utils';
import { useRouter } from 'next/navigation';
import { useMemo, useState, useRef, useEffect, useCallback } from 'react';
import { MoneyPriceButton } from './money-price-button.mjs';
import { getActiveProviderConfigUtil, getProductPricing } from './money-price-config-util.mjs';
import { UserState } from './money-price-types.mjs';
import { redirectToCustomerPortal } from './customer-portal.mjs';

const PLAN_KEYS = ['F1', 'P2', 'U3'];
function MoneyPriceInteractive({ data, config, checkoutApiEndpoint, customerPortalApiEndpoint, enableClerkModal = false, enabledBillingTypes, enableSubscriptionUpgrade = true, initialBillingType, disableAutoDetectBilling = false, initUserContext, }) {
    var _a, _b, _c, _d;
    const { redirectToSignIn, redirectToSignUp, user: clerkUser, openSignUp } = useClerk();
    const router = useRouter();
    const providerConfig = useMemo(() => getActiveProviderConfigUtil(config), [config]);
    const billingOptions = useMemo(() => {
        const options = data.billingSwitch.options;
        // 如果配置了 enabledBillingTypes，只显示配置的类型
        if (enabledBillingTypes === null || enabledBillingTypes === void 0 ? void 0 : enabledBillingTypes.length) {
            return options.filter(option => enabledBillingTypes.includes(option.key));
        }
        // 否则显示所有配置的选项
        return options;
    }, [data.billingSwitch.options, enabledBillingTypes]);
    const billingOptionMap = useMemo(() => {
        return billingOptions.reduce((acc, option) => {
            acc[option.key] = option;
            return acc;
        }, {});
    }, [billingOptions]);
    const defaultBilling = useMemo(() => {
        var _a;
        const defaultKey = data.billingSwitch.defaultKey;
        // 如果默认值在可用选项中，使用默认值
        if (billingOptions.some(opt => opt.key === defaultKey)) {
            return defaultKey;
        }
        // 否则使用第一个可用选项
        return ((_a = billingOptions[0]) === null || _a === void 0 ? void 0 : _a.key) || 'monthly';
    }, [data.billingSwitch.defaultKey, billingOptions]);
    const resolvedInitialBilling = useMemo(() => {
        if (initialBillingType &&
            billingOptions.some(option => option.key === initialBillingType)) {
            return initialBillingType;
        }
        return defaultBilling;
    }, [initialBillingType, billingOptions, defaultBilling]);
    const priceIdsByCycle = useMemo(() => {
        const priceIds = {};
        // 为每个可用的计费类型创建价格ID数组
        billingOptions.forEach(option => {
            priceIds[option.key] = [];
            if (option.key === 'onetime') {
                // 处理积分包产品
                const creditPacks = providerConfig.creditPackProducts || {};
                Object.values(creditPacks).forEach((pack) => {
                    priceIds[option.key].push(pack.priceId);
                });
            }
            else {
                // 处理订阅产品
                const products = providerConfig.subscriptionProducts || providerConfig.products || {};
                PLAN_KEYS.forEach(planKey => {
                    const product = products[planKey];
                    if (product && product.plans && product.plans[option.key]) {
                        priceIds[option.key].push(product.plans[option.key].priceId);
                    }
                });
            }
        });
        return priceIds;
    }, [providerConfig, billingOptions]);
    const serverAuthenticated = !!(initUserContext === null || initUserContext === void 0 ? void 0 : initUserContext.isClerkAuthenticated) && !!((_a = initUserContext === null || initUserContext === void 0 ? void 0 : initUserContext.xUser) === null || _a === void 0 ? void 0 : _a.clerkUserId);
    const clientAuthenticated = !!(clerkUser === null || clerkUser === void 0 ? void 0 : clerkUser.id);
    const isAuthenticated = serverAuthenticated || clientAuthenticated;
    const subscriptionSnapshot = (initUserContext === null || initUserContext === void 0 ? void 0 : initUserContext.isClerkAuthenticated) ? initUserContext === null || initUserContext === void 0 ? void 0 : initUserContext.xSubscription : null;
    const detectedBillingType = useMemo(() => {
        if (disableAutoDetectBilling) {
            return null;
        }
        if (!subscriptionSnapshot || subscriptionSnapshot.status !== 'active') {
            return null;
        }
        const priceId = subscriptionSnapshot.priceId;
        if (!priceId) {
            return null;
        }
        for (const [cycle, priceIds] of Object.entries(priceIdsByCycle)) {
            if (priceIds.includes(priceId)) {
                return cycle;
            }
        }
        return null;
    }, [
        disableAutoDetectBilling,
        subscriptionSnapshot === null || subscriptionSnapshot === void 0 ? void 0 : subscriptionSnapshot.status,
        subscriptionSnapshot === null || subscriptionSnapshot === void 0 ? void 0 : subscriptionSnapshot.priceId,
        priceIdsByCycle,
    ]);
    const initialBillingCandidate = useMemo(() => {
        if (initialBillingType) {
            return resolvedInitialBilling;
        }
        if (detectedBillingType) {
            return detectedBillingType;
        }
        return resolvedInitialBilling;
    }, [initialBillingType, resolvedInitialBilling, detectedBillingType]);
    const [billingType, setBillingType] = useState(initialBillingCandidate);
    const navigationLockRef = useRef(false);
    useEffect(() => {
        setBillingType(prev => (prev === initialBillingCandidate ? prev : initialBillingCandidate));
    }, [initialBillingCandidate]);
    const [isProcessing, setIsProcessing] = useState(false);
    const [isTouchDevice, setIsTouchDevice] = useState(false);
    useEffect(() => {
        setIsTouchDevice('ontouchstart' in window || navigator.maxTouchPoints > 0);
    }, []);
    const getUserState = useCallback(() => {
        var _a, _b;
        if (!subscriptionSnapshot || subscriptionSnapshot.status !== 'active') {
            return UserState.FreeUser;
        }
        const userPriceId = subscriptionSnapshot.priceId;
        if (!userPriceId)
            return UserState.FreeUser;
        const products = providerConfig.subscriptionProducts || providerConfig.products || {};
        const proPlans = ((_a = products.P2) === null || _a === void 0 ? void 0 : _a.plans) || {};
        const proIds = Object.values(proPlans).map((plan) => plan.priceId);
        if (proIds.includes(userPriceId)) {
            return UserState.ProUser;
        }
        const ultraPlans = ((_b = products.U3) === null || _b === void 0 ? void 0 : _b.plans) || {};
        const ultraIds = Object.values(ultraPlans).map((plan) => plan.priceId);
        if (ultraIds.includes(userPriceId)) {
            return UserState.UltraUser;
        }
        return UserState.FreeUser;
    }, [subscriptionSnapshot, providerConfig]);
    const userContext = useMemo(() => {
        return {
            isAuthenticated,
            subscriptionStatus: isAuthenticated ? getUserState() : UserState.Anonymous,
            subscriptionType: isAuthenticated ? detectedBillingType !== null && detectedBillingType !== void 0 ? detectedBillingType : undefined : undefined,
            subscriptionEndDate: isAuthenticated ? subscriptionSnapshot === null || subscriptionSnapshot === void 0 ? void 0 : subscriptionSnapshot.subPeriodEnd : undefined
        };
    }, [
        isAuthenticated,
        getUserState,
        detectedBillingType,
        subscriptionSnapshot === null || subscriptionSnapshot === void 0 ? void 0 : subscriptionSnapshot.subPeriodEnd
    ]);
    const fingerprintId = (_b = initUserContext === null || initUserContext === void 0 ? void 0 : initUserContext.fingerprintId) !== null && _b !== void 0 ? _b : null;
    const initUserId = (_d = (_c = initUserContext === null || initUserContext === void 0 ? void 0 : initUserContext.xUser) === null || _c === void 0 ? void 0 : _c.userId) !== null && _d !== void 0 ? _d : null;
    const handleAuth = useCallback(() => {
        if (!enableClerkModal) {
            redirectToSignUp();
            return;
        }
        if (!fingerprintId) {
            console.warn('Not found fingerprintId! NEED CHECK!');
        }
        const unsafeMetadata = {
            user_id: initUserId,
            fingerprint_id: fingerprintId,
        };
        openSignUp({ unsafeMetadata });
    }, [enableClerkModal, redirectToSignUp, openSignUp, initUserId, fingerprintId]);
    const handleAction = useCallback((plan, billing) => __awaiter(this, void 0, void 0, function* () {
        var _a;
        const isSubscriptionFlow = billing !== 'onetime';
        if (isSubscriptionFlow && !enableSubscriptionUpgrade) {
            return;
        }
        navigationLockRef.current = false;
        setIsProcessing(true);
        const markNavigating = () => {
            navigationLockRef.current = true;
        };
        try {
            const hasActiveSubscription = userContext.isAuthenticated &&
                (userContext.subscriptionStatus === UserState.ProUser ||
                    userContext.subscriptionStatus === UserState.UltraUser);
            const shouldUsePortal = isSubscriptionFlow && hasActiveSubscription;
            if (shouldUsePortal) {
                const handled = yield redirectToCustomerPortal({
                    customerPortalApiEndpoint,
                    redirectToSignIn,
                    returnUrl: window.location.href,
                });
                if (handled) {
                    markNavigating();
                    return;
                }
            }
            if (!checkoutApiEndpoint) {
                markNavigating();
                router.push('/');
                return;
            }
            const pricing = getProductPricing(plan, billing, config.activeProvider, config);
            const response = yield fetch(checkoutApiEndpoint, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    priceId: pricing.priceId,
                    plan,
                    billingType: billing,
                    provider: config.activeProvider
                })
            });
            if (response.redirected || response.status === 302 || response.status === 301) {
                markNavigating();
                window.location.href = response.url;
                return;
            }
            const contentType = response.headers.get('content-type');
            if (!contentType || !contentType.includes('application/json')) {
                console.error('Received non-JSON response, user may need to login');
                markNavigating();
                redirectToSignIn();
                return;
            }
            const result = yield response.json();
            if (!response.ok) {
                const errorMessage = result.error || `Request failed with status ${response.status}`;
                console.error('Upgrade request failed:', errorMessage);
                if (response.status === 401 || response.status === 403) {
                    markNavigating();
                    redirectToSignIn();
                }
                else {
                    alert(`Operation failed: ${errorMessage}`);
                }
                return;
            }
            if (result.success && ((_a = result.data) === null || _a === void 0 ? void 0 : _a.sessionUrl)) {
                markNavigating();
                window.location.href = result.data.sessionUrl;
            }
            else {
                console.error('Failed to create checkout session:', result.error || 'Unknown error');
            }
        }
        catch (error) {
            console.error('Error during upgrade:', error);
        }
        finally {
            if (!navigationLockRef.current) {
                setIsProcessing(false);
            }
        }
    }), [
        customerPortalApiEndpoint,
        checkoutApiEndpoint,
        config,
        router,
        redirectToSignIn,
        userContext,
        enableSubscriptionUpgrade
    ]);
    // 根据当前计费类型动态选择要显示的 plans
    const currentPlans = useMemo(() => {
        if (billingType === 'onetime') {
            return data.creditsPlans || [];
        }
        return data.subscriptionPlans || [];
    }, [billingType, data.subscriptionPlans, data.creditsPlans]);
    const maxFeaturesCount = useMemo(() => {
        const featureCounts = currentPlans.map(plan => { var _a; return ((_a = plan.features) === null || _a === void 0 ? void 0 : _a.length) || 0; });
        return Math.max(config.display.minFeaturesCount || 0, ...featureCounts);
    }, [currentPlans, config.display.minFeaturesCount]);
    const getFeatureRows = useCallback((plan) => {
        const features = plan.features || [];
        const filled = [...features];
        while (filled.length < maxFeaturesCount)
            filled.push(null);
        return filled;
    }, [maxFeaturesCount]);
    const getPricingForPlan = useCallback((planKey) => {
        return getProductPricing(planKey, billingType, config.activeProvider, config);
    }, [billingType, config]);
    const selectedBillingOption = billingOptionMap[billingType];
    const discountBadgeText = useMemo(() => {
        if (!(selectedBillingOption === null || selectedBillingOption === void 0 ? void 0 : selectedBillingOption.discountText))
            return null;
        // 对于 onetime 模式，直接显示 discountText，不依赖 discountPercent
        if (billingType === 'onetime') {
            return selectedBillingOption.discountText;
        }
        // 对于订阅模式，查找 discountPercent 并替换
        let discountPercent = null;
        const products = providerConfig.subscriptionProducts || providerConfig.products || {};
        PLAN_KEYS.forEach(planKey => {
            var _a, _b;
            const product = products[planKey];
            if ((_b = (_a = product === null || product === void 0 ? void 0 : product.plans) === null || _a === void 0 ? void 0 : _a[billingType]) === null || _b === void 0 ? void 0 : _b.discountPercent) {
                discountPercent = product.plans[billingType].discountPercent;
            }
        });
        if (!discountPercent)
            return null;
        return selectedBillingOption.discountText.replace('{percent}', String(discountPercent));
    }, [selectedBillingOption, providerConfig, billingType]);
    // 配置移动端BillingTypeButton悬浮样式
    return (jsxs(Fragment, { children: [jsx("div", { className: "flex justify-center mb-6 max-md:sticky max-md:top-30 max-md:z-30 max-md:py-2 max-md:bg-transparent", children: jsx("div", { className: "inline-flex bg-white dark:bg-gray-900 border border-gray-300 dark:border-gray-700 rounded-full px-2 py-2 sm:px-3 sm:py-3 max-md:w-full max-md:max-w-[340px] max-md:mx-auto shadow-sm", "data-billing-switch": true, children: billingOptions.map(option => {
                        const isActive = option.key === billingType;
                        const buttonClasses = isActive
                            ? 'text-white bg-linear-to-r from-purple-400 to-pink-500 hover:from-purple-500 hover:to-pink-600 dark:from-purple-500 dark:to-pink-600 dark:hover:from-purple-600 rounded-full shadow-sm'
                            : 'text-gray-800 dark:text-gray-200 hover:text-gray-900 dark:hover:text-gray-100 rounded-full';
                        const showBadge = option.key === billingType && !!discountBadgeText;
                        return (jsxs("div", { className: "relative flex items-center justify-center mx-1", children: [showBadge && (jsx("span", { className: "absolute z-10 left-1/2 -translate-x-1/2 -top-3 sm:-top-4 translate-y-[-50%] px-3 py-0.5  text-[0.625rem] sm:text-xs rounded-md bg-yellow-100 text-yellow-800 font-semibold shadow-sm whitespace-nowrap", children: discountBadgeText })), jsx("button", { className: cn('text-sm md:text-base font-medium transition relative text-center z-10 px-2 sm:px-4 py-2 min-w-[100px] sm:min-w-[120px]', buttonClasses), type: "button", "data-billing-button": option.key, onClick: () => setBillingType(option.key), children: option.name })] }, option.key));
                    }) }) }), jsx("div", { className: "w-full", children: jsx("div", { className: "flex flex-wrap justify-center gap-5 md:gap-6 xl:gap-8 w-full max-w-6xl mx-auto", children: currentPlans.map((plan) => {
                        const planKey = plan.key;
                        if (!PLAN_KEYS.includes(planKey)) {
                            console.warn(`Unknown plan key "${plan.key}" detected in pricing plans`);
                            return null;
                        }
                        const pricing = getPricingForPlan(planKey);
                        const showBillingSubtitle = plan.showBillingSubTitle !== false;
                        const hasDiscount = !!pricing.discountPercent && !!pricing.originalAmount;
                        // 移动端宽度样式
                        return (jsxs("div", { "data-price-plan": planKey, className: cn('flex flex-col bg-white dark:bg-gray-800/60 rounded-2xl border border-gray-300 dark:border-[#7c3aed40] transition p-5 md:p-8 h-full shadow-sm dark:shadow-none w-[85vw] max-w-[360px]', 'md:w-[clamp(280px,32vw,360px)] md:max-w-[360px] md:shrink-0', 'hover:border-2 hover:border-purple-500', 'focus-within:border-2 focus-within:border-purple-500'), style: { minHeight: maxFeaturesCount * (isTouchDevice ? 86 : 100) }, children: [jsxs("div", { className: "flex items-center gap-2 mb-2", children: [jsx("span", { className: "text-lg md:text-xl font-bold text-gray-900 dark:text-gray-100", children: plan.title }), plan.titleTags && plan.titleTags.map((tag, i) => (jsx("span", { className: "px-2 py-0.5 text-xs rounded bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200 font-semibold align-middle", children: tag }, i)))] }), jsxs("div", { className: "flex flex-col items-start w-full", "data-price-container": planKey, children: [jsxs("div", { className: "flex items-end gap-2", children: [jsx("span", { className: "text-3xl md:text-4xl font-extrabold text-gray-900 dark:text-gray-100", "data-price-value": planKey, children: pricing.amount === 0 ? 'Free' : `${data.currency}${pricing.amount}` }), pricing.amount > 0 && (jsx("span", { className: "text-base md:text-lg text-gray-700 dark:text-gray-300 font-medium mb-1", "data-price-unit": planKey, children: (selectedBillingOption === null || selectedBillingOption === void 0 ? void 0 : selectedBillingOption.unit) || '/month' }))] }), jsxs("div", { className: "flex flex-col md:flex-row items-start md:items-center gap-1 md:gap-2 min-h-[28px] mt-1", children: [hasDiscount && (jsxs(Fragment, { children: [jsxs("span", { className: "text-sm md:text-base text-gray-400 line-through", "data-price-original": planKey, children: [data.currency, pricing.originalAmount] }), (selectedBillingOption === null || selectedBillingOption === void 0 ? void 0 : selectedBillingOption.discountText) && (jsx("span", { className: "px-2 py-0.5 text-[11px] md:text-xs rounded bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200 font-semibold align-middle", "data-price-discount": planKey, children: selectedBillingOption.discountText.replace('{percent}', String(pricing.discountPercent)) }))] })), jsx("div", { className: cn('flex items-center gap-2 text-[11px] md:text-xs', !showBillingSubtitle && 'opacity-0 select-none'), "data-price-subtitle": planKey, children: showBillingSubtitle && billingType === 'onetime' ? (
                                                    // OneTime 模式下的特殊处理：普通文本 + 带样式的产品副标题
                                                    jsxs(Fragment, { children: [(selectedBillingOption === null || selectedBillingOption === void 0 ? void 0 : selectedBillingOption.subTitle) && (jsx("span", { className: "text-[11px] md:text-xs text-gray-700 dark:text-gray-300 font-medium", children: selectedBillingOption.subTitle })), plan.subtitle && (jsxs("span", { className: "px-2 py-0.5 text-[11px] md:text-xs rounded bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200 font-semibold align-middle", children: ["+", plan.subtitle] }))] })) : (
                                                    // 其他模式下保持原逻辑
                                                    showBillingSubtitle && (jsx("span", { className: "text-[11px] md:text-xs text-gray-700 dark:text-gray-300 font-medium", children: (selectedBillingOption === null || selectedBillingOption === void 0 ? void 0 : selectedBillingOption.subTitle) || '' }))) })] })] }), jsx("ul", { className: "flex-1 mb-6 mt-4 text-xs md:text-sm leading-5", children: getFeatureRows(plan).map((feature, i) => (jsxs("li", { className: "flex items-start gap-2 mb-2 min-h-[24px] md:min-h-[28px]", "data-feature-item": `${planKey}-${i}`, children: [feature ? (jsx("span", { className: "inline-flex items-center justify-center w-5 h-5 rounded-full bg-green-100 text-green-700 dark:bg-green-900 dark:text-green-200 mr-1", children: feature.icon ? jsx("span", { children: feature.icon }) : jsx("span", { className: "font-bold", children: "\u2713" }) })) : (jsx("span", { className: "inline-flex items-center justify-center w-5 h-5 rounded-full mr-1", children: "\u00A0" })), feature && feature.tag && (jsx("span", { className: "px-1 py-0.5 text-[6px] rounded bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200 font-semibold align-middle", children: feature.tag })), feature ? (jsxs("div", { className: "flex-1 text-gray-800 dark:text-gray-200", children: [jsx("span", { children: feature.description }), feature.tooltip && (jsx("span", { className: "block text-[11px] text-gray-500 dark:text-gray-400 mt-1", children: feature.tooltip }))] })) : (jsx("span", { children: "\u00A0" }))] }, i))) }), jsx("div", { className: "flex-1" }), jsx(MoneyPriceButton, { planKey: planKey, userContext: userContext, billingType: billingType, onAuth: handleAuth, onAction: handleAction, texts: data.buttonTexts, isProcessing: isProcessing, isInitLoading: false, enableSubscriptionUpgrade: enableSubscriptionUpgrade })] }, plan.key));
                    }) }) })] }));
}

export { MoneyPriceInteractive };
