import { UnreachableCaseError } from "ts-essentials";
import { BaseOpenApiV3_1ConverterNodeWithExample, } from "../../../BaseOpenApiV3_1Converter.node";
import { OPENAPI_INTEGER_TYPE_FORMAT, } from "../../../types/format.types";
function isOpenApiIntegerTypeFormat(format) {
    return OPENAPI_INTEGER_TYPE_FORMAT.includes(format);
}
export class IntegerConverterNode extends BaseOpenApiV3_1ConverterNodeWithExample {
    format;
    minimum;
    maximum;
    default;
    nullable;
    constructor(args) {
        super(args);
        this.nullable = args.nullable;
        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 an integer. Received ${this.input.default}`,
                path: this.accessPath,
            });
        }
        this.default = this.input.default;
        if (this.input.format != null) {
            if (!isOpenApiIntegerTypeFormat(this.input.format)) {
                this.context.errors.warning({
                    message: `Expected format to be one of ${OPENAPI_INTEGER_TYPE_FORMAT.join(", ")}. Received ${this.input.format}`,
                    path: this.accessPath,
                });
            }
            else {
                this.format = this.input.format;
            }
        }
    }
    convert() {
        let type = "integer";
        if (this.format != null) {
            switch (this.format) {
                case "int64":
                    type = "long";
                    break;
                case "int8":
                case "int16":
                case "int32":
                case "uint8":
                case "sf-integer":
                case undefined:
                    type = "integer";
                    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 ??
            (this.nullable ? null : 0));
    }
}
