
import type { PlatformAPI } from "../types";
import bridge from "@vkontakte/vk-bridge";

export function createVkAPI(): PlatformAPI {
    // Инициализация
    bridge.send("VKWebAppInit");

    let backHandler: (() => void) | null = null;

    // Схема: в VK есть VKWebAppUpdateConfig с appearance
    let colorScheme: "light" | "dark" = "light";
    bridge.subscribe((e) => {
        if (e.detail?.type === "VKWebAppUpdateConfig") {
            const appearance = e.detail.data?.appearance;
            colorScheme = appearance === "dark" ? "dark" : "light";
        }
        // @ts-ignore
        if (e.detail?.type === "VKWebAppBack") {
            backHandler?.();
        }
    });

    return {
        platform: "vk",
        get colorScheme() { return colorScheme; },
        safeAreaInsets: { top: 0, bottom: 0, left: 0, right: 0 },

        haptics: {
            impact: async () => { /* VK: вибрация зависит от платформы; можно добавить позже */ },
            notify: async () => {}
        },

        navigation: {
            setBackHandler: (cb) => { backHandler = cb; }
        },

        mainButton: {
            show: async () => {},
            hide: async () => {},
            setLoading: async () => {}
        }
    } as PlatformAPI;
}
