/**
 * This file has been claimed for ownership from @keycloakify/keycloak-admin-ui version 260700.0.2.
 * To relinquish ownership and restore this file to its original content, run the following command:
 *
 * $ npx keycloakify own --path "admin/KcAdminUi.tsx" --revert
 */

import "@patternfly/patternfly/patternfly-addons.css";
import "@patternfly/react-core/dist/styles/base.css";
import { useEffect, useReducer } from "react";
import { startColorSchemeManagement } from "./colorScheme";
import { createHashRouter, RouterProvider } from "react-router-dom";
import { i18n } from "./i18n/i18n";
import { Root } from "./Root";
import { routes } from "./routes";
import { getKcContext } from "./KcContext";

import "./index.css";
import "../console-ui.css";
import "./workspace.css";

const { kcContext } = getKcContext();

document.documentElement.dataset.kcThemeName = kcContext.themeName;

const router = createHashRouter([
    {
        path: "/",
        element: <Root />,
        children: routes
    }
]);
const prI18nInitialized = i18n.init();
startColorSchemeManagement();

export default function KcAdminUi() {
    const [isI18nInitialized, setI18nInitialized] = useReducer(() => true, false);

    useEffect(() => {
        prI18nInitialized.then(() => setI18nInitialized());
    }, []);

    useEffect(() => {
        if (!isI18nInitialized) {
            return;
        }

        const updateDocumentTitle = () => {
            document.title = i18n.t("nexusAdminConsoleTitle");
        };

        updateDocumentTitle();
        i18n.on("languageChanged", updateDocumentTitle);

        return () => {
            i18n.off("languageChanged", updateDocumentTitle);
        };
    }, [isI18nInitialized]);

    if (!isI18nInitialized) {
        return null;
    }

    return <RouterProvider router={router} />;
}
