import { useContext, useEffect, useState } from "react";
import {
	type BungeeConfig,
	WidgetConfigContext,
} from "../Providers/ConfigProvider";

interface ExtendedBungeeConfig extends BungeeConfig {
	/** To toggle multi-output */
	enableMultiOutput: boolean;

	/** Max number of tokens supported for multi output */
	multiOutputCap: number;

	// temporary
	manualBaseUrl: string;
	manualApiKey: string;
}

const defaultValues: Partial<ExtendedBungeeConfig> = {
	baseUrl: "https://backend.bungee.exchange",
	theme: {
		colors: {
			success: "#8AE8B6", // @fixme: will this be a problem?
		},
	},
	enableMultiOutput: true,
	multiOutputCap: 2,
	features: {
		internalToasts: true,
	},

	// temporary
	manualBaseUrl: "https://api.socket.tech/v2",
	manualApiKey: "1b2fd225-062f-41aa-8c63-d1fef19945e7",
};

export const useConfig = (): Partial<ExtendedBungeeConfig> => {
	const configContext = useContext(WidgetConfigContext);
	const [widgetConfig, setWidgetConfig] =
		useState<Partial<BungeeConfig>>(defaultValues);
	useEffect(() => {
		const config = {
			...defaultValues,
			...configContext,
		};

		setWidgetConfig(config);
	}, [configContext]);

	return widgetConfig;
};
