"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateTypes = void 0;
/* eslint-disable no-nested-ternary */
const fs_1 = __importDefault(require("fs"));
const json_schema_to_typescript_1 = require("json-schema-to-typescript");
const logger_1 = __importDefault(require("../utilities/logger"));
const load_1 = __importDefault(require("../config/load"));
const entityToJSONSchema_1 = require("../utilities/entityToJSONSchema");
function configToJsonSchema(config) {
    return {
        title: 'Config',
        type: 'object',
        additionalProperties: false,
        properties: {
            collections: (0, entityToJSONSchema_1.generateEntityObject)(config, 'collections'),
            globals: (0, entityToJSONSchema_1.generateEntityObject)(config, 'globals'),
        },
        required: ['collections', 'globals'],
        definitions: Object.fromEntries([
            ...config.globals.map((global) => [
                global.slug,
                (0, entityToJSONSchema_1.entityToJSONSchema)(config, global),
            ]),
            ...config.collections.map((collection) => [
                collection.slug,
                (0, entityToJSONSchema_1.entityToJSONSchema)(config, collection),
            ]),
        ]),
    };
}
function generateTypes() {
    const logger = (0, logger_1.default)();
    const config = (0, load_1.default)();
    const outputFile = process.env.PAYLOAD_TS_OUTPUT_PATH || config.typescript.outputFile;
    logger.info('Compiling TS types for Collections and Globals...');
    const jsonSchema = configToJsonSchema(config);
    (0, json_schema_to_typescript_1.compile)(jsonSchema, 'Config', {
        bannerComment: '/* tslint:disable */\n/**\n* This file was automatically generated by Payload CMS.\n* DO NOT MODIFY IT BY HAND. Instead, modify your source Payload config,\n* and re-run `payload generate:types` to regenerate this file.\n*/',
        style: {
            singleQuote: true,
        },
    }).then((compiled) => {
        fs_1.default.writeFileSync(outputFile, compiled);
        logger.info(`Types written to ${outputFile}`);
    });
}
exports.generateTypes = generateTypes;
// when generateTypes.js is launched directly
if (module.id === require.main.id) {
    generateTypes();
}
//# sourceMappingURL=generateTypes.js.map