"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRealizeWriteDto = getRealizeWriteDto;
const openapi_1 = require("@samchon/openapi");
function getRealizeWriteDto(ctx, operation) {
    return __awaiter(this, void 0, void 0, function* () {
        const document = filterDocument(operation, ctx.state().interface.document);
        const compiler = yield ctx.compiler();
        const entries = Object.entries(yield compiler.interface.write(document));
        const filter = (prefix, exclude) => {
            const result = entries.filter(([key]) => key.startsWith(prefix) === true);
            return Object.fromEntries(exclude
                ? result.filter(([key]) => key.startsWith(exclude) === false)
                : result);
        };
        const dto = filter("src/api/structures");
        return dto;
    });
}
function filterDocument(operation, document) {
    const components = {
        authorization: document.components.authorization,
        schemas: {},
    };
    const visit = (typeName) => {
        openapi_1.OpenApiTypeChecker.visit({
            components: document.components,
            schema: { $ref: `#/components/schemas/${typeName}` },
            closure: (s) => {
                if (openapi_1.OpenApiTypeChecker.isReference(s)) {
                    const key = s.$ref.split("/").pop();
                    components.schemas[key] = document.components.schemas[key];
                }
            },
        });
    };
    if (operation.requestBody)
        visit(operation.requestBody.typeName);
    if (operation.responseBody)
        visit(operation.responseBody.typeName);
    return {
        operations: [operation],
        components,
    };
}
//# sourceMappingURL=getRealizeWriteDto.js.map