import { z } from 'zod';

/**
 * Integration subsystem — DetectionConfig schema (#226-1)
 *
 * Canonical Zod schema for per-entity integration detection config. The schema is
 * the single source of truth for filter/mapping shape and is consumed by:
 *
 *   1. Runtime primitives — `PollChangeSource<T>`, `WebhookChangeSource<T>`
 *      (#226-3, #226-4) accept a parsed `DetectionConfig` at construction.
 *   2. Codegen — `src/schema/entity-definition.schema.ts` (#226-6) imports
 *      this schema so per-entity YAML `detection:` blocks validate against
 *      the same shape the runtime enforces.
 *
 * Locked decisions (see ADR-033 + decision memo Q1–Q6):
 *   - Filter vocabulary is flat AND of `{ field, op, value }` triples; richer
 *     boolean expressions (OR / NOT / nested) are deferred per epic open Q3.
 *   - Cursor strategy is a tagged union over the four shapes the three modes
 *     need (`systemModstamp`, `replayId`, `timestamp`, `eventId`). Each
 *     strategy types its cursor internally; the orchestrator persists what
 *     the iterator last yielded (integration skill rule 2).
 *   - `mode: 'poll'` may opt into `provenance: 'cdc'` so Stripe-style event
 *     endpoints (mechanically a poll, semantically CDC) reuse the poll
 *     primitive while emitting `Change<T>.source = 'cdc'`. Long-lived
 *     streaming CDC (SFDC Pub-Sub, Debezium) is a separate primitive
 *     deferred to #226-8.
 *   - `webhook` mode's `eventIdField` is optional: `WebhookChangeSource<T>`
 *     prefers an `eventId` yielded by the queue iterator and falls back to the
 *     `eventIdField` record extraction (precedence: yielded eventId >
 *     eventIdField extraction > undefined dedupKey).
 */

/**
 * Maps a single provider field onto the canonical record. `transform` is an
 * opt-in tag the adapter callback may inspect (`date-iso`, `decimal-string`,
 * etc.); the schema does not enumerate transforms — adapters interpret them.
 */
declare const FieldMappingSchema: z.ZodObject<{
    source: z.ZodString;
    target: z.ZodString;
    transform: z.ZodOptional<z.ZodString>;
}, "strip", z.ZodTypeAny, {
    source: string;
    target: string;
    transform?: string | undefined;
}, {
    source: string;
    target: string;
    transform?: string | undefined;
}>;
type FieldMapping = z.infer<typeof FieldMappingSchema>;
/**
 * A single resolved filter clause applied at fetch time. `value` is `unknown`
 * to admit primitives, arrays (for `in` / `nin`), and dates as ISO strings —
 * adapters interpret per provider.
 */
declare const ResolvedFilterSchema: z.ZodObject<{
    field: z.ZodString;
    op: z.ZodEnum<["eq", "neq", "in", "nin", "gt", "gte", "lt", "lte"]>;
    value: z.ZodUnknown;
}, "strip", z.ZodTypeAny, {
    field: string;
    op: "eq" | "neq" | "in" | "nin" | "gt" | "gte" | "lt" | "lte";
    value?: unknown;
}, {
    field: string;
    op: "eq" | "neq" | "in" | "nin" | "gt" | "gte" | "lt" | "lte";
    value?: unknown;
}>;
type ResolvedFilter = z.infer<typeof ResolvedFilterSchema>;
declare const CursorStrategySchema: z.ZodDiscriminatedUnion<"kind", [z.ZodObject<{
    kind: z.ZodLiteral<"systemModstamp">;
    field: z.ZodString;
}, "strip", z.ZodTypeAny, {
    field: string;
    kind: "systemModstamp";
}, {
    field: string;
    kind: "systemModstamp";
}>, z.ZodObject<{
    kind: z.ZodLiteral<"replayId">;
    field: z.ZodString;
}, "strip", z.ZodTypeAny, {
    field: string;
    kind: "replayId";
}, {
    field: string;
    kind: "replayId";
}>, z.ZodObject<{
    kind: z.ZodLiteral<"timestamp">;
    field: z.ZodString;
}, "strip", z.ZodTypeAny, {
    field: string;
    kind: "timestamp";
}, {
    field: string;
    kind: "timestamp";
}>, z.ZodObject<{
    kind: z.ZodLiteral<"eventId">;
    field: z.ZodString;
}, "strip", z.ZodTypeAny, {
    field: string;
    kind: "eventId";
}, {
    field: string;
    kind: "eventId";
}>, z.ZodObject<{
    kind: z.ZodLiteral<"historyId">;
    field: z.ZodString;
}, "strip", z.ZodTypeAny, {
    field: string;
    kind: "historyId";
}, {
    field: string;
    kind: "historyId";
}>, z.ZodObject<{
    kind: z.ZodLiteral<"syncToken">;
    field: z.ZodString;
}, "strip", z.ZodTypeAny, {
    field: string;
    kind: "syncToken";
}, {
    field: string;
    kind: "syncToken";
}>]>;
type CursorStrategy = z.infer<typeof CursorStrategySchema>;
/**
 * Whether a cursor strategy is *divisible* — a property of the strategy, not
 * the read primitive. Divisible cursors are sortable/monotonic watermarks whose
 * value is meaningful AS OF any single record (HubSpot `systemModstamp`, a
 * `timestamp` field, a Salesforce CDC `replayId`); the read primitive may
 * checkpoint per-ref mid-walk, so a crash resumes from the last delivered ref.
 *
 * Atomic cursors are opaque vendor tokens (Gmail `historyId`, Calendar
 * `syncToken`, a generic `eventId`) whose next value only exists at end-of-walk.
 * The primitive must withhold per-ref cursors and emit the token only at a safe
 * boundary, so an interrupted run never persists an unresumable mid-walk token
 * (it resumes all-or-nothing from the prior token — see `IncrementalReadBase`).
 *
 * `eventId` is classified atomic conservatively: a generic opaque id is treated
 * all-or-nothing unless a concrete strategy proves it monotonically resumable.
 */
declare const CURSOR_DIVISIBILITY: Readonly<Record<CursorStrategy["kind"], boolean>>;
/** Predicate form of {@link CURSOR_DIVISIBILITY}. */
declare function isDivisibleCursor(kind: CursorStrategy["kind"]): boolean;
/**
 * Poll-mode block. `provenance: 'cdc'` opts the poll primitive into stamping
 * `Change<T>.source = 'cdc'` and populating `dedupKey` from the cursor's
 * `field` — used for Stripe-style event endpoints. Defaults to `'poll'`.
 */
declare const PollDetectionSchema: z.ZodObject<{
    cursor: z.ZodDiscriminatedUnion<"kind", [z.ZodObject<{
        kind: z.ZodLiteral<"systemModstamp">;
        field: z.ZodString;
    }, "strip", z.ZodTypeAny, {
        field: string;
        kind: "systemModstamp";
    }, {
        field: string;
        kind: "systemModstamp";
    }>, z.ZodObject<{
        kind: z.ZodLiteral<"replayId">;
        field: z.ZodString;
    }, "strip", z.ZodTypeAny, {
        field: string;
        kind: "replayId";
    }, {
        field: string;
        kind: "replayId";
    }>, z.ZodObject<{
        kind: z.ZodLiteral<"timestamp">;
        field: z.ZodString;
    }, "strip", z.ZodTypeAny, {
        field: string;
        kind: "timestamp";
    }, {
        field: string;
        kind: "timestamp";
    }>, z.ZodObject<{
        kind: z.ZodLiteral<"eventId">;
        field: z.ZodString;
    }, "strip", z.ZodTypeAny, {
        field: string;
        kind: "eventId";
    }, {
        field: string;
        kind: "eventId";
    }>, z.ZodObject<{
        kind: z.ZodLiteral<"historyId">;
        field: z.ZodString;
    }, "strip", z.ZodTypeAny, {
        field: string;
        kind: "historyId";
    }, {
        field: string;
        kind: "historyId";
    }>, z.ZodObject<{
        kind: z.ZodLiteral<"syncToken">;
        field: z.ZodString;
    }, "strip", z.ZodTypeAny, {
        field: string;
        kind: "syncToken";
    }, {
        field: string;
        kind: "syncToken";
    }>]>;
    provenance: z.ZodOptional<z.ZodEnum<["poll", "cdc"]>>;
}, "strip", z.ZodTypeAny, {
    cursor: {
        field: string;
        kind: "systemModstamp";
    } | {
        field: string;
        kind: "replayId";
    } | {
        field: string;
        kind: "timestamp";
    } | {
        field: string;
        kind: "eventId";
    } | {
        field: string;
        kind: "historyId";
    } | {
        field: string;
        kind: "syncToken";
    };
    provenance?: "poll" | "cdc" | undefined;
}, {
    cursor: {
        field: string;
        kind: "systemModstamp";
    } | {
        field: string;
        kind: "replayId";
    } | {
        field: string;
        kind: "timestamp";
    } | {
        field: string;
        kind: "eventId";
    } | {
        field: string;
        kind: "historyId";
    } | {
        field: string;
        kind: "syncToken";
    };
    provenance?: "poll" | "cdc" | undefined;
}>;
type PollDetection = z.infer<typeof PollDetectionSchema>;
/**
 * Webhook-mode block. `eventIdField`, when present, names the field on the
 * emitted canonical record that `WebhookChangeSource<T>` reads to set
 * `Change<T>.dedupKey` — used only as the fallback when the queue iterator
 * does NOT yield an `eventId` alongside the record.
 *
 * `eventIdField` is **optional**: a queue iterator that always yields an
 * `eventId` (vendor delivery metadata, the preferred channel) need not declare
 * a record field for it. dedupKey precedence is: yielded `eventId` >
 * `eventIdField` record extraction > undefined.
 */
declare const WebhookDetectionSchema: z.ZodObject<{
    eventIdField: z.ZodOptional<z.ZodString>;
}, "strip", z.ZodTypeAny, {
    eventIdField?: string | undefined;
}, {
    eventIdField?: string | undefined;
}>;
type WebhookDetection = z.infer<typeof WebhookDetectionSchema>;
/**
 * Top-level detection config. Discriminated on `mode` so the relevant
 * mode-block (poll/webhook) is structurally required for that mode. CDC as a
 * long-lived streaming primitive is deferred (#226-8); CDC-as-provenance
 * (Stripe-style event endpoints) is expressed via `mode: 'poll'` with
 * `poll.provenance: 'cdc'`.
 */
declare const DetectionConfigSchema: z.ZodDiscriminatedUnion<"mode", [z.ZodObject<{
    mode: z.ZodLiteral<"poll">;
    poll: z.ZodObject<{
        cursor: z.ZodDiscriminatedUnion<"kind", [z.ZodObject<{
            kind: z.ZodLiteral<"systemModstamp">;
            field: z.ZodString;
        }, "strip", z.ZodTypeAny, {
            field: string;
            kind: "systemModstamp";
        }, {
            field: string;
            kind: "systemModstamp";
        }>, z.ZodObject<{
            kind: z.ZodLiteral<"replayId">;
            field: z.ZodString;
        }, "strip", z.ZodTypeAny, {
            field: string;
            kind: "replayId";
        }, {
            field: string;
            kind: "replayId";
        }>, z.ZodObject<{
            kind: z.ZodLiteral<"timestamp">;
            field: z.ZodString;
        }, "strip", z.ZodTypeAny, {
            field: string;
            kind: "timestamp";
        }, {
            field: string;
            kind: "timestamp";
        }>, z.ZodObject<{
            kind: z.ZodLiteral<"eventId">;
            field: z.ZodString;
        }, "strip", z.ZodTypeAny, {
            field: string;
            kind: "eventId";
        }, {
            field: string;
            kind: "eventId";
        }>, z.ZodObject<{
            kind: z.ZodLiteral<"historyId">;
            field: z.ZodString;
        }, "strip", z.ZodTypeAny, {
            field: string;
            kind: "historyId";
        }, {
            field: string;
            kind: "historyId";
        }>, z.ZodObject<{
            kind: z.ZodLiteral<"syncToken">;
            field: z.ZodString;
        }, "strip", z.ZodTypeAny, {
            field: string;
            kind: "syncToken";
        }, {
            field: string;
            kind: "syncToken";
        }>]>;
        provenance: z.ZodOptional<z.ZodEnum<["poll", "cdc"]>>;
    }, "strip", z.ZodTypeAny, {
        cursor: {
            field: string;
            kind: "systemModstamp";
        } | {
            field: string;
            kind: "replayId";
        } | {
            field: string;
            kind: "timestamp";
        } | {
            field: string;
            kind: "eventId";
        } | {
            field: string;
            kind: "historyId";
        } | {
            field: string;
            kind: "syncToken";
        };
        provenance?: "poll" | "cdc" | undefined;
    }, {
        cursor: {
            field: string;
            kind: "systemModstamp";
        } | {
            field: string;
            kind: "replayId";
        } | {
            field: string;
            kind: "timestamp";
        } | {
            field: string;
            kind: "eventId";
        } | {
            field: string;
            kind: "historyId";
        } | {
            field: string;
            kind: "syncToken";
        };
        provenance?: "poll" | "cdc" | undefined;
    }>;
    mapping: z.ZodArray<z.ZodObject<{
        source: z.ZodString;
        target: z.ZodString;
        transform: z.ZodOptional<z.ZodString>;
    }, "strip", z.ZodTypeAny, {
        source: string;
        target: string;
        transform?: string | undefined;
    }, {
        source: string;
        target: string;
        transform?: string | undefined;
    }>, "many">;
    filters: z.ZodDefault<z.ZodArray<z.ZodObject<{
        field: z.ZodString;
        op: z.ZodEnum<["eq", "neq", "in", "nin", "gt", "gte", "lt", "lte"]>;
        value: z.ZodUnknown;
    }, "strip", z.ZodTypeAny, {
        field: string;
        op: "eq" | "neq" | "in" | "nin" | "gt" | "gte" | "lt" | "lte";
        value?: unknown;
    }, {
        field: string;
        op: "eq" | "neq" | "in" | "nin" | "gt" | "gte" | "lt" | "lte";
        value?: unknown;
    }>, "many">>;
}, "strip", z.ZodTypeAny, {
    poll: {
        cursor: {
            field: string;
            kind: "systemModstamp";
        } | {
            field: string;
            kind: "replayId";
        } | {
            field: string;
            kind: "timestamp";
        } | {
            field: string;
            kind: "eventId";
        } | {
            field: string;
            kind: "historyId";
        } | {
            field: string;
            kind: "syncToken";
        };
        provenance?: "poll" | "cdc" | undefined;
    };
    mode: "poll";
    mapping: {
        source: string;
        target: string;
        transform?: string | undefined;
    }[];
    filters: {
        field: string;
        op: "eq" | "neq" | "in" | "nin" | "gt" | "gte" | "lt" | "lte";
        value?: unknown;
    }[];
}, {
    poll: {
        cursor: {
            field: string;
            kind: "systemModstamp";
        } | {
            field: string;
            kind: "replayId";
        } | {
            field: string;
            kind: "timestamp";
        } | {
            field: string;
            kind: "eventId";
        } | {
            field: string;
            kind: "historyId";
        } | {
            field: string;
            kind: "syncToken";
        };
        provenance?: "poll" | "cdc" | undefined;
    };
    mode: "poll";
    mapping: {
        source: string;
        target: string;
        transform?: string | undefined;
    }[];
    filters?: {
        field: string;
        op: "eq" | "neq" | "in" | "nin" | "gt" | "gte" | "lt" | "lte";
        value?: unknown;
    }[] | undefined;
}>, z.ZodObject<{
    mode: z.ZodLiteral<"webhook">;
    webhook: z.ZodObject<{
        eventIdField: z.ZodOptional<z.ZodString>;
    }, "strip", z.ZodTypeAny, {
        eventIdField?: string | undefined;
    }, {
        eventIdField?: string | undefined;
    }>;
    mapping: z.ZodArray<z.ZodObject<{
        source: z.ZodString;
        target: z.ZodString;
        transform: z.ZodOptional<z.ZodString>;
    }, "strip", z.ZodTypeAny, {
        source: string;
        target: string;
        transform?: string | undefined;
    }, {
        source: string;
        target: string;
        transform?: string | undefined;
    }>, "many">;
    filters: z.ZodDefault<z.ZodArray<z.ZodObject<{
        field: z.ZodString;
        op: z.ZodEnum<["eq", "neq", "in", "nin", "gt", "gte", "lt", "lte"]>;
        value: z.ZodUnknown;
    }, "strip", z.ZodTypeAny, {
        field: string;
        op: "eq" | "neq" | "in" | "nin" | "gt" | "gte" | "lt" | "lte";
        value?: unknown;
    }, {
        field: string;
        op: "eq" | "neq" | "in" | "nin" | "gt" | "gte" | "lt" | "lte";
        value?: unknown;
    }>, "many">>;
}, "strip", z.ZodTypeAny, {
    webhook: {
        eventIdField?: string | undefined;
    };
    mode: "webhook";
    mapping: {
        source: string;
        target: string;
        transform?: string | undefined;
    }[];
    filters: {
        field: string;
        op: "eq" | "neq" | "in" | "nin" | "gt" | "gte" | "lt" | "lte";
        value?: unknown;
    }[];
}, {
    webhook: {
        eventIdField?: string | undefined;
    };
    mode: "webhook";
    mapping: {
        source: string;
        target: string;
        transform?: string | undefined;
    }[];
    filters?: {
        field: string;
        op: "eq" | "neq" | "in" | "nin" | "gt" | "gte" | "lt" | "lte";
        value?: unknown;
    }[] | undefined;
}>]>;
type DetectionConfig = z.infer<typeof DetectionConfigSchema>;

export { CURSOR_DIVISIBILITY, type CursorStrategy, CursorStrategySchema, type DetectionConfig, DetectionConfigSchema, type FieldMapping, FieldMappingSchema, type PollDetection, PollDetectionSchema, type ResolvedFilter, ResolvedFilterSchema, type WebhookDetection, WebhookDetectionSchema, isDivisibleCursor };
