import { REFERENCE, REFERENCES, TEXT, } from '@based/schema/def';
import { QueryDefType } from '../types.js';
export const getAll = (props) => {
    const fields = [];
    for (const key in props) {
        const prop = props[key];
        if (prop.typeIndex !== REFERENCE && prop.typeIndex !== REFERENCES) {
            fields.push(prop.path.join('.'));
        }
    }
    return fields;
};
export const getAllRefs = (props, affix = '') => {
    const fields = [];
    for (const key in props) {
        const prop = props[key];
        if (prop.typeIndex === REFERENCE || prop.typeIndex === REFERENCES) {
            const refPath = prop.path.join('.') + affix;
            fields.push(refPath);
            if (prop.edges) {
                for (const edge in prop.edges) {
                    fields.push(refPath + '.' + edge);
                }
            }
        }
    }
    return fields;
};
export const includeField = (def, field) => {
    if (field === '*') {
        if (def.type === QueryDefType.Reference ||
            def.type === QueryDefType.References) {
            const fields = [];
            if (def.target.propDef.edges) {
                for (const edge in def.target.propDef.edges) {
                    fields.push(edge);
                }
            }
            includeFields(def, fields);
        }
        includeFields(def, getAll(def.props));
    }
    else if (field === '**') {
        includeFields(def, getAllRefs(def.props));
    }
    else if (field.startsWith('**.')) {
        includeFields(def, getAllRefs(def.props, field.substring(2)));
    }
    else {
        def.include.stringFields.add(field);
    }
};
export const includeFields = (def, fields) => {
    for (const field of fields) {
        includeField(def, field);
    }
};
export const includeAllProps = (def) => {
    for (const key in def.props) {
        const prop = def.props[key];
        if (prop.typeIndex !== REFERENCE && prop.typeIndex !== REFERENCES) {
            includeProp(def, prop);
        }
    }
};
export const includeProp = (def, prop) => {
    if (!prop || prop.typeIndex === REFERENCE || prop.typeIndex === REFERENCES) {
        return false;
    }
    if (prop.typeIndex === TEXT) {
        if (!def.include.langTextFields.has(prop.prop)) {
            def.include.langTextFields.set(prop.prop, { def: prop, codes: new Set() });
        }
        def.include.langTextFields.get(prop.prop).codes.add(def.lang ?? 0);
    }
    else {
        if (prop.separate) {
            def.include.props.set(prop.prop, prop);
        }
        else {
            def.include.main.len += prop.len;
            def.include.main.include[prop.start] = [0, prop];
            return true;
        }
    }
    return false;
};
//# sourceMappingURL=props.js.map