/*
 * Copyright 2018-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 { VDC } from "../../../extensions/routing-paths/vdcs-route-paths";
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";
import { DATACENTER_OVERVIEW_COMPONENT_SCOPE } from "./datacenter-overview.extension-point.constants";

/**
 * Dashboard manifest type (constant value)
 */
export const DATACENTER_OVERVIEW_TYPES = ["datacenter-overview", "navigation:datacenter:overview"];

/**
 * Dashboard manifest type (type value)
 */
export type DATACENTER_OVERVIEW_TYPES = ["datacenter-overview", "navigation:datacenter:overview"];

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

/**
 * Dashboard Section extension point manifests have the type of "datacenter-overview".
 * They declare a single component, which should be injected onto the dashboard screen.
 */
export interface DatacenterOverviewExtensionPointManifest extends ExtensionPointManifest {
    readonly type: "datacenter-overview" | "navigation:datacenter:overview";
    readonly component: string;
}

/**
 * This extension point handler supports extension manifests of version 9.5.0 and of type
 * "datacenter-overview".  It will register the defined component with the ExtensionComponentRegistry
 * under the scope of "urn:vmware:vcd:component-scope:datacenter-overview".  This scope is intended to be
 * injected in the dashboard (i.e. with <ng-container vcdExtensionComponents scope="...">)
 */
@Injectable()
export class DatacenterOverviewExtensionPointHandler implements ExtensionPointHandler {

    constructor(private extensionComponentRegistry: ExtensionComponentRegistry,
                private routingHandler: ExtensionPointRoutingHandler) {
    }

    /**
     * Supports version 9.5.0 and type "datacenter-overview" or "navigation:datacenter:overview".
     */
    supports(version: string, type: string): boolean {
        return SUPPORTED_VERSIONS.indexOf(version) != -1 && DATACENTER_OVERVIEW_TYPES.indexOf(type) != -1;
    }

    /**
     * Register the defined component with the ExtensionComponentRegistry under the scope of
     * "urn:vmware:vcd:component-scope:datacenter-overview".
     */
    bootstrap(
        extensionBundle: any,
        extensionAngularModule: NgModuleRef<any>,
        extensionPointManifest: DatacenterOverviewExtensionPointManifest,
        ordering: number,
        state: ExternalExtensionLoaderState
    ): void {
        if (!extensionPointManifest.component) {
            throw new Error("Datacenter Overview component not defined in manifest");
        }

        const vcdRoute = state.routes[`${state.spec.id}:${extensionPointManifest.urn}`];
        if (extensionPointManifest.module) {
            if (!vcdRoute) {
                throw new Error(`Datacenter Overview component '${extensionPointManifest.component}'
                defined a module but no route was found.`);
            }

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

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

        this.extensionComponentRegistry.registerExtensionComponent(registration);
    }
}
