import { langCodesMap } from '@based/schema';
import { DEF_RANGE_PROP_LIMIT, DEF_RANGE_REF_LIMIT } from './thresholds.js';
import { QueryDefType, } from './types.js';
import { validateAlias, validateId, validateIds, validateType, } from './validation.js';
const createEmptySharedDef = (skipValidation) => {
    const q = {
        errors: [],
        skipValidation,
        filter: { conditions: new Map(), size: 0 },
        range: { offset: 0, limit: 0 },
        lang: langCodesMap.get('none'),
        include: {
            langTextFields: new Map(),
            stringFields: new Set(),
            props: new Map(),
            propsRead: {},
            main: {
                len: 0,
                include: {},
            },
        },
        aggregate: null,
        sort: null,
        references: new Map(),
    };
    return q;
};
export const createQueryDef = (db, type, target, skipValidation) => {
    const queryDef = createEmptySharedDef(skipValidation);
    if (type === QueryDefType.Edge) {
        const t = target;
        const q = queryDef;
        q.props = t.ref.edges;
        q.type = type;
        q.target = t;
        return q;
    }
    else {
        const t = target;
        const q = queryDef;
        q.schema = validateType(db, q, t.type);
        q.props = q.schema.props;
        q.type = type;
        q.target = t;
        if (type === QueryDefType.Root) {
            if (t.id) {
                t.id = validateId(q, t.id);
            }
            else if (t.ids) {
                t.ids = validateIds(q, t.ids);
                q.range.limit = t.ids.length;
            }
            else if (t.alias) {
                t.resolvedAlias = validateAlias(q, t.alias);
            }
            else {
                q.range.limit = DEF_RANGE_PROP_LIMIT;
            }
        }
        else if (type === QueryDefType.References) {
            q.range.limit = DEF_RANGE_REF_LIMIT;
        }
        return q;
    }
};
//# sourceMappingURL=queryDef.js.map