"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var get_mediatype_examples_1 = __importDefault(require("../lib/get-mediatype-examples"));
var rmoas_types_1 = require("../rmoas.types");
/**
 * Retrieve a collection of response examples keyed, by their media type.
 *
 * @param operation Operation to retrieve response examples for.
 */
function getResponseExamples(operation) {
    return Object.keys(operation.responses || {})
        .map(function (status) {
        var response = operation.responses[status];
        var onlyHeaders = false;
        // If we have a $ref here that means that this was a circular ref so we should ignore it.
        if ((0, rmoas_types_1.isRef)(response)) {
            return false;
        }
        var mediaTypes = {};
        (response.content ? Object.keys(response.content) : []).forEach(function (mediaType) {
            if (!mediaType)
                return;
            var mediaTypeObject = response.content[mediaType];
            var examples = (0, get_mediatype_examples_1["default"])(mediaType, mediaTypeObject, {
                includeReadOnly: true,
                includeWriteOnly: false
            });
            if (examples) {
                mediaTypes[mediaType] = examples;
            }
        });
        // If the response has no content, but has headers, hardcode an empty example so the headers
        // modal will still display
        if (response.headers && Object.keys(response.headers).length && !Object.keys(mediaTypes).length) {
            mediaTypes['*/*'] = [];
            onlyHeaders = true;
        }
        if (!Object.keys(mediaTypes).length) {
            return false;
        }
        return __assign({ status: status, mediaTypes: mediaTypes }, (onlyHeaders ? { onlyHeaders: onlyHeaders } : {}));
    })
        .filter(Boolean);
}
exports["default"] = getResponseExamples;
