"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AvoSchemaParser = void 0;
const isArray = (obj) => {
    return Object.prototype.toString.call(obj) === '[object Array]';
};
class AvoSchemaParser {
    static extractSchema(eventProperties) {
        if (eventProperties === null || eventProperties === undefined) {
            return [];
        }
        const mapping = (object) => {
            if (isArray(object)) {
                const list = object.map((x) => {
                    return mapping(x);
                });
                return this.removeDuplicates(list);
            }
            else if (typeof object === 'object') {
                const mappedResult = [];
                for (const key in object) {
                    if (object.hasOwnProperty(key)) {
                        const val = object[key];
                        const mappedEntry = {
                            propertyName: key,
                            propertyType: this.getPropValueType(val)
                        };
                        if (typeof val === 'object' && val != null) {
                            mappedEntry['children'] = mapping(val);
                        }
                        mappedResult.push(mappedEntry);
                    }
                }
                return mappedResult;
            }
            else {
                return [];
            }
        };
        const mappedEventProps = mapping(eventProperties);
        return mappedEventProps;
    }
    static removeDuplicates(array) {
        const seen = {};
        return array.filter((item) => {
            const key = `${item.propertyName}:${item.propertyType}`;
            if (!seen[key]) {
                seen[key] = true;
                return true;
            }
            return false;
        });
    }
    static getBasicPropType(propValue) {
        const propType = typeof propValue;
        if (propValue == null) {
            return 'null';
        }
        else if (propType === 'string') {
            return 'string';
        }
        else if (propType === 'number' || propType === 'bigint') {
            if ((propValue + '').indexOf('.') >= 0) {
                return 'float';
            }
            else {
                return 'int';
            }
        }
        else if (propType === 'boolean') {
            return 'boolean';
        }
        else if (propType === 'object') {
            return 'object';
        }
        else {
            return 'unknown';
        }
    }
    static getPropValueType(propValue) {
        if (isArray(propValue)) {
            const propElement = propValue[0];
            if (propElement == null) {
                return 'list';
            }
            else {
                const propElementType = this.getBasicPropType(propElement);
                return `list(${propElementType})`;
            }
        }
        else {
            return this.getBasicPropType(propValue);
        }
    }
}
exports.AvoSchemaParser = AvoSchemaParser;
//# sourceMappingURL=AvoSchemaParser.js.map