/*
 * 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 { 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 vapp wizard/modal
 * (i.e. with <ng-container vcdExtensionComponents scope="...">)
 */
export const CREATE_VAPP_COMPONENT_SCOPE = `${COMPONENT_SCOPE_PREFIX}:create-vapp`;

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

export const CREATE_VAPP_OVF_TYPE = "create-vapp-ovf";
export type CREATE_VAPP_OVF_TYPE = "create-vapp-ovf";

export const CREATE_VAPP_TEMPLATE_TYPE = "create-vapp-template";
export type CREATE_VAPP_TEMPLATE_TYPE = "create-vapp-template";

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

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

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

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

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

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

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

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

        const registration = new ExtensionComponentRegistration(
            CREATE_VAPP_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);
    }
}
