import { UnreachableCaseError } from "ts-essentials";
import { BaseOpenApiV3_1ConverterNodeWithExample, } from "../../../BaseOpenApiV3_1Converter.node";
import { OPENAPI_NUMBER_TYPE_FORMAT, } from "../../../types/format.types";
function isOpenApiNumberTypeFormat(format) {
    return OPENAPI_NUMBER_TYPE_FORMAT.includes(format);
}
export class NumberConverterNode extends BaseOpenApiV3_1ConverterNodeWithExample {
    format;
    minimum;
    maximum;
    default;
    constructor(args) {
        super(args);
        this.safeParse();
    }
    parse() {
        this.minimum = this.input.minimum;
        this.maximum = this.input.maximum;
        if (this.input.default != null && typeof this.input.default !== "number") {
            this.context.errors.warning({
                message: `Expected default value to be a number. Received ${this.input.default}`,
                path: this.accessPath,
            });
        }
        this.default = this.input.default;
        if (this.input.format != null) {
            if (!isOpenApiNumberTypeFormat(this.input.format)) {
                this.context.errors.warning({
                    message: `Expected format to be one of ${OPENAPI_NUMBER_TYPE_FORMAT.join(", ")}. Received ${this.input.format}`,
                    path: this.accessPath,
                });
            }
            else {
                this.format = this.input.format;
            }
        }
    }
    convert() {
        let type = "double";
        if (this.format != null) {
            switch (this.format) {
                case "decimal":
                case "decimal128":
                case "double-int":
                case "double":
                case "float":
                case "sf-decimal":
                case undefined:
                    type = "double";
                    break;
                default:
                    new UnreachableCaseError(this.format);
                    break;
            }
        }
        return {
            type: "alias",
            value: {
                type: "primitive",
                value: {
                    type,
                    minimum: this.minimum,
                    maximum: this.maximum,
                    default: this.default,
                },
            },
        };
    }
    example() {
        return this.input.example ?? this.input.examples?.[0] ?? this.default ?? 0;
    }
}
