import type { LockWorkflow } from '@kici-dev/engine';
import type { RegistrationStore } from './registration-store.js';
/**
 * A registered workflow entry with typed fields for fast in-memory lookup.
 */
export interface RegisteredWorkflow {
    id: string;
    repoIdentifier: string;
    workflowName: string;
    lockEntry: LockWorkflow;
    triggerTypes: string[];
    routingKey: string;
    providerContext: Record<string, unknown>;
    disabled: boolean;
    /** Whether this is a global workflow (has repo patterns) */
    isGlobal: boolean;
    /**
     * Customer/org ID that owns this registration. Drives org-scoped lookups
     * via getByOrgAndEvent — cross-org leakage is structurally impossible
     * because the byOrgAndEvent key is namespaced by customerId.
     */
    customerId: string;
    commitSha: string | null;
    sourceFile: string | null;
}
/**
 * In-memory index of workflow registrations with version-based reload.
 *
 * Provides fast lookups by trigger type, customer/repo, and event type.
 * Updates happen only via loadFromDb/refreshIfNeeded -- the index is
 * read-only from the outside.
 */
export declare class RegistrationIndex {
    private readonly store;
    /** Current version matching the DB registry_versions counter */
    private version;
    /** Primary index: keyed by `{routingKey}:{repoIdentifier}` */
    private entries;
    /** Secondary index: keyed by trigger type for fast event routing */
    private byTriggerType;
    /** Tertiary index: global workflows keyed by `{routingKey}:{triggerType}` */
    private globalByTriggerType;
    /**
     * Tertiary-b index: global workflows keyed by `{customerId}|{triggerType}`.
     * Used for cross-provider global workflow dispatch within an org — e.g.
     * a universal-git Forgejo source authors a global workflow, a GitHub App
     * source in the same org delivers a push, and the global is dispatched
     * even though the inbound routing key differs from the registration's.
     * Cross-org isolation is structurally enforced by the customer_id prefix.
     */
    private globalByOrgAndTriggerType;
    /**
     * Quaternary index: webhook-trigger registrations keyed by
     * `${customerId}|${eventName}`. Drives the cross-source webhook lookup
     * (a generic webhook arriving in org A finds all webhook-trigger
     * registrations in org A whose lock_entry has a matching event). Cross-org
     * isolation is structurally enforced by the customer_id-prefixed key.
     */
    private byOrgAndEvent;
    /**
     * Quinary index: registrations keyed by `${customerId}|${repoIdentifier}`.
     * Drives the cross-source repo lookup (phase 28.5): when a generic webhook
     * carries a repository identifier in its payload, the dispatcher looks up
     * registrations for that (org, repo) pair across ALL routing keys and uses
     * each registration's stored `providerContext` + `routingKey` to reach the
     * owning provider bundle (e.g. a github source) for credential minting and
     * trigger evaluation. Cross-org isolation is structurally enforced by the
     * customer_id-prefixed key.
     */
    private byOrgAndRepo;
    constructor(store: RegistrationStore);
    /**
     * Bulk load all registrations from the DB store.
     * Rebuilds both in-memory indexes and updates the version.
     */
    loadFromDb(): Promise<void>;
    /**
     * Reload from DB if the remote version is newer than the local version.
     * Called when a peer heartbeat reports a higher registryVersion.
     */
    refreshIfNeeded(remoteVersion: number): Promise<void>;
    /**
     * Get the current in-memory version.
     */
    getVersion(): number;
    /**
     * Look up a single registration by ID (includes disabled entries).
     * Used by manual schedule to distinguish "not found" from "disabled".
     */
    getById(id: string): RegisteredWorkflow | undefined;
    /**
     * Get all workflows registered for a specific trigger type.
     * Used for event routing (e.g., find all workflows listening for 'kici_event').
     */
    getByTriggerType(triggerType: string): RegisteredWorkflow[];
    /**
     * Get all workflows registered for a specific routing key and repo.
     */
    getByRoutingKeyAndRepo(routingKey: string, repoIdentifier: string): RegisteredWorkflow[];
    /**
     * Cross-source webhook lookup: return all webhook-trigger registrations
     * within a customer/org that listen for `eventName`.
     *
     * Used by the cross-source dispatch branch in `processWebhook()` so that an
     * inbound generic webhook in org A can fan out to webhook-trigger workflows
     * registered to ANY source in org A. Org isolation is structurally enforced:
     * the lookup key is `${customerId}|${eventName}`, so registrations in a
     * different customer's bucket cannot leak through this method even if the
     * eventName matches.
     *
     * Disabled rows are filtered out, mirroring the other typed lookups.
     */
    getByOrgAndEvent(customerId: string, eventName: string): RegisteredWorkflow[];
    /**
     * Cross-source repo lookup: return all registrations in a given customer/org
     * that target a specific repo identifier, across all routing keys.
     *
     * Used by the cross-source dispatch branch in `processWebhook()` so that a
     * generic webhook carrying `repository.full_name` (or an equivalent payload
     * field) can be routed to git-trigger workflows (push, pr, tag, …) owned by
     * any source in the same org. Each returned registration carries its own
     * `routingKey` + `providerContext`, which the caller uses to resolve the
     * correct provider bundle and mint credentials for the owning provider.
     *
     * Org isolation is structurally enforced: the lookup key is
     * `${customerId}|${repoIdentifier}`, so registrations in a different
     * customer's bucket cannot leak through even if the repo identifier matches.
     *
     * Disabled rows are filtered out, mirroring the other typed lookups.
     */
    getByOrgAndRepo(customerId: string, repoIdentifier: string): RegisteredWorkflow[];
    /**
     * Get global workflows matching a trigger type within a routing key.
     * Used by the webhook processor for dual-query (per-repo + global).
     */
    getGlobalByTriggerType(triggerType: string, routingKey: string): RegisteredWorkflow[];
    /**
     * Get global workflows matching a trigger type within an org, across ALL
     * routing keys. Used by the webhook processor for cross-provider global
     * workflow dispatch.
     */
    getGlobalByOrgAndTriggerType(customerId: string, triggerType: string): RegisteredWorkflow[];
    /**
     * Convenience: get all workflows with 'schedule' trigger type.
     * Used by the cron scheduler to evaluate pending schedules.
     */
    getCronSchedules(): RegisteredWorkflow[];
    /**
     * Get workflows matching an event type string.
     * Maps event type strings to trigger type strings for lookup.
     *
     * Event type -> trigger type mapping:
     * - 'kici_event' -> 'kici_event'
     * - 'workflow_complete' -> 'workflow_complete' or 'lifecycle'
     * - 'job_complete' -> 'job_complete' or 'lifecycle'
     * - 'generic_webhook' -> 'generic_webhook'
     * - 'schedule' -> 'schedule'
     * - lifecycle event names -> 'lifecycle'
     */
    getByEventType(eventType: string): RegisteredWorkflow[];
}
//# sourceMappingURL=registration-index.d.ts.map