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

import { Injectable, NgModuleRef } from "@angular/core";
import { COMPONENT_SCOPE_PREFIX } from "../../../../lib-interfaces";
import { ExtensionPointManifest } from "../extensibility.model";
import { ExtensionComponentRegistration, ExtensionComponentRegistry } from "../extension-component.registry";
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 to be injected in create ip bindings wizard/modal
 * (i.e. with <ng-container vcdExtensionComponents scope="...">)
 */
export const CREATE_IP_BINDINGS_COMPONENT_SCOPE = `${COMPONENT_SCOPE_PREFIX}:create-ip-bindings`;

/**
 * Dashboard manifest type (constant value)
 */
export const CREATE_IP_BINDINGS_TYPE = "create-ip-bindings";

/**
 * Dashboard manifest type (type value)
 */
export type CREATE_IP_BINDINGS_TYPE = "create-ip-bindings";

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

/**
 * Extends create ip bindings wizard/modal by injecting component(s) inside.
 */
export interface CreateIpBindingsExtensionPointManifest extends ExtensionPointManifest {
    readonly type: "create-ip-bindings";
    readonly component: string;
}

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

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

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

        if (
            !extensionPointManifest.render ||
            (!extensionPointManifest.render.before && !extensionPointManifest.render.after)
        ) {
            throw new Error(
                `Create Ip Bindings component '${extensionPointManifest.component}' ` +
                "doesn't have render property defined"
            );
        }

        const registration = new ExtensionComponentRegistration(
            CREATE_IP_BINDINGS_COMPONENT_SCOPE,
            extensionAngularModule,
            null,
            null,
            [],
            ordering,
            extensionPointManifest,
            state.manifest.locales,
            null,
            getHashedString(state.manifest.vendor)
        );
        this.extensionComponentRegistry.registerExtensionComponent(registration);
    }
}
