"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiError = void 0;
exports.ok = ok;
exports.created = created;
exports.errorMiddleware = errorMiddleware;
class ApiError extends Error {
    code;
    status;
    details;
    constructor(code, message, status = 400, details) {
        super(message);
        this.code = code;
        this.status = status;
        this.details = details;
    }
}
exports.ApiError = ApiError;
function ok(res, payload) {
    return res.status(200).json(payload);
}
function created(res, payload) {
    return res.status(201).json(payload);
}
function errorMiddleware(err, _req, res, _next) {
    if (err instanceof ApiError) {
        res.status(err.status).json({
            ok: false,
            error: {
                code: err.code,
                message: err.message,
                details: err.details ?? null
            }
        });
        return;
    }
    const fallback = err instanceof Error ? err.message : "Unknown error";
    res.status(500).json({
        ok: false,
        error: {
            code: "INTERNAL_ERROR",
            message: fallback,
            details: null
        }
    });
}
