import { Time } from '@pawel-up/data-mock/Time.js';
import { AmfNamespace as ns } from '../definitions/Namespace.js';
import { ApiSchemaValues } from '../ApiSchemaValues.js';
import { JsonDataNodeGenerator } from '../data-node/JsonDataNodeGenerator.js';
/**
 * A base class for generators that generates a schema from AMF's shape definition.
 */
export class ShapeBase {
    opts;
    time = new Time();
    constructor(opts = {}) {
        this.opts = Object.freeze({ ...opts });
    }
    _scalarValue(schema) {
        const { defaultValue, examples, values, inherits, dataType } = schema;
        // check examples
        if (this.opts.renderExamples && examples && examples.length) {
            const example = examples.find((item) => !!item.structuredValue);
            const value = this._exampleToObject(example);
            if (typeof value !== 'undefined') {
                return ApiSchemaValues.readTypedValue(value, dataType);
            }
        }
        // check the default value
        if (defaultValue) {
            const gen = new JsonDataNodeGenerator();
            const processed = gen.processNode(defaultValue);
            // return ApiSchemaValues.readTypedValue(processed, dataType);
            return processed;
        }
        // check enum values
        if (values && values.length) {
            const typed = values[0];
            if (typed.value !== undefined) {
                return ApiSchemaValues.readTypedValue(typed.value, dataType);
            }
        }
        // check parents
        if (Array.isArray(inherits) && inherits.length) {
            for (let i = 0, len = inherits.length; i < len; i += 1) {
                const result = this._scalarValue(inherits[i]);
                if (result !== undefined) {
                    return result;
                }
            }
        }
        if (this.opts.renderMocked) {
            return ApiSchemaValues.generateMockedValue(schema);
        }
        // return this[dataTypeToExample](dataType, format);
        // create a default value.
        return ApiSchemaValues.generateDefaultValue(schema);
    }
    /**
     * Checks whether the union represents a scalar + nil which is equivalent
     * to having scalar that is not required.
     *
     * See more about nil values in RAML:
     * https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md#nil-type
     *
     * @param union The list of unions in the shape
     */
    _isNotRequiredUnion(union) {
        let scalars = 0;
        let hasNil = false;
        union.forEach((i) => {
            if (i.types.includes(ns.aml.vocabularies.shapes.NilShape)) {
                hasNil = true;
            }
            else if (i.types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
                const scalar = i;
                if (scalar.dataType === ns.w3.xmlSchema.nil) {
                    hasNil = true;
                }
                else {
                    scalars += 1;
                }
            }
            else if (i.types.includes(ns.aml.vocabularies.shapes.FileShape)) {
                scalars += 1;
            }
        });
        if (!hasNil) {
            return false;
        }
        // size of union minus the nil union
        if (scalars === union.length - 1) {
            return true;
        }
        return false;
    }
}
//# sourceMappingURL=ShapeBase.js.map