import { isNonNullish } from "@fern-api/ui-core-utils";
import { BaseOpenApiV3_1ConverterNodeWithExample, } from "../../../BaseOpenApiV3_1Converter.node";
import { convertToObjectProperties } from "../../../utils/3.1/convertToObjectProperties";
import { resolveParameterBaseReference } from "../../../utils/3.1/resolveParameterBaseReference";
import { AvailabilityConverterNode } from "../../extensions/AvailabilityConverter.node";
import { isReferenceObject } from "../../guards/isReferenceObject";
import { SchemaConverterNode } from "../../schemas/SchemaConverter.node";
export function convertOperationObjectProperties(properties) {
    if (properties == null) {
        return undefined;
    }
    return convertToObjectProperties(properties, Object.entries(properties ?? {})
        .map(([key, property]) => (property.required ? key : undefined))
        .filter(isNonNullish));
}
export class ParameterBaseObjectConverterNode extends BaseOpenApiV3_1ConverterNodeWithExample {
    availability;
    required;
    schema;
    description;
    inputExample;
    constructor(args) {
        super(args);
        this.safeParse(args.parameterName);
    }
    parse(parameterName) {
        this.description = this.input.description;
        let schema;
        if (isReferenceObject(this.input)) {
            schema = this.input;
        }
        else {
            if (this.input.schema != null) {
                schema = this.input.schema;
                this.required = this.input.required;
            }
            else {
                schema = {
                    type: "string",
                };
                this.required = false;
            }
        }
        this.availability = new AvailabilityConverterNode({
            input: schema,
            context: this.context,
            accessPath: this.accessPath,
            pathId: "availability",
        });
        this.schema = new SchemaConverterNode({
            input: schema,
            context: this.context,
            accessPath: this.accessPath,
            pathId: "schema",
            seenSchemas: new Set(),
            nullable: undefined,
            schemaName: parameterName,
        });
        // TODO: support multiple examples
        const input = resolveParameterBaseReference(this.input, this.context.document);
        this.inputExample =
            input?.example ?? Object.values(input?.examples ?? {})[0];
    }
    convert() {
        return this.schema?.convert();
    }
    example(exampleArgs) {
        return this.inputExample ?? this.schema?.example(exampleArgs);
    }
}
