/*
 * Copyright 2023-2025 VMware, Inc. All rights reserved. VMware Confidential
 */

import { Injectable, NgModuleRef } from "@angular/core";
import { RESOURCES_ROUTE } from "../../../app/plugin-container/plugin-container.module";
import { DOMAIN_NETWORKING } from "../../../extensions/routing-paths/vdcs-route-paths";
import { COMPONENT_SCOPE_PREFIX } from "../../../../lib-interfaces";
import { ExtensionPointManifest } from "../extensibility.model";
import { ExtensionComponentRegistration, ExtensionComponentRegistry } from "../extension-component.registry";
import { ExtensionPointRoutingHandler } from "../extension-point-routing.handler";
import { ExtensionPointHandler } from "../extension-point.handler";
import { ExternalExtensionLoaderState } from "../loader/extension-loader.model";
import { getHashedString } from "../loader/external/create-external-extension-route.step";

/**
 * This scope is intended for injection in the tenant networking sub navigation.
 */
export const NETWORKING_COMPONENT_SCOPE = `${COMPONENT_SCOPE_PREFIX}:networking`;

/**
 * Networking extension type for the manifest. (Constant)
 */
export const NETWORKING_TYPE = "navigation:networking";

/**
 * Networking extension type for the manifest. (Type)
 */
export type NETWORKING_TYPE = "navigation:networking";

/**
 * The manifest versions this is supported by.
 */
export const SUPPORTED_VERSIONS: string[] = ["9.5.0"];

/**
 * Networking extension points in the manifest have the type of "urn:vmware:vcd:component-scope:networking".
 * They declare a single component or a full Angular module, which should be injected onto networking sub navigation.
 */
export interface NetworkingExtensionPointManifest extends ExtensionPointManifest {
    readonly type: NETWORKING_TYPE;
    readonly component: string;
    readonly module?: string;
    readonly route?: string;
}

/**
 * Handler for the Networking extension points.
 * It will register the plugin modules directly in the main navigation through the plugins manager service.
 */
@Injectable()
export class NetworkingExtensionPointHandler implements ExtensionPointHandler {
    constructor(
        private extensionComponentRegistry: ExtensionComponentRegistry,
        private routingHandler: ExtensionPointRoutingHandler
    ) {
    }

    supports(version: string, type: string): boolean {
        return SUPPORTED_VERSIONS.indexOf(version) != -1 && type == NETWORKING_TYPE;
    }

    bootstrap(
        extensionBundle: any,
        extensionAngularModule: NgModuleRef<any>,
        extensionPointManifest: NetworkingExtensionPointManifest,
        ordering: number,
        state: ExternalExtensionLoaderState
    ): void {
        if (!extensionPointManifest.component) {
            throw new Error("Networking component not defined in manifest!");
        }

        const vcdRoute = state.routes[`${state.spec.id}:${extensionPointManifest.urn}`];

        if (extensionPointManifest.module) {
            if (!vcdRoute) {
                throw new Error(`Navigation extension '${extensionPointManifest.component}' 
                defines a module, but no matching route was found for the registration.`);
            }

            this.routingHandler.registerExtensionRouting(
                this.routingHandler.findRoute(DOMAIN_NETWORKING, [RESOURCES_ROUTE]),
                state.manifest.vendor, vcdRoute, extensionPointManifest.name);
        }

        const component = null;

        const registration = new ExtensionComponentRegistration(
            NETWORKING_COMPONENT_SCOPE,
            extensionAngularModule,
            null,
            component,
            [],
            ordering,
            extensionPointManifest,
            state.manifest.locales,
            extensionPointManifest.module ? vcdRoute : null,
            getHashedString(state.manifest.vendor)
        );

        this.extensionComponentRegistry.registerExtensionComponent(registration);
    }
}
