import { ApiExampleGenerator, ApiMonacoSchemaGenerator, ApiSchemaGenerator } from '@api-client/core/build/browser.js';
const cache = new Map();
export function getPayloadValue(payload) {
    if (cache.has(payload.id)) {
        return cache.get(payload.id);
    }
    const { id, mediaType = 'text/plain', schema } = payload;
    if (mediaType === 'multipart/form-data') {
        // schema generators don't support this yet,
        const info = { value: new FormData(), schemas: [] };
        cache.set(id, info);
        return info;
    }
    if (!schema) {
        const info = { value: '', schemas: [] };
        cache.set(id, info);
        return info;
    }
    const schemaFactory = new ApiMonacoSchemaGenerator();
    const monacoSchemes = schemaFactory.generate(schema, id);
    let { examples } = payload;
    if (!Array.isArray(examples) || !examples.length) {
        examples = schema.examples;
    }
    if (Array.isArray(examples) && examples.length) {
        const [example] = examples;
        const generator = new ApiExampleGenerator();
        const value = generator.read(example, mediaType);
        const info = { value, schemas: monacoSchemes };
        cache.set(id, info);
        return info;
    }
    // generate values.
    const result = ApiSchemaGenerator.asExample(schema, mediaType, {
        selectedUnions: [],
        renderExamples: true,
        renderOptional: true,
        renderMocked: true,
    });
    if (!result || !result.renderValue) {
        const info = { value: '', schemas: monacoSchemes };
        cache.set(id, info);
        return info;
    }
    const info = { value: result.renderValue, schemas: monacoSchemes };
    cache.set(id, info);
    return info;
}
/**
 * @param id The ApiPayload id.
 * @param value The value to cache.
 * @param model Optional model to set.
 */
export function cachePayloadValue(id, value, model) {
    if (cache.has(id)) {
        const info = cache.get(id);
        info.value = value;
        if (model) {
            info.model = model;
        }
        return;
    }
    cache.set(id, { value, model });
}
/**
 * @param id Payload id to read the value.
 */
export function readCachePayloadValue(id) {
    return cache.get(id);
}
//# sourceMappingURL=PayloadUtils.js.map