import { dereferenceSchema } from "./refs.js";
import { isArraySchema, isObjectSchema } from "./types/utils.js";
export function getShapePaths(projectSchema, shape, args = {}) {
    const { includeRoot = false, prefix = '', depthLimit = 2 } = args;
    const options = [];
    const visit = (propertySchema, path, depth) => {
        if (depth > depthLimit) {
            return;
        }
        if (depth !== 0 || includeRoot) {
            options.push(path.join('.'));
        }
        const schema = dereferenceSchema(projectSchema, propertySchema);
        if (isArraySchema(schema)) {
            if (isObjectSchema(schema.items)) {
                visit(schema.items, path, depth + 1);
            }
        }
        else if (isObjectSchema(schema)) {
            for (const [name, propSchema] of Object.entries(schema.properties)) {
                if (propSchema.type === 'array') {
                    const arrayPath = [...path, name];
                    if (depth + 1 <= depthLimit) {
                        options.push(arrayPath.join('.'));
                    }
                    if (propSchema.items) {
                        const indexedPath = [...path, `${name}[0]`];
                        visit(propSchema.items, indexedPath, depth + 1);
                    }
                }
                else {
                    const newPath = [...path, name];
                    visit(propSchema, newPath, depth + 1);
                }
            }
        }
    };
    visit(shape.schema, prefix ? [prefix] : [], 0);
    return options;
}
