import { AmfNamespace as ns } from '../definitions/Namespace.js';
import { readTypedValue } from '../Utils.js';
/**
 * Base class for all schema generators based on AMF's DataNode which includes AMF's examples.
 */
export class DataNodeBase {
    /**
     * @param node The AMF data node to turn into a schema.
     * @returns Undefined when passed non-DataNode domain element.
     */
    processNode(node) {
        const { types } = node;
        if (types.includes(ns.aml.vocabularies.data.Scalar)) {
            return this.processScalarNode(node);
        }
        if (types.includes(ns.aml.vocabularies.data.Array)) {
            return this.processArrayNode(node);
        }
        if (types.includes(ns.aml.vocabularies.data.Object)) {
            return this.processObjectNode(node);
        }
        return undefined;
    }
    /**
     * @param scalar The scalar node to process.
     * @returns The scalar value.
     */
    processScalarNode(scalar) {
        return readTypedValue(scalar.value, scalar.dataType);
    }
    /**
     * @param array The array node to process.
     * @returns Array value.
     */
    processArrayNode(array) {
        const container = [];
        array.members.forEach((member) => {
            const result = this.processNode(member);
            if (result !== undefined) {
                container.push(result);
            }
        });
        return container;
    }
    /**
     * @param object The object node to process.
     * @returns Object value.
     */
    processObjectNode(object) {
        const container = {};
        const { properties } = object;
        Object.keys(properties).forEach((key) => {
            const definition = properties[key];
            const result = this.processNode(definition);
            if (typeof result !== 'undefined') {
                const name = this.normalizePropertyName(key);
                container[name] = result;
            }
        });
        return container;
    }
    /**
     * Normalizes a property name. It decodes URL encoded values.
     * @param name The property name to normalize
     */
    normalizePropertyName(name) {
        let result = name;
        try {
            result = decodeURIComponent(result);
        }
        catch {
            // ...
        }
        return result;
    }
}
//# sourceMappingURL=DataNodeBase.js.map