import path from 'path';
import { ConfigLoader } from "./core/impl/config-loader.impl";
import { AbstractTransformerOrchestrator } from "./core/impl/transformer-orchestrator.abstract.impl";
import { IConfigLoader } from "./core/models/config-loader.interface";
import { IConfigRegistry, ITransformerRegistry } from "./core/models/registry.interface";
import { ITransformer } from "./core/models/transformer.interface";
import { StudioAsset, WrappedAsset } from "./core/models/zip-processor.interface";
/**
 * Core implementation of the Transform Orchestrator
 * Provides basic transformation capabilities with minimal configuration
 */
export class CoreTransformOrchestrator extends AbstractTransformerOrchestrator {
    /**
     * Create a new core transform orchestrator
     * @param configRegistry Configuration registry
     * @param transformerRegistry Transformer registry
     * @param targetVersion Target gateway version
     */
    constructor(
        configRegistry: IConfigRegistry,
        transformerRegistry: ITransformerRegistry,
        configLoader: IConfigLoader,
        targetVersion: string = '10.1.0'
    ) {
        super(configRegistry, transformerRegistry, configLoader, targetVersion);
    }

    /**
     * Check if an API and its assets are valid for transformation
     * Core implementation always returns true
     * @param api The API asset
     * @param relatedAssets Related assets
     * @returns True if the API is valid for transformation
     */
    protected isApiValid(api: StudioAsset, relatedAssets: StudioAsset[]): boolean {
        return true;
    }

    /**
     * Filter assets to include only those needed for the target gateway
     * Core implementation includes all assets by default
     * @param assets Array of assets to filter
     * @returns Filtered array of assets
     */
    public filterAssets(assets: StudioAsset[]): StudioAsset[] {
        return assets;
    }

    /**
     * Post-transformation hook that runs after all assets have been transformed
     * Core implementation extracts the outputAsset from each wrapped asset
     * @param transformedAssets Array of wrapped transformed assets
     * @param resources Resources extracted from the ZIP file
     * @param apiName Name of the API being processed
     * @param apiMetadata Metadata of the API
     * @returns Final processed output
     */
    protected async postTransform(
        allAssets: StudioAsset[],
        transformedAssets: WrappedAsset[],
        resources: Record<string, string>,
        apiName: string,
        apiMetadata: any
    ): Promise<any> {
        return transformedAssets
            .filter(asset => asset.outputAsset)
            .map(asset => asset.outputAsset);
    }

    /**
     * Pre-transformation hook that runs before individual asset transformations
     * Core implementation simply wraps each asset with its metadata
     * @param inputAssets Array of assets extracted from ZIP
     * @returns Processed assets ready for transformation
     */
    protected async preTransform(
        inputAssets: StudioAsset[],
    ): Promise<WrappedAsset[]> {
        return inputAssets.map(asset => ({
            inputSchema: asset,
            metadata: asset.metadata
        }));
    }
}

/**
 * Factory function to create a core transform orchestrator with default registries
 * @returns A new core transform orchestrator
 */
export function createCoreOrchestrator(): CoreTransformOrchestrator {
    const configLoader = new ConfigLoader(path.resolve(__dirname, '../src'));

    const configRegistry: IConfigRegistry = {
        getConfigPath: (_sourceVersion: string, _targetVersion: string, _kind: string): string | undefined => {
            return '/configs/skip-transform.json';
        }
    };

    const transformerRegistry: ITransformerRegistry = {
        getTransformer: (_name: string): ITransformer | undefined => {
            return undefined;
        }
    };

    return new CoreTransformOrchestrator(
        configRegistry,
        transformerRegistry,
        configLoader
    );
}