/*!
 * Copyright (C) Microsoft Corporation. All rights reserved.
 */
import { getVfs } from '../../VfsManager.js';
import { convertToValidIdentifier, normalizeGenericTypeName } from './nameUtils.js';
/**
 * Type guard for plain JSON objects. Used widely for safe property access
 * on `unknown` schema values before codegen processing begins.
 */
export function isJsonObject(value) {
    // Conclusive because 'object' type from JSON.parse() cannot have Symbol keys, or be undefined, functions, Dates, RegExp, Map/Set, or Classes
    return value !== null && typeof value === 'object' && !Array.isArray(value);
}
/** Retrieves the properties map from a schema definition for iteration during code generation. */
export function getPropertiesNode(definition) {
    if (!('properties' in definition) || !isJsonObject(definition.properties)) {
        throw new Error("The definition does not contain a valid 'properties' node.");
    }
    return definition.properties;
}
/** Returns the set of required property names from a schema, used to determine optionality in generated types. */
export function getRequiredFields(definition) {
    if ('required' in definition && Array.isArray(definition.required)) {
        return new Set(definition.required.map((r) => String(r)));
    }
    return new Set();
}
/** Converts a JSON Schema type to the TypeScript type used in generated interfaces. */
export function mapJsonTypeToTypeScript(jsonType) {
    switch (jsonType) {
        case 'string':
            return 'string';
        case 'number':
        case 'integer':
            return 'number';
        case 'boolean':
            return 'boolean';
        case 'object':
            return 'Record<string, unknown>';
        default:
            return 'unknown';
    }
}
/** Resolves a JSON Schema `$ref` to the TypeScript type name it refers to. */
export function extractReferencedType(refValue) {
    const parts = refValue.split('/');
    const raw = normalizeGenericTypeName(parts[parts.length - 1] ?? '');
    return convertToValidIdentifier(raw);
}
/** Returns the schema's `name` field, which identifies the data source or connector. */
export function getNameNode(schema) {
    if (isJsonObject(schema)) {
        const name = schema.name;
        if (typeof name !== 'string' || !name.trim()) {
            throw new Error("No 'name' node found in schema");
        }
        return name.trim();
    }
    throw new Error('Schema must be a JSON object to extract name');
}
/** Recursively collects all `.json` file paths under the given directory. */
export async function getAllJsonFiles(folderPath) {
    const files = [];
    const vfs = getVfs();
    async function recurse(dir) {
        try {
            const entries = await vfs.readdir(dir);
            for (const entry of entries) {
                const fullPath = vfs.join(dir, entry);
                if (entry.endsWith('.json')) {
                    files.push(fullPath);
                }
                else {
                    // Check if it's a directory and recurse into it
                    const isDir = await vfs.isDirectory(fullPath);
                    if (isDir) {
                        await recurse(fullPath);
                    }
                }
            }
        }
        catch (error) {
            // If we can't read the directory, skip it silently
            // This could happen if it's not a directory or if permissions are insufficient
        }
    }
    await recurse(folderPath);
    return files;
}
//# sourceMappingURL=schemaUtils.js.map