/*
 * Copyright 2020-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 { WizardExtensionComponent } from "../extension-point-orchestrator/extension-point-container.interface";
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 the create vm wizard/modal
 * (i.e. with <ng-container vcdExtensionComponents scope="...">)
 */
export const CREATE_VM_COMPONENT_SCOPE = `${COMPONENT_SCOPE_PREFIX}:create-vm`;

/**
 * Dashboard manifest type (constant value)
 */
export const CREATE_VM_TYPE = "create-vm";

/**
 * Dashboard manifest type (type value)
 */
export type CREATE_VM_TYPE = "create-vm";

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

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

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

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

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

        // const component = extensionBundle[extensionPointManifest.component];
        // if (!component) {
        //     throw new Error(
        //         `Create VM component '${extensionPointManifest.component}' ` + "not exported by extension bundle"
        //     );
        // }

        // if (!Object.prototype.isPrototypeOf.call(WizardExtensionComponent.prototype, component.prototype)) {
        //     throw new Error(
        //         `Create VM component '${extensionPointManifest.component}' ` +
        //             "not inheriting from WizardExtensionPointComponent base class"
        //     );
        // }

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

        const registration = new ExtensionComponentRegistration(
            CREATE_VM_COMPONENT_SCOPE,
            extensionAngularModule,
            // state.angularModuleFactories[`${state.spec.id}:${extensionPointManifest.urn}`],
            null,
            // component,
            null,
            [],
            ordering,
            extensionPointManifest,
            state.manifest.locales,
            null,
            getHashedString(state.manifest.vendor)
        );
        this.extensionComponentRegistry.registerExtensionComponent(registration);
    }
}
