import type { AssociationDefinition, RelationalResult } from '../types/index.js';
import type ItemsService from '../services/ItemsService.js';
/**
 * Relation builder for dynamic schemas
 */
export declare class RelationBuilder {
    private relationsMap;
    /** Reverse index: target collection → M2A junction info for O(1) delete lookups */
    private m2aTargetIndex;
    /**
     * Store association definitions for a table
     * Relations will be resolved at query time using Drizzle's query API
     * Note: This merges with existing associations instead of replacing them
     */
    storeAssociations(tableName: string, associations: Record<string, AssociationDefinition>): void;
    /**
     * Get associations for a table
     */
    getAssociations(tableName: string): Record<string, AssociationDefinition> | undefined;
    /**
     * Get all associations
     */
    getAllAssociations(): Map<string, Record<string, AssociationDefinition>>;
    /**
     * Get foreign key column name for a relation
     */
    getForeignKey(assoc: AssociationDefinition, defaultKey?: string): string;
    /**
     * Check if an association is a one-to-many type
     */
    isOneToMany(assoc: AssociationDefinition): boolean;
    /**
     * Check if an association is a many-to-one type
     */
    isManyToOne(assoc: AssociationDefinition): boolean;
    /**
     * Check if an association is a many-to-many type
     */
    isManyToMany(assoc: AssociationDefinition): boolean;
    /**
     * Get M2A junction models that reference a given target collection.
     * Uses a lazily-built reverse index for O(1) lookups instead of scanning all associations.
     */
    getM2AJunctionsForTarget(collection: string): Array<{
        model: string;
    }>;
}
/**
 * Singleton instance
 */
export declare const relationBuilder: RelationBuilder;
/**
 * Helper to create foreign key constraint SQL
 */
export declare function createForeignKeySQL(tableName: string, columnName: string, referencedTable: string, referencedColumn?: string, onDelete?: string, onUpdate?: string): string;
/**
 * Helper to check if a field is a polymorphic relation
 */
export declare function isPolymorphicRelation(assoc: AssociationDefinition): boolean;
/**
 * Helper to get polymorphic field names
 */
export declare function getPolymorphicFields(relationName: string): {
    typeField: string;
    idField: string;
};
/**
 * Build junction table for many-to-many relations
 * Example: UserRoles for User <-> Role
 */
export declare function buildJunctionTable(table1Name: string, table2Name: string, table1Key?: string, table2Key?: string): string;
/**
 * Convert Sequelize association type to Drizzle relation type
 */
export declare function sequelizeTosDrizzleRelationType(sequelizeType: string): 'one' | 'many' | null;
/**
 * Process relational data - extract nested objects/arrays
 * @param collection - Collection name
 * @param data - Data to process
 * @param service - Service instance
 * @param ItemsServiceClass - ItemsService class for creating related service instances
 */
export declare function processRelationalData(collection: string, data: Record<string, any>, service: any, ItemsServiceClass: typeof ItemsService): Promise<RelationalResult>;
/**
 * Handle HasMany relationship after main record is created
 *
 * @param item - The created/updated main record with its ID
 * @param association - Name of the association
 * @param associationInfo - Metadata about the association
 * @param value - Array of related items to create/update
 * @param service - The service instance
 * @param ItemsServiceClass - ItemsService class for creating related service instances
 * @param transaction - Optional transaction
 */
export declare function handleHasManyRelationship(item: any, association: string, associationInfo: any, value: any[], service: any, ItemsServiceClass: typeof ItemsService, transaction?: any): Promise<void>;
/**
 * Handle M2M (BelongsToMany) relationship after main record is created
 *
 * @param item - The created/updated main record with its ID
 * @param association - Name of the association
 * @param associationInfo - Metadata about the association
 * @param value - Array of related items (IDs or objects with IDs)
 * @param service - The service instance
 * @param ItemsServiceClass - ItemsService class for creating related service instances
 * @param transaction - Optional transaction
 */
export declare function handleM2MRelationship(item: any, association: string, associationInfo: any, value: any[], service: any, ItemsServiceClass: typeof ItemsService, transaction?: any): Promise<void>;
/**
 * Handle M2A (polymorphic) relationship after main record is created
 *
 * @param item - The created/updated main record with its ID
 * @param association - Name of the association
 * @param associationInfo - Metadata about the association
 * @param value - Array of polymorphic relation items
 * @param service - The service instance
 * @param ItemsServiceClass - ItemsService class (not used in M2A but kept for consistency)
 * @param transaction - Optional transaction
 */
export declare function handleM2ARelationship(item: any, association: string, associationInfo: any, value: any[], service: any, ItemsServiceClass: typeof ItemsService, transaction?: any): Promise<void>;
/**
 * Handle related records before delete.
 *
 * Regular relations (HasMany, HasOne, M2M) rely on DB FK constraints for cascade.
 * M2A targets need app-level cleanup because the polymorphic `item_id` column
 * in the junction table has no real DB FK (it references multiple tables).
 *
 * Scans all associations to find M2A sources whose `tables` array includes
 * the collection being deleted, then removes the corresponding junction rows.
 */
export declare function handleRelatedRecordsBeforeDelete(item: any, service: any, transaction?: any): Promise<void>;
/**
 * Validate relational data - ensures referenced records exist
 */
export declare function validateRelationalData(data: Record<string, any>, collection: string, service: any): Promise<void>;
/**
 * Resolve circular dependencies in data
 * Detects BelongsTo relations that reference the parent record being created
 */
export declare function resolveCircularDependencies(data: Record<string, any>, collection: string, service: any): Promise<{
    resolvedData: Record<string, any>;
    deferredFields: Array<{
        field: string;
        value: any;
    }>;
}>;
/**
 * Process deferred fields after main record creation
 * Updates related records to link back to the newly created parent
 */
export declare function processDeferredFields(item: any, deferredFields: Array<{
    field: string;
    value: any;
}>, service: any, transaction?: any): Promise<void>;
//# sourceMappingURL=relationUtils.d.ts.map