import React, { useEffect, useState } from "react";
import { C3AppTeaser } from "./c3-app-teaser";
import { canCreateCluster, canUpgradePlan, isTrialExpired, } from "../../utils/camunda";
import { useC3Profile } from "../c3-user-configuration/c3-profile-provider/c3-profile-provider";
import { Loading } from "@carbon/react";
import { getStartingPrice } from "../../api/organizations";
import { useC3UserConfiguration } from "../c3-user-configuration/c3-user-configuration-provider";
export const C3AppTeaserPage = ({ appName, redirectToClusters, redirectToCreateCluster, redirectToCheckout, }) => {
    const { activeOrganizationId, decodedAudience, domain, analyticsTrack, userToken, } = useC3UserConfiguration();
    const { activeOrg, clusters } = useC3Profile();
    const [pricing, setPricing] = useState(null);
    const [hasTrialExpired, setHasTrialExpired] = useState(false);
    const [hasSleepingCluster, setHasSleepingCluster] = useState(false);
    useEffect(() => {
        if (!userToken || !decodedAudience)
            return;
        (async () => {
            const { result } = await getStartingPrice({
                token: userToken,
                audience: decodedAudience,
            });
            if (result) {
                setPricing(result);
            }
        })();
    }, [userToken, decodedAudience]);
    useEffect(() => {
        if (activeOrg)
            setHasTrialExpired(isTrialExpired(activeOrg));
    }, [activeOrg]);
    useEffect(() => {
        if (clusters && appName) {
            const sleepingCluster = clusters.find((c) => c.status[appName] === "Suspended");
            setHasSleepingCluster(!!sleepingCluster);
        }
    }, [clusters, appName]);
    const onClickCta = () => {
        if (hasTrialExpired) {
            analyticsTrack?.("checkout:open", { appTeaser: appName });
            if (redirectToCheckout) {
                redirectToCheckout();
            }
            else {
                window.location.href = `https://console.${domain}/org/${activeOrganizationId}/checkout`;
            }
        }
        else if (hasSleepingCluster) {
            analyticsTrack?.("clusters:manage:open", { appTeaser: appName });
            if (redirectToClusters) {
                redirectToClusters();
            }
            else {
                window.location.href = `https://console.${domain}/org/${activeOrganizationId}/clusters`;
            }
        }
        else {
            analyticsTrack?.("clusters:create:open", { appTeaser: appName });
            if (redirectToCreateCluster) {
                redirectToCreateCluster();
            }
            else {
                window.location.href = `https://console.${domain}/org/${activeOrganizationId}/clusters/create`;
            }
        }
    };
    return activeOrg && appName ? (React.createElement(C3AppTeaser, { appName: appName, canCreateCluster: canCreateCluster(activeOrg), canUpgradePlan: canUpgradePlan(activeOrg), hasTrialExpired: hasTrialExpired, hasSleepingCluster: hasSleepingCluster, onClickCta: onClickCta, pricing: pricing })) : (React.createElement(Loading, { withOverlay: true }));
};
