/*
Copyright 2025 New Vector Ltd.

SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/

import { ThemeProvider } from "styled-components";
import compound from "@vector-im/compound-web/dist/style.css" with { type: "css" };

import type { Module, Api, ModuleFactory } from "@element-hq/element-web-module-api";
import Translations from "./translations.json";
import { ModuleConfig, CONFIG_KEY } from "./config";
import Banner from "./Banner";
import { name as ModuleName } from "../package.json";
import style from "./style.css" with { type: "css" };

class BannerModule implements Module {
    public static readonly moduleApiVersion = "^1.0.0";

    private config?: ModuleConfig;

    public constructor(private api: Api) {}

    public async load(): Promise<void> {
        const rawConfig = this.api.config.get(CONFIG_KEY);
        if (!rawConfig) {
            console.debug(`No configuration found for module "${ModuleName}", skipping initialization.`);
            return;
        }

        document.adoptedStyleSheets.push(compound);
        document.adoptedStyleSheets.push(style);

        this.api.i18n.register(Translations);

        try {
            this.config = ModuleConfig.parse(rawConfig);
        } catch (e) {
            console.error("Failed to init module", e);
            throw new Error(`Errors in module configuration for "${ModuleName}"`);
        }

        const div = document.createElement("div");
        div.dataset.testid = "banner";
        this.api.rootNode.before(div);

        const root = this.api.createRoot(div);
        root.render(
            <ThemeProvider theme={this.config.theme}>
                <Banner
                    api={this.api}
                    logoUrl={this.config.logo_url}
                    href={this.config.logo_link_url}
                    menu={this.config.menu}
                    title={this.config.title ?? this.api.config.get("brand")}
                />
            </ThemeProvider>,
        );
    }
}

export default BannerModule satisfies ModuleFactory;
