/*
 * 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 { APPLICATIONS_PATH } from "../../../extensions/tenant-routing-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 { COMPONENT_SCOPE_PREFIX } from "../../../../lib-interfaces";

/**
 * This scope is intended to be injected in the appliations
 * (i.e. with <ng-container vcdExtensionComponents scope="...">)
 */
export const APPLICATIONS_COMPONENT_SCOPE = `${COMPONENT_SCOPE_PREFIX}:applications`;

/**
 * Applications manifest type (constant value)
 */
export const APPLICATIONS_TYPE = "navigation:applications";

/**
 * Applications manifest type (type value)
 */
export type APPLICATIONS_TYPE = "navigation:applications";

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

/**
 * Applications Section extension point manifests have the type of "applications".
 * They declare a single component, which should be injected onto the applications screen.
 */
export interface ApplicationsExtensionPointManifest extends ExtensionPointManifest {
    readonly type: APPLICATIONS_TYPE;
    readonly component: string;
    readonly module?: string;
    readonly route?: string;
}

/**
 * This extension point handler supports extension manifests of version 9.5.0 and of type
 * "applications".  It will register the defined component with the ExtensionComponentRegistry
 * under the scope of "urn:vmware:vcd:component-scope:applications.  This scope is intended to be
 * injected in the Applications (i.e. with <ng-container vcdExtensionComponents scope="...">)
 */
@Injectable()
export class ApplicationsExtensionPointHandler implements ExtensionPointHandler {
    constructor(private extensionComponentRegistry: ExtensionComponentRegistry,
                private routingHandler: ExtensionPointRoutingHandler) {
    }

    /**
     * Supports version 9.5.0 and type "applications.
     */
    supports(version: string, type: string): boolean {
        return SUPPORTED_VERSIONS.indexOf(version) != -1 && type == APPLICATIONS_TYPE;
    }

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

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

        if (extensionPointManifest.module) {
            if (!vcdRoute) {
                throw new Error(`Applications component '${extensionPointManifest.component}'
                defined a module but no route was found.`);
            }

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

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

        this.extensionComponentRegistry.registerExtensionComponent(registration);
    }
}
