import { HttpProject } from '../models/HttpProject.js';
import { ApiError } from '../runtime/store/Errors.js';
import { StoreSdk } from '../runtime/store/StoreSdkNode.js';
import { ProjectSerialRunner } from '../runtime/node/ProjectSerialRunner.js';
import { ProjectParallelRunner } from '../runtime/node/ProjectParallelRunner.js';
import Proxy from './Proxy.js';
/**
 * Runs requests from an HTTP project read from the store.
 */
export default class HttpProjectProxy extends Proxy {
    project;
    options;
    async configureStore(init, token, baseUri) {
        const { pid, options, organization } = init;
        if (!pid) {
            throw new ApiError({
                error: true,
                message: 'Invalid request',
                detail: 'The "pid" parameter is required.',
                code: 400,
            });
        }
        if (!organization) {
            throw new ApiError({
                error: true,
                message: 'Invalid request',
                detail: 'The "organization" parameter is required.',
                code: 400,
            });
        }
        if (!options) {
            throw new ApiError({
                error: true,
                message: 'Invalid request',
                detail: 'The "options" parameter is required.',
                code: 400,
            });
        }
        if (!token) {
            throw new ApiError({
                error: true,
                message: 'Invalid request',
                detail: 'Set the authentication credentials.',
                code: 400,
            });
        }
        if (!baseUri) {
            throw new ApiError({
                error: true,
                message: 'Invalid request',
                detail: 'The store uri is missing.',
                code: 400,
            });
        }
        if (!baseUri.startsWith('http')) {
            throw new ApiError({
                error: true,
                message: 'Invalid request',
                detail: 'The store uri is invalid.',
                code: 400,
            });
        }
        const sdk = new StoreSdk(baseUri);
        sdk.token = token;
        let project;
        try {
            const data = await sdk.file.readMedia(organization, pid);
            project = data.media;
        }
        catch (cause) {
            const e = cause;
            throw new ApiError(e.message, 400);
        }
        if (project.key !== pid) {
            throw new ApiError(`Unable to read the project.`, 500);
        }
        this.options = options;
        this.project = new HttpProject(project);
    }
    async configureProject(init) {
        const { project, options } = init;
        if (!options) {
            throw new ApiError({
                error: true,
                message: 'Invalid request',
                detail: 'The "options" parameter is required.',
                code: 400,
            });
        }
        if (!project) {
            throw new ApiError({
                error: true,
                message: 'Invalid request',
                detail: 'The "project" parameter is required.',
                code: 400,
            });
        }
        this.options = options;
        this.project = new HttpProject(project);
    }
    async configure(init, token, baseUri) {
        const typedStore = init;
        if (typeof typedStore.pid === 'string') {
            return this.configureStore(typedStore, token, baseUri);
        }
        const typedProject = init;
        return this.configureProject(typedProject);
    }
    async execute() {
        const project = this.project;
        const opts = this.options;
        let factory;
        if (opts.parallel) {
            factory = new ProjectParallelRunner(project, opts);
        }
        else {
            factory = new ProjectSerialRunner();
            factory.configure(project, opts);
        }
        function unhandledRejection() {
            //
        }
        // the executing library handles all related errors it need.
        // However, when executing a request to an unknown host Node process may
        // throw unhandled error event when the error is properly reported by the
        // application. This suppresses these errors.
        // Note, uncomment this line for debug.
        process.on('unhandledRejection', unhandledRejection);
        const report = await factory.execute();
        process.off('unhandledRejection', unhandledRejection);
        return {
            result: report,
        };
    }
}
//# sourceMappingURL=HttpProjectProxy.js.map