import { AmfNamespace } from '@api-client/core/amf/definitions/Namespace.js';
/**
 * Computes a data model for custom definition for the OAuth 2 scheme
 * According to the annotation published at
 * https://github.com/raml-org/raml-annotations/tree/master/annotations/security-schemes
 */
export class Oauth2RamlCustomData {
    readParams(properties) {
        const result = [];
        Object.keys(properties).forEach((key) => {
            const definition = properties[key];
            if (definition.types.includes(AmfNamespace.aml.vocabularies.data.Object)) {
                const property = this.getProperty(definition);
                result.push(property);
            }
            else if (definition.types.includes(AmfNamespace.aml.vocabularies.data.Scalar)) {
                const property = this.getPropertyScalar(definition);
                result.push(property);
            }
        });
        return result;
    }
    /**
     * Creates an IApiParameter for an annotation that has properties.
     * This expects the properties to be defined like RAML's type definition.
     */
    getProperty(definition) {
        const { properties = {}, id, name } = definition;
        const result = {
            id,
            name,
            examples: [],
            payloads: [],
            types: [AmfNamespace.aml.vocabularies.apiContract.Parameter],
            customDomainProperties: [],
        };
        const schema = this.readSchema(definition);
        if (schema) {
            result.schema = schema;
        }
        if (properties.required) {
            const req = properties.required;
            result.required = req.value === 'true';
        }
        return result;
    }
    /**
     * Creates an IApiParameter for an annotation that has no properties but rather a simplified
     * notation of `propertyName: dataType`.
     */
    getPropertyScalar(definition) {
        const { dataType = '', id, name } = definition;
        const result = {
            id,
            name,
            examples: [],
            payloads: [],
            types: [AmfNamespace.aml.vocabularies.apiContract.Parameter],
            customDomainProperties: [],
        };
        const schema = this.createSchema();
        schema.types = [AmfNamespace.aml.vocabularies.shapes.ScalarShape];
        schema.id = id;
        schema.name = name;
        schema.dataType = this.typeToSchemaType(dataType);
        result.schema = schema;
        return result;
    }
    readSchema(property) {
        const { properties = {}, name, id } = property;
        // const { example, examples, } = properties;
        const isArray = this.readIsSchemaArray(properties.type, properties.items);
        const type = this.readSchemaType(properties.type, properties.items);
        let schema;
        if (isArray) {
            const s = this.createSchema();
            s.types = [AmfNamespace.aml.vocabularies.shapes.ArrayShape];
            s.id = id;
            s.name = name;
            s.items = this.createTypedSchema(type, property);
            schema = s;
        }
        else {
            schema = this.createTypedSchema(type, property);
        }
        return schema;
    }
    createTypedSchema(type, object) {
        switch (type) {
            case 'string':
            case 'number':
            case 'integer':
            case 'float':
            case 'double':
            case 'long':
            case 'date-only':
            case 'date-time':
            case 'time-only':
            case 'nil':
            case 'date':
            case 'boolean':
                return this.createScalarSchema(object, type);
            default:
                return undefined;
        }
    }
    createSchema() {
        return {
            id: '',
            types: [],
            and: [],
            name: '',
            inherits: [],
            or: [],
            values: [],
            xone: [],
            customDomainProperties: [],
        };
    }
    createScalarSchema(object, type) {
        const { properties = {}, name, id } = object;
        const schema = this.createSchema();
        schema.types = [AmfNamespace.aml.vocabularies.shapes.ScalarShape];
        schema.id = id;
        schema.name = name;
        schema.dataType = this.typeToSchemaType(type);
        if (properties.format) {
            const item = properties.format;
            schema.format = item.value;
        }
        if (properties.default) {
            const item = properties.default;
            schema.defaultValueStr = item.value;
            // schema.defaultValue = item.value;
        }
        if (properties.description) {
            const item = properties.description;
            schema.description = item.value;
        }
        if (properties.displayName) {
            const item = properties.displayName;
            schema.displayName = item.value;
        }
        if (properties.pattern) {
            const item = properties.pattern;
            schema.pattern = item.value;
        }
        if (properties.maximum) {
            const item = properties.maximum;
            const value = Number(item.value);
            if (!Number.isNaN(value)) {
                schema.maximum = value;
            }
        }
        if (properties.minimum) {
            const item = properties.minimum;
            const value = Number(item.value);
            if (!Number.isNaN(value)) {
                schema.minimum = value;
            }
        }
        if (properties.multipleOf) {
            const item = properties.multipleOf;
            const value = Number(item.value);
            if (!Number.isNaN(value)) {
                schema.multipleOf = value;
            }
        }
        if (properties.maxLength) {
            const item = properties.maxLength;
            const value = Number(item.value);
            if (!Number.isNaN(value)) {
                schema.maxLength = value;
            }
        }
        if (properties.minLength) {
            const item = properties.minLength;
            const value = Number(item.value);
            if (!Number.isNaN(value)) {
                schema.minLength = value;
            }
        }
        return schema;
    }
    /**
     * @param object
     * @param items The definition of the `items` property that corresponds to RAML's items property of an array
     */
    readSchemaType(object, items) {
        if (object.dataType !== AmfNamespace.w3.xmlSchema.string) {
            return AmfNamespace.w3.xmlSchema.string;
        }
        let inputType = object.value || '';
        if (inputType.endsWith('[]')) {
            inputType = inputType.replace('[]', '');
        }
        if (inputType === 'array' && items) {
            return this.readSchemaType(items);
        }
        return inputType || 'string';
    }
    typeToSchemaType(type) {
        switch (type) {
            case 'boolean':
                return AmfNamespace.w3.xmlSchema.boolean;
            case 'number':
                return AmfNamespace.w3.xmlSchema.number;
            case 'integer':
                return AmfNamespace.w3.xmlSchema.integer;
            case 'float':
                return AmfNamespace.w3.xmlSchema.float;
            case 'double':
                return AmfNamespace.w3.xmlSchema.double;
            case 'long':
                return AmfNamespace.w3.xmlSchema.long;
            case 'date-only':
                return AmfNamespace.w3.xmlSchema.date;
            case 'date-time':
                return AmfNamespace.w3.xmlSchema.dateTime;
            case 'time-only':
                return AmfNamespace.w3.xmlSchema.time;
            case 'nil':
                return AmfNamespace.w3.xmlSchema.nil;
            default:
                return AmfNamespace.w3.xmlSchema.string;
        }
    }
    /**
     * Checks whether the custom property represents an array.
     * @param type
     * @param items The definition of the `items` property that corresponds to RAML's items property of an array
     * @returns True when the schema is an array.
     */
    readIsSchemaArray(type, items) {
        if (!type && items) {
            return true;
        }
        if (!type) {
            return false;
        }
        const inputType = type.value || '';
        if (inputType.endsWith('[]') || inputType === 'array') {
            return true;
        }
        return false;
    }
}
//# sourceMappingURL=Oauth2RamlCustomData.js.map