import type { App } from 'vue';
import Toast from 'vue-toastification';
import 'vue-toastification/dist/index.css';
import formatter from '@/UI-kit/plugins/formatter';
import pluralEndings from '@/UI-kit/plugins/plural-endings';
import Maska from 'maska';
import { autoAnimatePlugin } from '@formkit/auto-animate/vue';
import axios from '@/UI-kit/plugins/axios';
import { uiKitI18n } from '@/configs';

export default {
    install(
        app: App,
        options: {
            baseURL: string;
            i18n?: any; // Optional host i18n instance
        },
    ) {
        app.use(autoAnimatePlugin);
        app.use(Maska);
        app.use(Toast);

        // Merge uiKitI18n messages into host i18n if provided
        if (options.i18n?.global) {
            const hostGlobal = options.i18n.global;
            const uiGlobal = uiKitI18n.global;

            // Merge UI kit messages into host i18n for each locale
            ['ru', 'kg', 'tj', 'uz'].forEach((locale) => {
                const hostMessages = hostGlobal.getLocaleMessage(locale) || {};
                const uiMessages = uiGlobal.getLocaleMessage(locale) || {};

                hostGlobal.setLocaleMessage(locale, {
                    ...uiMessages,
                    ...hostMessages,
                });
            });
        } else {
            // Host app doesn't have i18n, install ours
            console.log('[UIKitInstall] Installing UI kit i18n as standalone');
            app.use(uiKitI18n);
        }

        app.use(formatter);
        app.use(pluralEndings);
        app.use(axios, options);
    },
};
