/**
 * Register Pro UI extensions from the main bundle (shared stores/registry).
 */

import React from "react";
import type { ComponentType } from "react";

import { AiSettingsSection } from "@pro/components/Settings/AiSettingsSection";
import { CountUpAnimationSettings } from "@pro/components/Settings/CountUpAnimationSettings";
import { AiHelperUpgradeButton } from "@/components/Toolbar/AiHelperUpgradeButton";
import { registerPriceAnimationOption } from "@/animations/registry";
import { registerExtension } from "@/extensions/registry";
import { useActiveScenario } from "@/stores/canvasStore";
import type { PriceScenario } from "@/types";
import { isPro } from "@/utils/license";

let registered = false;

function createAiHelperSlot(AiHelper: ComponentType<{ scenario: PriceScenario }>) {
  return function AiHelperSlot() {
    const scenario = useActiveScenario();
    return React.createElement(AiHelper, { scenario });
  };
}

/**
 * Register Pro extensions (UI slots and optional features).
 */
export function registerProExtensions(): void {
  if (registered) {
    return;
  }

  registered = true;
  registerExtension("settings-ai-section", AiSettingsSection);

  if (isPro()) {
    void import("@pro/components/Canvas/AiHelperButton").then(({ AiHelperButton }) => {
      registerExtension("canvas-toolbar", createAiHelperSlot(AiHelperButton));
    });
    registerPriceAnimationOption({ value: "countUp", label: "Count Up ⭐" });
    registerExtension("animation-countup-settings", CountUpAnimationSettings);
    return;
  }

  registerExtension("canvas-toolbar", AiHelperUpgradeButton);
}
