"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TypeIsArrayFixValue = exports.TypeIsArrayToSwagger = void 0;
const typeGenerator_1 = require("./generators/typeGenerator");
const isClass_1 = __importDefault(require("../utils/isClass"));
const toSwaggerGenerator_1 = require("./generators/toSwaggerGenerator");
const router_1 = require("@asapjs/router");
exports.TypeIsArrayToSwagger = (0, toSwaggerGenerator_1.toSwaggerGenerator)((options) => {
    var _a;
    const isDto = (0, isClass_1.default)(options);
    const generateScheme = (items) => ({
        type: 'array',
        items,
    });
    if (isDto) {
        const dto = new options();
        const name = `${dto.constructor.name}_ARRAY`;
        (0, router_1.addScheme)({
            name,
            data: generateScheme(dto.swagger()),
        });
        return {
            $ref: `#/components/schemas/${name}`,
        };
    }
    else {
        const type = options();
        const name = `TYPE_${type.__name.toUpperCase()}_ARRAY`;
        (0, router_1.addScheme)({
            name,
            data: generateScheme((_a = type.toSwagger) === null || _a === void 0 ? void 0 : _a.call(type)),
        });
        return {
            $ref: `#/components/schemas/${name}`,
        };
    }
});
const TypeIsArrayFixValue = (o) => {
    try {
        if (typeof o === 'string')
            return JSON.parse(String(o));
        return o;
    }
    catch (_a) {
        return o;
    }
};
exports.TypeIsArrayFixValue = TypeIsArrayFixValue;
const TypeIsArray = (0, typeGenerator_1.typeGenerator)((options) => ({
    __name: 'array',
    toSwagger: () => (0, exports.TypeIsArrayToSwagger)(options),
    fixValue: exports.TypeIsArrayFixValue,
}));
exports.default = TypeIsArray;
//# sourceMappingURL=array.js.map