import { isNonNullish } from "@fern-api/ui-core-utils";
import { BaseOpenApiV3_1ConverterNode, } from "../../../BaseOpenApiV3_1Converter.node";
import { resolveResponseReference } from "../../../utils/3.1/resolveResponseReference";
import { isReferenceObject } from "../../guards/isReferenceObject";
import { ParameterBaseObjectConverterNode } from "../parameters/ParameterBaseObjectConverter.node";
import { ResponseMediaTypeObjectConverterNode, } from "./ResponseMediaTypeObjectConverter.node";
export class ResponseObjectConverterNode extends BaseOpenApiV3_1ConverterNode {
    path;
    method;
    statusCode;
    requests;
    shapes;
    headers;
    responses;
    description;
    constructor(args, path, method, statusCode, requests, shapes) {
        super(args);
        this.path = path;
        this.method = method;
        this.statusCode = statusCode;
        this.requests = requests;
        this.shapes = shapes;
        this.safeParse();
    }
    parse(streamingFormat) {
        this.description = this.input.description;
        const input = resolveResponseReference(this.input, this.context.document);
        if (input == null) {
            this.context.errors.error({
                message: isReferenceObject(this.input)
                    ? `Undefined reference: ${this.input.$ref}`
                    : "Expected response, received null",
                path: this.accessPath,
            });
            return;
        }
        Object.entries(input.headers ?? {}).forEach(([headerName, schema]) => {
            this.headers ??= {};
            this.headers[headerName] = new ParameterBaseObjectConverterNode({
                input: schema,
                context: this.context,
                accessPath: this.accessPath,
                pathId: "headers",
                parameterName: headerName,
            });
        });
        if (input.content == null) {
            this.responses ??= [];
            this.responses.push(new ResponseMediaTypeObjectConverterNode({
                input: {},
                context: this.context,
                accessPath: this.accessPath,
                pathId: ["content"],
            }, "empty", streamingFormat, this.path, this.method, this.statusCode, this.requests, this.shapes));
        }
        else {
            Object.entries(input.content ?? {}).forEach(([contentType, contentTypeObject]) => {
                this.responses ??= [];
                this.responses.push(new ResponseMediaTypeObjectConverterNode({
                    input: contentTypeObject,
                    context: this.context,
                    accessPath: this.accessPath,
                    pathId: ["content", contentType],
                }, contentType, streamingFormat, this.path, this.method, this.statusCode, this.requests, 
                // TODO: add response headers, but this needs to be added to FDR shape
                this.shapes));
            });
        }
    }
    convert() {
        return this.responses
            ?.flatMap((response) => response.convert())
            .filter(isNonNullish);
    }
}
