import { AmfNamespace as ns } from './definitions/Namespace.js';
import { ShapeJsonSchemaGenerator } from './shape/ShapeJsonSchemaGenerator.js';
import { ShapeXmlSchemaGenerator } from './shape/ShapeXmlSchemaGenerator.js';
/**
 * A class that processes AMF's Shape to auto-generate a schema from examples/type for a given media type.
 * This should be used when examples for the Shape are not available but the application still needs to
 * render an example or a schema from the Shape.
 * If examples can be found directly in the shape, use the `ApiExampleGenerator` instead.
 */
export class ApiSchemaGenerator {
    mime;
    opts;
    generator;
    /**
     *
     * @param mime The example mime type to format the generated example.
     * @param opts Optional configuration.
     */
    constructor(mime, opts = {}) {
        this.mime = mime;
        this.opts = Object.freeze({ ...opts });
        if (mime.includes('json')) {
            this.generator = new ShapeJsonSchemaGenerator(opts);
        }
        else if (mime.includes('xml')) {
            this.generator = new ShapeXmlSchemaGenerator(opts);
        }
    }
    /**
     * @param shape The Shape definition
     * @param mime The mime type for the value.
     * @returns Customized Example with the `renderValue` that is the generated Example value.
     */
    static asExample(shape, mime, opts) {
        const generator = new ApiSchemaGenerator(mime, opts);
        return generator.toExample(shape);
    }
    /**
     * @param shape The Shape definition
     * @param mime The mime type for the value.
     * @returns The generated schema
     */
    static asSchema(shape, mime, opts) {
        const generator = new ApiSchemaGenerator(mime, opts);
        return generator.toValue(shape);
    }
    /**
     * Generates the schema from the AMF shape.
     *
     * @param shape The Shape definition
     */
    generate(shape) {
        const { generator } = this;
        if (!generator) {
            return undefined;
        }
        return generator.generate(shape);
    }
    /**
     * @link {#generate()}
     * @param shape The Shape definition
     */
    toValue(shape) {
        return this.generate(shape);
    }
    /**
     * Generates an API Example object with the value to render.
     * @param shape The Shape definition
     * @returns Customized Example with the `renderValue` that is the generated Example value.
     */
    toExample(shape) {
        const renderValue = this.generate(shape);
        if (renderValue === null || renderValue === undefined) {
            return undefined;
        }
        const result = {
            id: `${shape.id}/generated`,
            strict: true,
            types: [ns.aml.vocabularies.apiContract.Example],
            mediaType: this.mime,
            renderValue,
            customDomainProperties: [],
        };
        const typed = shape;
        if (Array.isArray(typed.examples) && typed.examples.length) {
            const [example] = typed.examples;
            if (example.value) {
                result.value = example.value;
            }
        }
        return result;
    }
}
//# sourceMappingURL=ApiSchemaGenerator.js.map